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