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

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: toyoshim@'s comments 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));
29 return result.Append(FILE_PATH_LITERAL("cld2_component")).Append(kCrxVersion);
30 }
31
32 base::FilePath GetStandaloneDataFileSource() {
33 return GetTestDataSourceDirectory()
34 .Append(FILE_PATH_LITERAL("_platform_specific"))
35 .Append(FILE_PATH_LITERAL("all"))
36 .Append(chrome::kCLDDataFilename);
37 }
38
39 // Using the USER_DATA_DIR not only mimics true functionality, but also is
40 // important to test isolation. Each test gets its own USER_DATA_DIR, which
41 // ensures proper isolation between test processes running in parallel.
42 base::FilePath GetStandaloneDataFileDestination() {
43 base::FilePath result;
44 DCHECK(PathService::Get(chrome::DIR_USER_DATA, &result));
45 return result.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 base::FilePath GetExtractedComponentDestination() {
52 base::FilePath result;
53 DCHECK(PathService::Get(chrome::DIR_COMPONENT_CLD2, &result));
54 return result;
55 }
56
57 base::FilePath GetComponentDataFileDestination() {
58 return GetExtractedComponentDestination()
59 .Append(kCrxVersion)
60 .Append(FILE_PATH_LITERAL("_platform_specific"))
61 .Append(FILE_PATH_LITERAL("all"))
62 .Append(chrome::kCLDDataFilename);
63 }
64
65 void DeleteStandaloneDataFile() {
66 base::DeleteFile(GetStandaloneDataFileDestination(), false);
67 }
68
69 void CopyStandaloneDaTaFile() {
70 DeleteStandaloneDataFile(); // sanity: blow away any old copies
Sorin Jianu 2014/05/19 17:41:23 '.' at the end.
Andrew Hayden (chromium.org) 2014/05/19 20:54:01 Done.
71 const base::FilePath target_file = GetStandaloneDataFileDestination();
72 const base::FilePath target_dir = target_file.DirName();
73 DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL));
74 DCHECK(base::CopyFile(GetStandaloneDataFileSource(), target_file));
75 DCHECK(base::PathExists(target_file));
76 }
77
78 void DeleteComponentTree() {
79 base::DeleteFile(GetExtractedComponentDestination(), true);
80 }
81
82 void CopyComponentTree() {
83 DeleteComponentTree(); // sanity: blow away any old copies
Sorin Jianu 2014/05/19 17:41:23 Same as above.
Andrew Hayden (chromium.org) 2014/05/19 20:54:01 Done.
84 const base::FilePath target_dir = GetExtractedComponentDestination();
85 const base::FilePath source_dir = GetTestDataSourceDirectory();
86 DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL));
87 DCHECK(base::CopyDirectory(source_dir, target_dir, true));
88 DCHECK(base::PathExists(target_dir));
89 DCHECK(base::PathExists(GetComponentDataFileDestination()));
90 }
91
92 } // namespace
93
94 namespace test {
95
96 #if defined(CLD2_DYNAMIC_MODE)
97 void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() {
98 // This code must live within the class in order to gain "friend" access.
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::ClearStandaloneDataFileState() {
109 }
110 #endif
111
112 #if (CLD_VERSION == 1) || !defined(CLD2_DYNAMIC_MODE)
113
114 // No dynamic data capabilities; methods do nothing.
115 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() {
116 }
117 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() {
118 }
119
120 #elif defined(CLD2_IS_COMPONENT)
121
122 // Dynamic data mode is enabled and we are using the component updater
Sorin Jianu 2014/05/19 17:41:23 .
Andrew Hayden (chromium.org) 2014/05/19 20:54:01 Done.
123 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() {
124 CopyComponentTree();
125 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
126 GetComponentDataFileDestination());
127 }
128
129 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() {
130 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
131 base::FilePath());
132 DeleteComponentTree();
133 }
134
135 #else
136
137 // Dynamic data mode is enabled and we are using a standalone file
138 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() {
139 ClearStandaloneDataFileState();
140 CopyStandaloneDataFile();
141 }
142
143 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() {
144 ClearStandaloneDataFileState();
145 DeleteStandaloneDataFile();
146 }
147
148 #endif
149
150 } // namespace test
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698