Chromium Code Reviews| 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/path_service.h" | |
| 10 #include "base/platform_file.h" | |
| 11 #include "base/synchronization/lock.h" | |
| 12 #include "chrome/browser/component_updater/cld_component_installer.h" | |
| 13 #include "chrome/browser/translate/translate_tab_helper.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 const base::FilePath::CharType kCrxVersion[] = FILE_PATH_LITERAL("160"); | |
| 26 | |
| 27 void GetTestDataSourceDirectory(base::FilePath* out_path) { | |
| 28 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, out_path)); | |
| 29 *out_path = out_path->Append(FILE_PATH_LITERAL("cld2_component")) | |
| 30 .Append(kCrxVersion); | |
| 31 } | |
| 32 | |
| 33 void GetStandaloneDataFileSource(base::FilePath* out_path) { | |
| 34 ASSERT_NO_FATAL_FAILURE(GetTestDataSourceDirectory(out_path)); | |
|
Sorin Jianu
2014/05/19 18:16:29
Are these the macros from gunit? If yes, I wonder
Andrew Hayden (chromium.org)
2014/05/19 20:54:02
Yes. But EXPECT allows execution to continue, wher
| |
| 35 *out_path = out_path->Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 36 .Append(FILE_PATH_LITERAL("all")) | |
| 37 .Append(chrome::kCLDDataFilename); | |
| 38 } | |
| 39 | |
| 40 // Using the USER_DATA_DIR not only mimics true functionality, but also is | |
| 41 // important to test isolation. Each test gets its own USER_DATA_DIR, which | |
| 42 // ensures proper isolation between test processes running in parallel. | |
| 43 void GetStandaloneDataFileDestination(base::FilePath* out_path) { | |
| 44 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, out_path)); | |
| 45 *out_path = out_path->Append(chrome::kCLDDataFilename); | |
| 46 } | |
| 47 | |
| 48 // DIR_COMPONENT_CLD2 is also defined as being relative to USER_DATA_DIR, so | |
| 49 // like GetStandaloneDataFileDestination, this is safe to run in multiple | |
| 50 // parallel test processes. | |
| 51 void GetExtractedComponentDestination(base::FilePath* out_path) { | |
| 52 ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); | |
| 53 } | |
| 54 | |
| 55 void GetComponentDataFileDestination(base::FilePath* out_path) { | |
| 56 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(out_path)); | |
| 57 *out_path = out_path->Append(kCrxVersion) | |
| 58 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 59 .Append(FILE_PATH_LITERAL("all")) | |
| 60 .Append(chrome::kCLDDataFilename); | |
| 61 } | |
| 62 | |
| 63 void DeleteStandaloneDataFile() { | |
| 64 base::FilePath path; | |
| 65 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&path)); | |
| 66 base::DeleteFile(path, false); | |
| 67 } | |
| 68 | |
| 69 void CopyStandaloneDaTaFile() { | |
|
msw
2014/05/19 18:54:48
nit: Data
Andrew Hayden (chromium.org)
2014/05/19 20:54:02
I compiled with no dynamic mode and with component
| |
| 70 DeleteStandaloneDataFile(); // sanity: blow away any old copies. | |
| 71 base::FilePath target_file; | |
| 72 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&target_file)); | |
| 73 base::FilePath target_dir = target_file.DirName(); | |
| 74 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | |
| 75 base::FilePath source_file; | |
| 76 ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileSource(&source_file)); | |
| 77 ASSERT_TRUE(base::CopyFile(source_file, target_file)); | |
| 78 ASSERT_TRUE(base::PathExists(target_file)); | |
| 79 } | |
| 80 | |
| 81 void DeleteComponentTree() { | |
| 82 base::FilePath tree_path; | |
| 83 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); | |
| 84 base::DeleteFile(tree_path, true); | |
| 85 } | |
| 86 | |
| 87 void CopyComponentTree() { | |
| 88 DeleteComponentTree(); // sanity: blow away any old copies. | |
| 89 base::FilePath target_dir; | |
| 90 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&target_dir)); | |
| 91 base::FilePath source_dir; | |
| 92 ASSERT_NO_FATAL_FAILURE(GetTestDataSourceDirectory(&source_dir)); | |
| 93 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | |
| 94 ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); | |
| 95 ASSERT_TRUE(base::PathExists(target_dir)); | |
| 96 base::FilePath check_path; | |
| 97 ASSERT_NO_FATAL_FAILURE(GetComponentDataFileDestination(&check_path)); | |
| 98 ASSERT_TRUE(base::PathExists(check_path)); | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 namespace test { | |
| 104 | |
| 105 #if defined(CLD2_DYNAMIC_MODE) | |
|
msw
2014/05/19 18:54:48
nit: just put all the code within the function def
Andrew Hayden (chromium.org)
2014/05/19 20:54:02
Done.
| |
| 106 void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { | |
| 107 // This code must live within the class in order to gain "friend" access. | |
| 108 base::AutoLock lock(TranslateTabHelper::s_file_lock_.Get()); | |
| 109 if (TranslateTabHelper::s_cached_file_) { | |
| 110 // Leaks any open handle, no way to avoid safely. | |
| 111 TranslateTabHelper::s_cached_file_ = NULL; | |
| 112 TranslateTabHelper::s_cached_data_offset_ = 0; | |
| 113 TranslateTabHelper::s_cached_data_length_ = 0; | |
| 114 } | |
| 115 } | |
| 116 #else | |
| 117 void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { | |
| 118 } | |
| 119 #endif | |
| 120 | |
| 121 // Constructor does nothing in all cases. See #ifdefs for Init() and dtor. | |
| 122 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { | |
| 123 } | |
| 124 | |
| 125 #if (CLD_VERSION == 1) || !defined(CLD2_DYNAMIC_MODE) | |
|
msw
2014/05/19 18:54:48
nit: again, I think it'd be clearer to move the pr
Andrew Hayden (chromium.org)
2014/05/19 20:54:02
Done.
| |
| 126 | |
| 127 // No dynamic data capabilities. | |
| 128 void ScopedCLDDynamicDataHarness::Init() { | |
| 129 } | |
| 130 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { | |
| 131 } | |
| 132 | |
| 133 #elif defined(CLD2_IS_COMPONENT) | |
| 134 | |
| 135 // Dynamic data mode is enabled and we are using the component updater. | |
| 136 void ScopedCLDDynamicDataHarness::Init() { | |
| 137 ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); | |
| 138 base::FilePath data_file; | |
| 139 ASSERT_NO_FATAL_FAILURE(GetComponentDataFileDestination(&data_file)); | |
| 140 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | |
| 141 data_file); | |
| 142 base::FilePath result = component_updater::GetLatestCldDataFile(); | |
| 143 ASSERT_EQ(data_file, result); | |
| 144 } | |
| 145 | |
| 146 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { | |
| 147 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | |
| 148 base::FilePath()); | |
| 149 DeleteComponentTree(); | |
| 150 } | |
| 151 | |
| 152 #else | |
| 153 | |
| 154 // Dynamic data mode is enabled and we are using a standalone file. | |
| 155 void ScopedCLDDynamicDataHarness::Init() { | |
| 156 ASSERT_NO_FATAL_FAILURE(ClearStandaloneDataFileState()); | |
| 157 ASSERT_NO_FATAL_FAILURE(CopyStandaloneDataFile()); | |
| 158 } | |
| 159 | |
| 160 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { | |
| 161 ClearStandaloneDataFileState(); | |
| 162 DeleteStandaloneDataFile(); | |
| 163 } | |
| 164 | |
| 165 #endif | |
| 166 | |
| 167 } // namespace test | |
| OLD | NEW |