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