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/translate/translate_browser_test_utils.h" | |
6 | |
7 #include "base/base_paths.h" | |
8 #include "base/file_util.h" | |
9 #include "base/logging.h" | |
10 #include "base/path_service.h" | |
11 #include "base/platform_file.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "chrome/browser/component_updater/cld_component_installer.h" | |
14 #include "chrome/browser/translate/chrome_translate_client.h" | |
15 #include "chrome/common/chrome_constants.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace { | |
20 | |
21 // This constant yields the version of the CRX that has been extracted into | |
22 // the test data directory, and must be kept in sync with what is there. | |
23 // A reciprocal comment has been placed in | |
24 // chrome/test/data/cld2_component/README.chromium; don't update one without | |
25 // updating the other. | |
26 #if defined(CLD2_DYNAMIC_MODE) | |
27 const base::FilePath::CharType kCrxVersion[] = FILE_PATH_LITERAL("160"); | |
28 | |
29 void GetTestDataSourceDirectory(base::FilePath* out_path) { | |
30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, out_path)); | |
31 *out_path = out_path->Append(FILE_PATH_LITERAL("cld2_component")) | |
32 .Append(kCrxVersion); | |
33 } | |
34 #endif // defined(CLD2_DYNAMIC_MODE) | |
35 | |
36 #if defined(CLD2_DYNAMIC_MODE) && !defined(CLD2_IS_COMPONENT) | |
37 void GetStandaloneDataFileSource(base::FilePath* out_path) { | |
38 GetTestDataSourceDirectory(out_path); | |
39 *out_path = out_path->Append(FILE_PATH_LITERAL("_platform_specific")) | |
40 .Append(FILE_PATH_LITERAL("all")) | |
41 .Append(chrome::kCLDDataFilename); | |
42 } | |
43 | |
44 // Using the USER_DATA_DIR not only mimics true functionality, but also is | |
45 // important to test isolation. Each test gets its own USER_DATA_DIR, which | |
46 // ensures proper isolation between test processes running in parallel. | |
47 void GetStandaloneDataFileDestination(base::FilePath* out_path) { | |
48 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, out_path)); | |
49 *out_path = out_path->Append(chrome::kCLDDataFilename); | |
50 } | |
51 | |
52 void DeleteStandaloneDataFile() { | |
53 base::FilePath path; | |
54 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&path)); | |
55 DLOG(INFO) << "Deleting CLD test data file from " << path.value(); | |
56 base::DeleteFile(path, false); | |
57 } | |
58 | |
59 void CopyStandaloneDataFile() { | |
60 DeleteStandaloneDataFile(); // sanity: blow away any old copies. | |
61 base::FilePath target_file; | |
62 GetStandaloneDataFileDestination(&target_file); | |
63 base::FilePath target_dir = target_file.DirName(); | |
64 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | |
65 base::FilePath source_file; | |
66 GetStandaloneDataFileSource(&source_file); | |
67 DLOG(INFO) << "Copying CLD test data file from " << source_file.value() | |
68 << " to " << target_file.value(); | |
69 ASSERT_TRUE(base::CopyFile(source_file, target_file)); | |
70 ASSERT_TRUE(base::PathExists(target_file)); | |
71 } | |
72 #endif // defined(CLD2_DYNAMIC_MODE) && !defined(CLD2_IS_COMPONENT) | |
73 | |
74 #if defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | |
75 // DIR_COMPONENT_CLD2 is also defined as being relative to USER_DATA_DIR, so | |
76 // like GetStandaloneDataFileDestination, this is safe to run in multiple | |
77 // parallel test processes. | |
78 void GetExtractedComponentDestination(base::FilePath* out_path) { | |
79 ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); | |
80 } | |
81 | |
82 void GetComponentDataFileDestination(base::FilePath* out_path) { | |
83 GetExtractedComponentDestination(out_path); | |
84 *out_path = out_path->Append(kCrxVersion) | |
85 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
86 .Append(FILE_PATH_LITERAL("all")) | |
87 .Append(chrome::kCLDDataFilename); | |
88 } | |
89 | |
90 void DeleteComponentTree() { | |
91 base::FilePath tree_path; | |
92 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); | |
93 DLOG(INFO) << "Deleting CLD component test files from " << tree_path.value(); | |
94 base::DeleteFile(tree_path, true); | |
95 } | |
96 | |
97 void CopyComponentTree() { | |
98 DeleteComponentTree(); // sanity: blow away any old copies. | |
99 base::FilePath target_dir; | |
100 GetExtractedComponentDestination(&target_dir); | |
101 base::FilePath source_dir; | |
102 GetTestDataSourceDirectory(&source_dir); | |
103 DLOG(INFO) << "Copying CLD component test files from " << source_dir.value() | |
104 << " to " << target_dir.value(); | |
105 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | |
106 ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); | |
107 ASSERT_TRUE(base::PathExists(target_dir)); | |
108 base::FilePath check_path; | |
109 GetComponentDataFileDestination(&check_path); | |
110 ASSERT_TRUE(base::PathExists(check_path)); | |
111 } | |
112 #endif // defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | |
113 | |
114 } // namespace | |
115 | |
116 namespace test { | |
117 | |
118 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { | |
119 // Constructor does nothing in all cases. See Init() for initialization. | |
120 } | |
121 | |
122 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { | |
123 DLOG(INFO) << "Tearing down CLD data harness"; | |
124 #if defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | |
125 // Dynamic data mode is enabled and we are using the component updater. | |
126 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | |
127 base::FilePath()); | |
128 DeleteComponentTree(); | |
129 #elif defined(CLD2_DYNAMIC_MODE) | |
130 // Dynamic data mode is enabled and we are using a standalone file. | |
131 ClearStandaloneDataFileState(); | |
132 DeleteStandaloneDataFile(); | |
133 #endif // defined(CLD2_DYNAMIC_MODE) | |
134 } | |
135 | |
136 void ScopedCLDDynamicDataHarness::Init() { | |
137 DLOG(INFO) << "Initializing CLD data harness"; | |
138 #if defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | |
139 // Dynamic data mode is enabled and we are using the component updater. | |
140 ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); | |
141 base::FilePath data_file; | |
142 GetComponentDataFileDestination(&data_file); | |
143 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | |
144 data_file); | |
145 base::FilePath result = component_updater::GetLatestCldDataFile(); | |
146 ASSERT_EQ(data_file, result); | |
147 #elif defined(CLD2_DYNAMIC_MODE) | |
148 // Dynamic data mode is enabled and we are using a standalone file. | |
149 ASSERT_NO_FATAL_FAILURE(ClearStandaloneDataFileState()); | |
150 ASSERT_NO_FATAL_FAILURE(CopyStandaloneDataFile()); | |
151 #endif // defined(CLD2_DYNAMIC_MODE) | |
152 } | |
153 | |
154 void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { | |
155 #if defined(CLD2_DYNAMIC_MODE) | |
156 DLOG(INFO) << "Clearing CLD data file state"; | |
157 // This code must live within the class in order to gain "friend" access. | |
158 base::AutoLock lock(ChromeTranslateClient::s_file_lock_.Get()); | |
159 if (ChromeTranslateClient::s_cached_file_) { | |
160 // Leaks any open handle, no way to avoid safely. | |
161 ChromeTranslateClient::s_cached_file_ = NULL; | |
162 ChromeTranslateClient::s_cached_data_offset_ = 0; | |
163 ChromeTranslateClient::s_cached_data_length_ = 0; | |
164 } | |
165 #endif // defined(CLD2_DYNAMIC_MODE) | |
166 } | |
167 | |
168 } // namespace test | |
OLD | NEW |