| 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 "chrome/browser/component_updater/cld_component_installer.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/files/file_path.h" | |
| 15 #include "base/files/file_util.h" | |
| 16 #include "base/files/scoped_temp_dir.h" | |
| 17 #include "base/macros.h" | |
| 18 #include "base/strings/string_util.h" | |
| 19 #include "base/strings/utf_string_conversions.h" | |
| 20 #include "base/values.h" | |
| 21 #include "base/version.h" | |
| 22 #include "components/translate/content/browser/browser_cld_data_provider.h" | |
| 23 #include "components/translate/content/common/cld_data_source.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 #include "testing/platform_test.h" | |
| 26 | |
| 27 using component_updater::CldComponentInstallerTraits; | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // This has to match what's in cld_component_installer.cc. | |
| 32 const base::FilePath::CharType kTestCldDataFileName[] = | |
| 33 FILE_PATH_LITERAL("cld2_data.bin"); | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 namespace component_updater { | |
| 38 | |
| 39 class CldComponentInstallerTest : public PlatformTest { | |
| 40 public: | |
| 41 CldComponentInstallerTest() {} | |
| 42 void SetUp() override { | |
| 43 PlatformTest::SetUp(); | |
| 44 translate::CldDataSource::DisableSanityChecksForTest(); | |
| 45 | |
| 46 // ScopedTempDir automatically does a recursive delete on the entire | |
| 47 // directory in its destructor, so no cleanup is required in TearDown. | |
| 48 // Note that all files created by this test case are created within the | |
| 49 // directory that is created here, so even though they are not explicitly | |
| 50 // created *as temp files*, they will still get cleaned up automagically. | |
| 51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 52 | |
| 53 // The "latest CLD data file" is a static piece of information, and thus | |
| 54 // for correctness we empty it before each test. | |
| 55 CldComponentInstallerTraits::SetLatestCldDataFile(base::FilePath()); | |
| 56 base::FilePath path_now = | |
| 57 CldComponentInstallerTraits::GetLatestCldDataFile(); | |
| 58 ASSERT_TRUE(path_now.empty()); | |
| 59 } | |
| 60 | |
| 61 void TearDown() override { | |
| 62 // Restore sanity checks. | |
| 63 translate::CldDataSource::EnableSanityChecksForTest(); | |
| 64 } | |
| 65 | |
| 66 protected: | |
| 67 base::ScopedTempDir temp_dir_; | |
| 68 CldComponentInstallerTraits traits_; | |
| 69 | |
| 70 private: | |
| 71 DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTest); | |
| 72 }; | |
| 73 | |
| 74 TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { | |
| 75 const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); | |
| 76 CldComponentInstallerTraits::SetLatestCldDataFile(expected); | |
| 77 base::FilePath result = CldComponentInstallerTraits::GetLatestCldDataFile(); | |
| 78 ASSERT_EQ(expected, result); | |
| 79 } | |
| 80 | |
| 81 TEST_F(CldComponentInstallerTest, VerifyInstallation) { | |
| 82 // All files are created within a ScopedTempDir, which deletes all | |
| 83 // children when its destructor is called (at the end of each test). | |
| 84 const base::DictionaryValue manifest; | |
| 85 ASSERT_FALSE(traits_.VerifyInstallation(manifest, temp_dir_.path())); | |
| 86 const base::FilePath data_file_dir = | |
| 87 temp_dir_.path() | |
| 88 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 89 .Append(FILE_PATH_LITERAL("all")); | |
| 90 ASSERT_TRUE(base::CreateDirectory(data_file_dir)); | |
| 91 const base::FilePath data_file = data_file_dir.Append(kTestCldDataFileName); | |
| 92 const std::string test_data("fake cld2 data file content here :)"); | |
| 93 ASSERT_EQ(static_cast<int32_t>(test_data.length()), | |
| 94 base::WriteFile(data_file, test_data.c_str(), test_data.length())); | |
| 95 ASSERT_TRUE(traits_.VerifyInstallation(manifest, temp_dir_.path())); | |
| 96 } | |
| 97 | |
| 98 TEST_F(CldComponentInstallerTest, OnCustomInstall) { | |
| 99 const base::DictionaryValue manifest; | |
| 100 const base::FilePath install_dir; | |
| 101 // Sanity: shouldn't crash. | |
| 102 ASSERT_TRUE(traits_.OnCustomInstall(manifest, install_dir)); | |
| 103 } | |
| 104 | |
| 105 TEST_F(CldComponentInstallerTest, GetInstalledPath) { | |
| 106 const base::FilePath base_dir; | |
| 107 const base::FilePath result = | |
| 108 CldComponentInstallerTraits::GetInstalledPath(base_dir); | |
| 109 ASSERT_TRUE(base::EndsWith(result.value(), kTestCldDataFileName, | |
| 110 base::CompareCase::SENSITIVE)); | |
| 111 } | |
| 112 | |
| 113 TEST_F(CldComponentInstallerTest, GetRelativeInstallDir) { | |
| 114 ASSERT_FALSE(traits_.GetRelativeInstallDir().empty()); | |
| 115 } | |
| 116 | |
| 117 TEST_F(CldComponentInstallerTest, GetHash) { | |
| 118 std::vector<uint8_t> hash; | |
| 119 traits_.GetHash(&hash); | |
| 120 ASSERT_EQ(static_cast<size_t>(32), hash.size()); | |
| 121 } | |
| 122 | |
| 123 TEST_F(CldComponentInstallerTest, GetName) { | |
| 124 ASSERT_FALSE(traits_.GetName().empty()); | |
| 125 } | |
| 126 | |
| 127 TEST_F(CldComponentInstallerTest, ComponentReady) { | |
| 128 std::unique_ptr<base::DictionaryValue> manifest; | |
| 129 const base::FilePath install_dir(FILE_PATH_LITERAL("/foo")); | |
| 130 const base::Version version("1.2.3.4"); | |
| 131 traits_.ComponentReady(version, install_dir, std::move(manifest)); | |
| 132 base::FilePath result = CldComponentInstallerTraits::GetLatestCldDataFile(); | |
| 133 ASSERT_TRUE(base::StartsWith(result.AsUTF16Unsafe(), | |
| 134 install_dir.AsUTF16Unsafe(), | |
| 135 base::CompareCase::SENSITIVE)); | |
| 136 ASSERT_TRUE(base::EndsWith(result.value(), kTestCldDataFileName, | |
| 137 base::CompareCase::SENSITIVE)); | |
| 138 } | |
| 139 | |
| 140 } // namespace component_updater | |
| OLD | NEW |