| 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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.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 "chrome/common/chrome_constants.h" |
| 18 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "components/translate/content/browser/data_file_browser_cld_data_provid
er.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 21 | 22 |
| 22 namespace component_updater { | 23 namespace component_updater { |
| 23 | 24 |
| 24 class CldComponentInstallerTest : public PlatformTest { | 25 class CldComponentInstallerTest : public PlatformTest { |
| 25 public: | 26 public: |
| 26 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
| 27 PlatformTest::SetUp(); | 28 PlatformTest::SetUp(); |
| 28 | 29 |
| 29 // ScopedTempDir automatically does a recursive delete on the entire | 30 // ScopedTempDir automatically does a recursive delete on the entire |
| 30 // directory in its destructor, so no cleanup is required in TearDown. | 31 // 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 // 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 // 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 // created *as temp files*, they will still get cleaned up automagically. |
| 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 35 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 35 | 36 |
| 37 cld_data_file_name = |
| 38 component_updater::CldComponentInstallerTraits::GetCldDataFileName(); |
| 39 |
| 36 // The "latest CLD data file" is a static piece of information, and thus | 40 // The "latest CLD data file" is a static piece of information, and thus |
| 37 // for correctness we empty it before each test. | 41 // for correctness we empty it before each test. |
| 38 traits.SetLatestCldDataFile(base::FilePath()); | 42 traits.SetLatestCldDataFile(base::FilePath()); |
| 39 } | 43 } |
| 40 | 44 |
| 45 const base::FilePath::CharType* cld_data_file_name; |
| 41 base::ScopedTempDir temp_dir_; | 46 base::ScopedTempDir temp_dir_; |
| 42 component_updater::CldComponentInstallerTraits traits; | 47 component_updater::CldComponentInstallerTraits traits; |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { | 50 TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { |
| 46 ASSERT_TRUE(component_updater::GetLatestCldDataFile().empty()); | |
| 47 const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); | 51 const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); |
| 48 traits.SetLatestCldDataFile(expected); | 52 traits.SetLatestCldDataFile(expected); |
| 49 | 53 |
| 50 base::FilePath result = component_updater::GetLatestCldDataFile(); | 54 base::FilePath result = |
| 55 translate::DataFileBrowserCldDataProvider::GetCldDataFilePath(); |
| 51 ASSERT_EQ(expected, result); | 56 ASSERT_EQ(expected, result); |
| 52 } | 57 } |
| 53 | 58 |
| 54 TEST_F(CldComponentInstallerTest, VerifyInstallation) { | 59 TEST_F(CldComponentInstallerTest, VerifyInstallation) { |
| 55 // All files are created within a ScopedTempDir, which deletes all | 60 // All files are created within a ScopedTempDir, which deletes all |
| 56 // children when its destructor is called (at the end of each test). | 61 // children when its destructor is called (at the end of each test). |
| 57 ASSERT_FALSE(traits.VerifyInstallation(temp_dir_.path())); | 62 ASSERT_FALSE(traits.VerifyInstallation(temp_dir_.path())); |
| 58 const base::FilePath data_file_dir = | 63 const base::FilePath data_file_dir = |
| 59 temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append( | 64 temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append( |
| 60 FILE_PATH_LITERAL("all")); | 65 FILE_PATH_LITERAL("all")); |
| 61 ASSERT_TRUE(base::CreateDirectory(data_file_dir)); | 66 ASSERT_TRUE(base::CreateDirectory(data_file_dir)); |
| 62 const base::FilePath data_file = | 67 const base::FilePath data_file = |
| 63 data_file_dir.Append(chrome::kCLDDataFilename); | 68 data_file_dir.Append(cld_data_file_name); |
| 64 const std::string test_data("fake cld2 data file content here :)"); | 69 const std::string test_data("fake cld2 data file content here :)"); |
| 65 ASSERT_EQ(static_cast<int32>(test_data.length()), | 70 ASSERT_EQ(static_cast<int32>(test_data.length()), |
| 66 base::WriteFile(data_file, test_data.c_str(), test_data.length())); | 71 base::WriteFile(data_file, test_data.c_str(), test_data.length())); |
| 67 ASSERT_TRUE(traits.VerifyInstallation(temp_dir_.path())); | 72 ASSERT_TRUE(traits.VerifyInstallation(temp_dir_.path())); |
| 68 } | 73 } |
| 69 | 74 |
| 70 TEST_F(CldComponentInstallerTest, OnCustomInstall) { | 75 TEST_F(CldComponentInstallerTest, OnCustomInstall) { |
| 71 const base::DictionaryValue manifest; | 76 const base::DictionaryValue manifest; |
| 72 const base::FilePath install_dir; | 77 const base::FilePath install_dir; |
| 73 // Sanity: shouldn't crash. | 78 // Sanity: shouldn't crash. |
| 74 ASSERT_TRUE(traits.OnCustomInstall(manifest, install_dir)); | 79 ASSERT_TRUE(traits.OnCustomInstall(manifest, install_dir)); |
| 75 } | 80 } |
| 76 | 81 |
| 77 TEST_F(CldComponentInstallerTest, GetInstalledPath) { | 82 TEST_F(CldComponentInstallerTest, GetInstalledPath) { |
| 78 const base::FilePath base_dir; | 83 const base::FilePath base_dir; |
| 79 const base::FilePath result = | 84 const base::FilePath result = |
| 80 CldComponentInstallerTraits::GetInstalledPath(base_dir); | 85 CldComponentInstallerTraits::GetInstalledPath(base_dir); |
| 81 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); | 86 ASSERT_TRUE(EndsWith(result.value(), cld_data_file_name, true)); |
| 82 } | 87 } |
| 83 | 88 |
| 84 TEST_F(CldComponentInstallerTest, GetBaseDirectory) { | 89 TEST_F(CldComponentInstallerTest, GetBaseDirectory) { |
| 85 const base::FilePath result = traits.GetBaseDirectory(); | 90 const base::FilePath result = traits.GetBaseDirectory(); |
| 86 ASSERT_FALSE(result.empty()); | 91 ASSERT_FALSE(result.empty()); |
| 87 } | 92 } |
| 88 | 93 |
| 89 TEST_F(CldComponentInstallerTest, GetHash) { | 94 TEST_F(CldComponentInstallerTest, GetHash) { |
| 90 std::vector<uint8> hash; | 95 std::vector<uint8> hash; |
| 91 traits.GetHash(&hash); | 96 traits.GetHash(&hash); |
| 92 ASSERT_EQ(static_cast<size_t>(32), hash.size()); | 97 ASSERT_EQ(static_cast<size_t>(32), hash.size()); |
| 93 } | 98 } |
| 94 | 99 |
| 95 TEST_F(CldComponentInstallerTest, GetName) { | 100 TEST_F(CldComponentInstallerTest, GetName) { |
| 96 ASSERT_FALSE(traits.GetName().empty()); | 101 ASSERT_FALSE(traits.GetName().empty()); |
| 97 } | 102 } |
| 98 | 103 |
| 99 TEST_F(CldComponentInstallerTest, ComponentReady) { | 104 TEST_F(CldComponentInstallerTest, ComponentReady) { |
| 100 scoped_ptr<base::DictionaryValue> manifest; | 105 scoped_ptr<base::DictionaryValue> manifest; |
| 101 const base::FilePath install_dir(FILE_PATH_LITERAL("/foo")); | 106 const base::FilePath install_dir(FILE_PATH_LITERAL("/foo")); |
| 102 const base::Version version("1.2.3.4"); | 107 const base::Version version("1.2.3.4"); |
| 103 traits.ComponentReady(version, install_dir, manifest.Pass()); | 108 traits.ComponentReady(version, install_dir, manifest.Pass()); |
| 104 base::FilePath result = component_updater::GetLatestCldDataFile(); | 109 base::FilePath result = |
| 110 translate::DataFileBrowserCldDataProvider::GetCldDataFilePath(); |
| 105 ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), | 111 ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), |
| 106 install_dir.AsUTF16Unsafe(), | 112 install_dir.AsUTF16Unsafe(), |
| 107 true)); | 113 true)); |
| 108 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); | 114 ASSERT_TRUE(EndsWith(result.value(), cld_data_file_name, true)); |
| 109 } | 115 } |
| 110 | 116 |
| 111 } // namespace component_updater | 117 } // namespace component_updater |
| OLD | NEW |