Home | History | Annotate | Download | only in test
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <vector>
      6 
      7 #include "base/file_util.h"
      8 #include "base/files/file_path.h"
      9 #include "base/files/scoped_temp_dir.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/run_loop.h"
     12 #include "base/strings/string_util.h"
     13 #include "base/strings/utf_string_conversions.h"
     14 #include "base/values.h"
     15 #include "base/version.h"
     16 #include "chrome/browser/component_updater/cld_component_installer.h"
     17 #include "chrome/common/chrome_constants.h"
     18 #include "chrome/common/chrome_paths.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 #include "testing/platform_test.h"
     21 
     22 namespace component_updater {
     23 
     24 class CldComponentInstallerTest : public PlatformTest {
     25  public:
     26   virtual void SetUp() OVERRIDE {
     27     PlatformTest::SetUp();
     28 
     29     // ScopedTempDir automatically does a recursive delete on the entire
     30     // directory in its destructor, so no cleanup is required in TearDown.
     31     // Note that all files created by this test case are created within the
     32     // directory that is created here, so even though they are not explicitly
     33     // created *as temp files*, they will still get cleaned up automagically.
     34     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     35 
     36     // The "latest CLD data file" is a static piece of information, and thus
     37     // for correctness we empty it before each test.
     38     traits.SetLatestCldDataFile(base::FilePath());
     39   }
     40 
     41   base::ScopedTempDir temp_dir_;
     42   component_updater::CldComponentInstallerTraits traits;
     43 };
     44 
     45 TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) {
     46   ASSERT_TRUE(component_updater::GetLatestCldDataFile().empty());
     47   const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test"));
     48   traits.SetLatestCldDataFile(expected);
     49 
     50   base::FilePath result = component_updater::GetLatestCldDataFile();
     51   ASSERT_EQ(expected, result);
     52 }
     53 
     54 TEST_F(CldComponentInstallerTest, VerifyInstallation) {
     55   // All files are created within a ScopedTempDir, which deletes all
     56   // children when its destructor is called (at the end of each test).
     57   ASSERT_FALSE(traits.VerifyInstallation(temp_dir_.path()));
     58   const base::FilePath data_file_dir =
     59       temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append(
     60           FILE_PATH_LITERAL("all"));
     61   ASSERT_TRUE(base::CreateDirectory(data_file_dir));
     62   const base::FilePath data_file =
     63       data_file_dir.Append(chrome::kCLDDataFilename);
     64   const std::string test_data("fake cld2 data file content here :)");
     65   ASSERT_EQ(static_cast<int32>(test_data.length()),
     66             base::WriteFile(data_file, test_data.c_str(), test_data.length()));
     67   ASSERT_TRUE(traits.VerifyInstallation(temp_dir_.path()));
     68 }
     69 
     70 TEST_F(CldComponentInstallerTest, OnCustomInstall) {
     71   const base::DictionaryValue manifest;
     72   const base::FilePath install_dir;
     73   // Sanity: shouldn't crash.
     74   ASSERT_TRUE(traits.OnCustomInstall(manifest, install_dir));
     75 }
     76 
     77 TEST_F(CldComponentInstallerTest, GetInstalledPath) {
     78   const base::FilePath base_dir;
     79   const base::FilePath result =
     80       CldComponentInstallerTraits::GetInstalledPath(base_dir);
     81   ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true));
     82 }
     83 
     84 TEST_F(CldComponentInstallerTest, GetBaseDirectory) {
     85   const base::FilePath result = traits.GetBaseDirectory();
     86   ASSERT_FALSE(result.empty());
     87 }
     88 
     89 TEST_F(CldComponentInstallerTest, GetHash) {
     90   std::vector<uint8> hash;
     91   traits.GetHash(&hash);
     92   ASSERT_EQ(static_cast<size_t>(32), hash.size());
     93 }
     94 
     95 TEST_F(CldComponentInstallerTest, GetName) {
     96   ASSERT_FALSE(traits.GetName().empty());
     97 }
     98 
     99 TEST_F(CldComponentInstallerTest, ComponentReady) {
    100   scoped_ptr<base::DictionaryValue> manifest;
    101   const base::FilePath install_dir(FILE_PATH_LITERAL("/foo"));
    102   const base::Version version("1.2.3.4");
    103   traits.ComponentReady(version, install_dir, manifest.Pass());
    104   base::FilePath result = component_updater::GetLatestCldDataFile();
    105   ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(),
    106                          install_dir.AsUTF16Unsafe(),
    107                          true));
    108   ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true));
    109 }
    110 
    111 }  // namespace component_updater
    112