Index: chrome/browser/translate/translate_browser_test_utils.cc |
diff --git a/chrome/browser/translate/translate_browser_test_utils.cc b/chrome/browser/translate/translate_browser_test_utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0c7a628b21140f2367258417a7c133c803cf7a32 |
--- /dev/null |
+++ b/chrome/browser/translate/translate_browser_test_utils.cc |
@@ -0,0 +1,150 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/translate/translate_browser_test_utils.h" |
+ |
+#include "base/base_paths.h" |
+#include "base/file_util.h" |
+#include "base/path_service.h" |
+#include "base/platform_file.h" |
+#include "base/synchronization/lock.h" |
+#include "chrome/browser/component_updater/cld_component_installer.h" |
+#include "chrome/browser/translate/translate_tab_helper.h" |
+#include "chrome/common/chrome_constants.h" |
+#include "chrome/common/chrome_paths.h" |
+ |
+namespace { |
+ |
+// This constant yields the version of the CRX that has been extracted into |
+// the test data directory, and must be kept in sync with what is there. |
+// A reciprocal comment has been placed in |
+// chrome/test/data/cld2_component/README.chromium; don't update one without |
+// updating the other. |
+const base::FilePath::CharType kCrxVersion[] = FILE_PATH_LITERAL("160"); |
+ |
+base::FilePath GetTestDataSourceDirectory() { |
+ base::FilePath result; |
+ DCHECK(PathService::Get(chrome::DIR_TEST_DATA, &result)); |
+ return result.Append(FILE_PATH_LITERAL("cld2_component")).Append(kCrxVersion); |
+} |
+ |
+base::FilePath GetStandaloneDataFileSource() { |
+ return GetTestDataSourceDirectory() |
+ .Append(FILE_PATH_LITERAL("_platform_specific")) |
+ .Append(FILE_PATH_LITERAL("all")) |
+ .Append(chrome::kCLDDataFilename); |
+} |
+ |
+// Using the USER_DATA_DIR not only mimics true functionality, but also is |
+// important to test isolation. Each test gets its own USER_DATA_DIR, which |
+// ensures proper isolation between test processes running in parallel. |
+base::FilePath GetStandaloneDataFileDestination() { |
+ base::FilePath result; |
+ DCHECK(PathService::Get(chrome::DIR_USER_DATA, &result)); |
+ return result.Append(chrome::kCLDDataFilename); |
+} |
+ |
+// DIR_COMPONENT_CLD2 is also defined as being relative to USER_DATA_DIR, so |
+// like GetStandaloneDataFileDestination, this is safe to run in multiple |
+// parallel test processes. |
+base::FilePath GetExtractedComponentDestination() { |
+ base::FilePath result; |
+ DCHECK(PathService::Get(chrome::DIR_COMPONENT_CLD2, &result)); |
+ return result; |
+} |
+ |
+base::FilePath GetComponentDataFileDestination() { |
+ return GetExtractedComponentDestination() |
+ .Append(kCrxVersion) |
+ .Append(FILE_PATH_LITERAL("_platform_specific")) |
+ .Append(FILE_PATH_LITERAL("all")) |
+ .Append(chrome::kCLDDataFilename); |
+} |
+ |
+void DeleteStandaloneDataFile() { |
+ base::DeleteFile(GetStandaloneDataFileDestination(), false); |
+} |
+ |
+void CopyStandaloneDaTaFile() { |
+ 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.
|
+ const base::FilePath target_file = GetStandaloneDataFileDestination(); |
+ const base::FilePath target_dir = target_file.DirName(); |
+ DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL)); |
+ DCHECK(base::CopyFile(GetStandaloneDataFileSource(), target_file)); |
+ DCHECK(base::PathExists(target_file)); |
+} |
+ |
+void DeleteComponentTree() { |
+ base::DeleteFile(GetExtractedComponentDestination(), true); |
+} |
+ |
+void CopyComponentTree() { |
+ 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.
|
+ const base::FilePath target_dir = GetExtractedComponentDestination(); |
+ const base::FilePath source_dir = GetTestDataSourceDirectory(); |
+ DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL)); |
+ DCHECK(base::CopyDirectory(source_dir, target_dir, true)); |
+ DCHECK(base::PathExists(target_dir)); |
+ DCHECK(base::PathExists(GetComponentDataFileDestination())); |
+} |
+ |
+} // namespace |
+ |
+namespace test { |
+ |
+#if defined(CLD2_DYNAMIC_MODE) |
+void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { |
+ // This code must live within the class in order to gain "friend" access. |
+ base::AutoLock lock(TranslateTabHelper::s_file_lock_.Get()); |
+ if (TranslateTabHelper::s_cached_file_) { |
+ // Leaks any open handle, no way to avoid safely. |
+ TranslateTabHelper::s_cached_file_ = NULL; |
+ TranslateTabHelper::s_cached_data_offset_ = 0; |
+ TranslateTabHelper::s_cached_data_length_ = 0; |
+ } |
+} |
+#else |
+void ScopedCLDDynamicDataHarness::ClearStandaloneDataFileState() { |
+} |
+#endif |
+ |
+#if (CLD_VERSION == 1) || !defined(CLD2_DYNAMIC_MODE) |
+ |
+// No dynamic data capabilities; methods do nothing. |
+ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { |
+} |
+ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
+} |
+ |
+#elif defined(CLD2_IS_COMPONENT) |
+ |
+// 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.
|
+ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { |
+ CopyComponentTree(); |
+ component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
+ GetComponentDataFileDestination()); |
+} |
+ |
+ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
+ component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
+ base::FilePath()); |
+ DeleteComponentTree(); |
+} |
+ |
+#else |
+ |
+// Dynamic data mode is enabled and we are using a standalone file |
+ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { |
+ ClearStandaloneDataFileState(); |
+ CopyStandaloneDataFile(); |
+} |
+ |
+ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
+ ClearStandaloneDataFileState(); |
+ DeleteStandaloneDataFile(); |
+} |
+ |
+#endif |
+ |
+} // namespace test |