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 "chrome/browser/translate/translate_browser_test_utils.h" | 5 #include "chrome/browser/translate/translate_browser_test_utils.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 void GetTestDataSourceDirectory(base::FilePath* out_path) { | 29 void GetTestDataSourceDirectory(base::FilePath* out_path) { |
30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, out_path)); | 30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, out_path)); |
31 *out_path = out_path->Append(FILE_PATH_LITERAL("cld2_component")) | 31 *out_path = out_path->Append(FILE_PATH_LITERAL("cld2_component")) |
32 .Append(kCrxVersion); | 32 .Append(kCrxVersion); |
33 } | 33 } |
34 #endif // defined(CLD2_DYNAMIC_MODE) | 34 #endif // defined(CLD2_DYNAMIC_MODE) |
35 | 35 |
36 #if defined(CLD2_DYNAMIC_MODE) && !defined(CLD2_IS_COMPONENT) | 36 #if defined(CLD2_DYNAMIC_MODE) && !defined(CLD2_IS_COMPONENT) |
37 void GetStandaloneDataFileSource(base::FilePath* out_path) { | 37 void GetStandaloneDataFileSource(base::FilePath* out_path) { |
38 ASSERT_NO_FATAL_FAILURE(GetTestDataSourceDirectory(out_path)); | 38 GetTestDataSourceDirectory(out_path); |
39 *out_path = out_path->Append(FILE_PATH_LITERAL("_platform_specific")) | 39 *out_path = out_path->Append(FILE_PATH_LITERAL("_platform_specific")) |
40 .Append(FILE_PATH_LITERAL("all")) | 40 .Append(FILE_PATH_LITERAL("all")) |
41 .Append(chrome::kCLDDataFilename); | 41 .Append(chrome::kCLDDataFilename); |
42 } | 42 } |
43 | 43 |
44 // Using the USER_DATA_DIR not only mimics true functionality, but also is | 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 | 45 // important to test isolation. Each test gets its own USER_DATA_DIR, which |
46 // ensures proper isolation between test processes running in parallel. | 46 // ensures proper isolation between test processes running in parallel. |
47 void GetStandaloneDataFileDestination(base::FilePath* out_path) { | 47 void GetStandaloneDataFileDestination(base::FilePath* out_path) { |
48 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, out_path)); | 48 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, out_path)); |
49 *out_path = out_path->Append(chrome::kCLDDataFilename); | 49 *out_path = out_path->Append(chrome::kCLDDataFilename); |
50 } | 50 } |
51 | 51 |
52 void DeleteStandaloneDataFile() { | 52 void DeleteStandaloneDataFile() { |
53 base::FilePath path; | 53 base::FilePath path; |
54 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&path)); | 54 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&path)); |
55 DLOG(INFO) << "Deleting CLD test data file from " << path.value(); | 55 DLOG(INFO) << "Deleting CLD test data file from " << path.value(); |
56 base::DeleteFile(path, false); | 56 base::DeleteFile(path, false); |
57 } | 57 } |
58 | 58 |
59 void CopyStandaloneDataFile() { | 59 void CopyStandaloneDataFile() { |
60 DeleteStandaloneDataFile(); // sanity: blow away any old copies. | 60 DeleteStandaloneDataFile(); // sanity: blow away any old copies. |
61 base::FilePath target_file; | 61 base::FilePath target_file; |
62 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&target_file)); | 62 GetStandaloneDataFileDestination(&target_file); |
63 base::FilePath target_dir = target_file.DirName(); | 63 base::FilePath target_dir = target_file.DirName(); |
64 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | 64 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); |
65 base::FilePath source_file; | 65 base::FilePath source_file; |
66 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileSource(&source_file)); | 66 GetStandaloneDataFileSource(&source_file); |
67 DLOG(INFO) << "Copying CLD test data file from " << source_file.value() | 67 DLOG(INFO) << "Copying CLD test data file from " << source_file.value() |
68 << " to " << target_file.value(); | 68 << " to " << target_file.value(); |
69 ASSERT_TRUE(base::CopyFile(source_file, target_file)); | 69 ASSERT_TRUE(base::CopyFile(source_file, target_file)); |
70 ASSERT_TRUE(base::PathExists(target_file)); | 70 ASSERT_TRUE(base::PathExists(target_file)); |
71 } | 71 } |
72 #endif // defined(CLD2_DYNAMIC_MODE) && !defined(CLD2_IS_COMPONENT) | 72 #endif // defined(CLD2_DYNAMIC_MODE) && !defined(CLD2_IS_COMPONENT) |
73 | 73 |
74 #if defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | 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 | 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 | 76 // like GetStandaloneDataFileDestination, this is safe to run in multiple |
77 // parallel test processes. | 77 // parallel test processes. |
78 void GetExtractedComponentDestination(base::FilePath* out_path) { | 78 void GetExtractedComponentDestination(base::FilePath* out_path) { |
79 ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); | 79 ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); |
80 } | 80 } |
81 | 81 |
82 void GetComponentDataFileDestination(base::FilePath* out_path) { | 82 void GetComponentDataFileDestination(base::FilePath* out_path) { |
83 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(out_path)); | 83 GetExtractedComponentDestination(out_path); |
84 *out_path = out_path->Append(kCrxVersion) | 84 *out_path = out_path->Append(kCrxVersion) |
85 .Append(FILE_PATH_LITERAL("_platform_specific")) | 85 .Append(FILE_PATH_LITERAL("_platform_specific")) |
86 .Append(FILE_PATH_LITERAL("all")) | 86 .Append(FILE_PATH_LITERAL("all")) |
87 .Append(chrome::kCLDDataFilename); | 87 .Append(chrome::kCLDDataFilename); |
88 } | 88 } |
89 | 89 |
90 void DeleteComponentTree() { | 90 void DeleteComponentTree() { |
91 base::FilePath tree_path; | 91 base::FilePath tree_path; |
92 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); | 92 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); |
93 DLOG(INFO) << "Deleting CLD component test files from " << tree_path.value(); | 93 DLOG(INFO) << "Deleting CLD component test files from " << tree_path.value(); |
94 base::DeleteFile(tree_path, true); | 94 base::DeleteFile(tree_path, true); |
95 } | 95 } |
96 | 96 |
97 void CopyComponentTree() { | 97 void CopyComponentTree() { |
98 DeleteComponentTree(); // sanity: blow away any old copies. | 98 DeleteComponentTree(); // sanity: blow away any old copies. |
99 base::FilePath target_dir; | 99 base::FilePath target_dir; |
100 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&target_dir)); | 100 GetExtractedComponentDestination(&target_dir); |
101 base::FilePath source_dir; | 101 base::FilePath source_dir; |
| 102 GetTestDataSourceDirectory(&source_dir); |
102 DLOG(INFO) << "Copying CLD component test files from " << source_dir.value() | 103 DLOG(INFO) << "Copying CLD component test files from " << source_dir.value() |
103 << " to " << target_dir.value(); | 104 << " to " << target_dir.value(); |
104 ASSERT_NO_FATAL_FAILURE(GetTestDataSourceDirectory(&source_dir)); | |
105 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | 105 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); |
106 ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); | 106 ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); |
107 ASSERT_TRUE(base::PathExists(target_dir)); | 107 ASSERT_TRUE(base::PathExists(target_dir)); |
108 base::FilePath check_path; | 108 base::FilePath check_path; |
109 ASSERT_NO_FATAL_FAILURE(GetComponentDataFileDestination(&check_path)); | 109 GetComponentDataFileDestination(&check_path); |
110 ASSERT_TRUE(base::PathExists(check_path)); | 110 ASSERT_TRUE(base::PathExists(check_path)); |
111 } | 111 } |
112 #endif // defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | 112 #endif // defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) |
113 | 113 |
114 } // namespace | 114 } // namespace |
115 | 115 |
116 namespace test { | 116 namespace test { |
117 | 117 |
118 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { | 118 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { |
119 // Constructor does nothing in all cases. See Init() for initialization. | 119 // Constructor does nothing in all cases. See Init() for initialization. |
(...skipping 12 matching lines...) Expand all Loading... |
132 DeleteStandaloneDataFile(); | 132 DeleteStandaloneDataFile(); |
133 #endif // defined(CLD2_DYNAMIC_MODE) | 133 #endif // defined(CLD2_DYNAMIC_MODE) |
134 } | 134 } |
135 | 135 |
136 void ScopedCLDDynamicDataHarness::Init() { | 136 void ScopedCLDDynamicDataHarness::Init() { |
137 DLOG(INFO) << "Initializing CLD data harness"; | 137 DLOG(INFO) << "Initializing CLD data harness"; |
138 #if defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) | 138 #if defined(CLD2_DYNAMIC_MODE) && defined(CLD2_IS_COMPONENT) |
139 // Dynamic data mode is enabled and we are using the component updater. | 139 // Dynamic data mode is enabled and we are using the component updater. |
140 ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); | 140 ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); |
141 base::FilePath data_file; | 141 base::FilePath data_file; |
142 ASSERT_NO_FATAL_FAILURE(GetComponentDataFileDestination(&data_file)); | 142 GetComponentDataFileDestination(&data_file); |
143 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | 143 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
144 data_file); | 144 data_file); |
145 base::FilePath result = component_updater::GetLatestCldDataFile(); | 145 base::FilePath result = component_updater::GetLatestCldDataFile(); |
146 ASSERT_EQ(data_file, result); | 146 ASSERT_EQ(data_file, result); |
147 #elif defined(CLD2_DYNAMIC_MODE) | 147 #elif defined(CLD2_DYNAMIC_MODE) |
148 // Dynamic data mode is enabled and we are using a standalone file. | 148 // Dynamic data mode is enabled and we are using a standalone file. |
149 ASSERT_NO_FATAL_FAILURE(ClearStandaloneDataFileState()); | 149 ASSERT_NO_FATAL_FAILURE(ClearStandaloneDataFileState()); |
150 ASSERT_NO_FATAL_FAILURE(CopyStandaloneDataFile()); | 150 ASSERT_NO_FATAL_FAILURE(CopyStandaloneDataFile()); |
151 #endif // defined(CLD2_DYNAMIC_MODE) | 151 #endif // defined(CLD2_DYNAMIC_MODE) |
152 } | 152 } |
153 | 153 |
154 void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { | 154 void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { |
155 #if defined(CLD2_DYNAMIC_MODE) | 155 #if defined(CLD2_DYNAMIC_MODE) |
156 DLOG(INFO) << "Clearing CLD data file state"; | 156 DLOG(INFO) << "Clearing CLD data file state"; |
157 // This code must live within the class in order to gain "friend" access. | 157 // This code must live within the class in order to gain "friend" access. |
158 base::AutoLock lock(TranslateTabHelper::s_file_lock_.Get()); | 158 base::AutoLock lock(TranslateTabHelper::s_file_lock_.Get()); |
159 if (TranslateTabHelper::s_cached_file_) { | 159 if (TranslateTabHelper::s_cached_file_) { |
160 // Leaks any open handle, no way to avoid safely. | 160 // Leaks any open handle, no way to avoid safely. |
161 TranslateTabHelper::s_cached_file_ = NULL; | 161 TranslateTabHelper::s_cached_file_ = NULL; |
162 TranslateTabHelper::s_cached_data_offset_ = 0; | 162 TranslateTabHelper::s_cached_data_offset_ = 0; |
163 TranslateTabHelper::s_cached_data_length_ = 0; | 163 TranslateTabHelper::s_cached_data_length_ = 0; |
164 } | 164 } |
165 #endif // defined(CLD2_DYNAMIC_MODE) | 165 #endif // defined(CLD2_DYNAMIC_MODE) |
166 } | 166 } |
167 | 167 |
168 } // namespace test | 168 } // namespace test |
OLD | NEW |