Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1409)

Side by Side Diff: chrome/browser/translate/translate_browser_test_utils.cc

Issue 285293004: Allow browser tests to run with dynamic CLD data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cld_uma
Patch Set: Format Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
17 namespace {
18
19 // This constant yields the version of the CRX that has been extracted into
20 // the test data directory, and must be kept in sync with what is there.
21 // A reciprocal comment has been placed in
22 // chrome/test/data/cld2_component/README.chromium; don't update one without
23 // updating the other.
24 const base::FilePath::CharType kCrxVersion[] = FILE_PATH_LITERAL("160");
25
26 base::FilePath GetTestDataSourceDirectory() {
27 base::FilePath result;
28 DCHECK(PathService::Get(chrome::DIR_TEST_DATA, &result));
Paweł Hajdan Jr. 2014/05/19 15:06:56 Please don't use DCHECK for code with side-effects
29 return result.Append(FILE_PATH_LITERAL("cld2_component")).Append(kCrxVersion);
30 }
31
32 base::FilePath GetStandaloneFileSource() {
Takashi Toyoshima 2014/05/19 14:32:24 GetStandaloneDataFileSource() is consistent with t
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Standardized on "DataFile" wherever I was using "*
33 return GetTestDataSourceDirectory()
34 .Append(FILE_PATH_LITERAL("_platform_specific"))
35 .Append(FILE_PATH_LITERAL("all"))
36 .Append(chrome::kCLDDataFilename);
37 }
38
39 base::FilePath GetStandaloneDataFileDestination() {
40 base::FilePath result;
41 DCHECK(PathService::Get(chrome::DIR_USER_DATA, &result));
Takashi Toyoshima 2014/05/19 14:32:24 Just a question to check my understanding. The dat
Paweł Hajdan Jr. 2014/05/19 15:06:56 Yes.
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Asked Pawel to confirm this. I've added comments t
42 return result.Append(chrome::kCLDDataFilename);
43 }
44
45 base::FilePath GetExtractedComponentDestination() {
46 base::FilePath result;
47 DCHECK(PathService::Get(chrome::DIR_COMPONENT_CLD2, &result));
Takashi Toyoshima 2014/05/19 14:32:24 Same question. Actually I'm not familiar with wher
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Yes, and I've added a comment to make this clear.
48 return result;
49 }
50
51 base::FilePath GetComponentDataFileDestination() {
52 return GetExtractedComponentDestination()
53 .Append(kCrxVersion)
54 .Append(FILE_PATH_LITERAL("_platform_specific"))
55 .Append(FILE_PATH_LITERAL("all"))
56 .Append(chrome::kCLDDataFilename);
57 }
58
59 void DeleteStandaloneFile() {
Takashi Toyoshima 2014/05/19 14:32:24 DeleteStandaloneDataFile() is consistent with othe
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Standardized all use of "*File" to "DataFile"
60 base::DeleteFile(GetStandaloneDataFileDestination(), false);
61 }
Takashi Toyoshima 2014/05/19 14:32:24 empty line between line 61 and 62.
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Done.
62 void CopyStandaloneFile() {
Takashi Toyoshima 2014/05/19 14:32:24 CopyStandaloneDataFile()? It you feel naming funct
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Made consistent.
63 DeleteStandaloneFile(); // sanity: blow away any old copies
64 const base::FilePath target_file = GetStandaloneDataFileDestination();
65 const base::FilePath target_dir = target_file.DirName();
66 DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL));
67 DCHECK(base::CopyFile(GetStandaloneFileSource(), target_file));
68 DCHECK(base::PathExists(target_file));
69 }
70
71 void DeleteComponentTree() {
72 base::DeleteFile(GetExtractedComponentDestination(), true);
73 }
74
75 void CopyComponentTree() {
76 DeleteComponentTree(); // sanity: blow away any old copies
77 const base::FilePath target_dir = GetExtractedComponentDestination();
78 const base::FilePath source_dir = GetTestDataSourceDirectory();
79 DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL));
80 DCHECK(base::CopyDirectory(source_dir, target_dir, true));
81 DCHECK(base::PathExists(target_dir));
82 DCHECK(base::PathExists(GetComponentDataFileDestination()));
83 }
84
85 } // namespace
86
87 namespace test {
88
89 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() {
90 MakeDynamicCLDDataAvailableForTest();
91 }
92
93 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() {
94 MakeDynamicCLDDataUnavailableForTest();
95 }
96
97 #if defined(CLD2_DYNAMIC_MODE)
98 void ScopedCLDDynamicDataHarness::ClearStandaloneFileState() {
99 base::AutoLock lock(TranslateTabHelper::s_file_lock_.Get());
100 if (TranslateTabHelper::s_cached_file_) {
101 // Leaks any open handle, no way to avoid safely.
102 TranslateTabHelper::s_cached_file_ = NULL;
103 TranslateTabHelper::s_cached_data_offset_ = 0;
104 TranslateTabHelper::s_cached_data_length_ = 0;
105 }
106 }
107 #else
108 void ScopedCLDDynamicDataHarness::ClearStandaloneFileState() {
Takashi Toyoshima 2014/05/19 14:32:24 If we move ClearStandaloneFileState() to anonymous
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 Can't be done because it accesses "friend"-only da
Takashi Toyoshima 2014/05/19 16:14:20 Oh, sorry I missed it. Agreed.
109 }
110 #endif
111
112 #if (CLD_VERSION == 1) || !defined(CLD2_DYNAMIC_MODE)
113
114 // No dynamic data capabilities; methods do nothing.
115 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() {
116 }
117 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() {
118 }
119
120 #elif defined(CLD2_IS_COMPONENT)
121
122 // Dynamic data mode is enabled and we are using the component updater
123 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() {
124 CopyComponentTree();
125 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
126 GetComponentDataFileDestination());
127 }
128
129 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() {
130 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
131 base::FilePath());
132 DeleteComponentTree();
133 }
134
135 #else
Takashi Toyoshima 2014/05/19 14:32:24 This #if/#elif/#else conditions seem to need a com
Andrew Hayden (chromium.org) 2014/05/19 15:14:21 I've got comments right inside the #ifdefs. I thin
136
137 // Dynamic data mode is enabled and we are using a standalone file
138 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() {
139 ClearStandaloneFileState();
140 CopyStandaloneFile();
141 }
142
143 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() {
144 ClearStandaloneFileState();
145 DeleteStandaloneFile();
146 }
147
148 #endif
149
150 } // namespace test
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698