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