OLD | NEW |
(Empty) | |
| 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 = traits.GetInstalledPath(base_dir); |
| 80 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); |
| 81 } |
| 82 |
| 83 TEST_F(CldComponentInstallerTest, GetBaseDirectory) { |
| 84 const base::FilePath result = traits.GetBaseDirectory(); |
| 85 ASSERT_FALSE(result.empty()); |
| 86 } |
| 87 |
| 88 TEST_F(CldComponentInstallerTest, GetHash) { |
| 89 std::vector<uint8> hash; |
| 90 traits.GetHash(&hash); |
| 91 ASSERT_EQ(static_cast<size_t>(32), hash.size()); |
| 92 } |
| 93 |
| 94 TEST_F(CldComponentInstallerTest, GetName) { |
| 95 ASSERT_FALSE(traits.GetName().empty()); |
| 96 } |
| 97 |
| 98 TEST_F(CldComponentInstallerTest, ComponentReady) { |
| 99 scoped_ptr<base::DictionaryValue> manifest; |
| 100 const base::FilePath install_dir(FILE_PATH_LITERAL("/foo")); |
| 101 const base::Version version("1.2.3.4"); |
| 102 traits.ComponentReady(version, install_dir, manifest.Pass()); |
| 103 base::FilePath result = component_updater::GetLatestCldDataFile(); |
| 104 ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), |
| 105 install_dir.AsUTF16Unsafe(), |
| 106 true)); |
| 107 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); |
| 108 } |
| 109 |
| 110 } // namespace component_updater |
OLD | NEW |