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