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/path_service.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "base/version.h" | 16 #include "base/version.h" |
16 #include "chrome/browser/component_updater/cld_component_installer.h" | 17 #include "chrome/browser/component_updater/cld_component_installer.h" |
17 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.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 |
| 30 base::FilePath base_dir; |
| 31 PathService::Get(chrome::DIR_COMPONENT_CLD2, &base_dir); |
| 32 traits.reset(new component_updater::CldComponentInstallerTraits( |
| 33 base_dir, base::FilePath(chrome::kCLDDataFilename))); |
| 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 traits->SetLatestCldDataFile(base::FilePath()); |
39 } | 45 } |
40 | 46 |
41 base::ScopedTempDir temp_dir_; | 47 base::ScopedTempDir temp_dir_; |
42 component_updater::CldComponentInstallerTraits traits; | 48 scoped_ptr<component_updater::CldComponentInstallerTraits> traits; |
43 }; | 49 }; |
44 | 50 |
45 TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { | 51 TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { |
46 ASSERT_TRUE(component_updater::GetLatestCldDataFile().empty()); | 52 ASSERT_TRUE(component_updater::GetLatestCldDataFile().empty()); |
47 const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); | 53 const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); |
48 traits.SetLatestCldDataFile(expected); | 54 traits->SetLatestCldDataFile(expected); |
49 | 55 |
50 base::FilePath result = component_updater::GetLatestCldDataFile(); | 56 base::FilePath result = component_updater::GetLatestCldDataFile(); |
51 ASSERT_EQ(expected, result); | 57 ASSERT_EQ(expected, result); |
52 } | 58 } |
53 | 59 |
54 TEST_F(CldComponentInstallerTest, VerifyInstallation) { | 60 TEST_F(CldComponentInstallerTest, VerifyInstallation) { |
55 // All files are created within a ScopedTempDir, which deletes all | 61 // All files are created within a ScopedTempDir, which deletes all |
56 // children when its destructor is called (at the end of each test). | 62 // children when its destructor is called (at the end of each test). |
57 ASSERT_FALSE(traits.VerifyInstallation(temp_dir_.path())); | 63 ASSERT_FALSE(traits->VerifyInstallation(temp_dir_.path())); |
58 const base::FilePath data_file_dir = | 64 const base::FilePath data_file_dir = |
59 temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append( | 65 temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append( |
60 FILE_PATH_LITERAL("all")); | 66 FILE_PATH_LITERAL("all")); |
61 ASSERT_TRUE(base::CreateDirectory(data_file_dir)); | 67 ASSERT_TRUE(base::CreateDirectory(data_file_dir)); |
62 const base::FilePath data_file = | 68 const base::FilePath data_file = |
63 data_file_dir.Append(chrome::kCLDDataFilename); | 69 data_file_dir.Append(chrome::kCLDDataFilename); |
64 const std::string test_data("fake cld2 data file content here :)"); | 70 const std::string test_data("fake cld2 data file content here :)"); |
65 ASSERT_EQ(static_cast<int32>(test_data.length()), | 71 ASSERT_EQ(static_cast<int32>(test_data.length()), |
66 base::WriteFile(data_file, test_data.c_str(), test_data.length())); | 72 base::WriteFile(data_file, test_data.c_str(), test_data.length())); |
67 ASSERT_TRUE(traits.VerifyInstallation(temp_dir_.path())); | 73 ASSERT_TRUE(traits->VerifyInstallation(temp_dir_.path())); |
68 } | 74 } |
69 | 75 |
70 TEST_F(CldComponentInstallerTest, OnCustomInstall) { | 76 TEST_F(CldComponentInstallerTest, OnCustomInstall) { |
71 const base::DictionaryValue manifest; | 77 const base::DictionaryValue manifest; |
72 const base::FilePath install_dir; | 78 const base::FilePath install_dir; |
73 // Sanity: shouldn't crash. | 79 // Sanity: shouldn't crash. |
74 ASSERT_TRUE(traits.OnCustomInstall(manifest, install_dir)); | 80 ASSERT_TRUE(traits->OnCustomInstall(manifest, install_dir)); |
75 } | 81 } |
76 | 82 |
77 TEST_F(CldComponentInstallerTest, GetInstalledPath) { | 83 TEST_F(CldComponentInstallerTest, GetInstalledPath) { |
78 const base::FilePath base_dir; | 84 const base::FilePath base_dir; |
79 const base::FilePath result = | 85 const base::FilePath result = traits->GetInstalledPath(base_dir); |
80 CldComponentInstallerTraits::GetInstalledPath(base_dir); | |
81 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); | 86 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, 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 = component_updater::GetLatestCldDataFile(); |
105 ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), | 110 ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), |
106 install_dir.AsUTF16Unsafe(), | 111 install_dir.AsUTF16Unsafe(), |
107 true)); | 112 true)); |
108 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); | 113 ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true)); |
109 } | 114 } |
110 | 115 |
111 } // namespace component_updater | 116 } // namespace component_updater |
OLD | NEW |