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..ae7c14415b4208155ff4f42766db29016f416ad9 |
--- /dev/null |
+++ b/chrome/browser/translate/translate_browser_test_utils.cc |
@@ -0,0 +1,133 @@ |
+// 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 "chrome/browser/component_updater/cld_component_installer.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 |
Sorin Jianu
2014/05/15 17:16:13
Inside a namespace, the code is indented starting
Andrew Hayden (chromium.org)
2014/05/16 17:53:20
I actually thought we just didn't indent it at all
|
+ // 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 GetStandaloneFileSource() { |
+ return GetTestDataSourceDirectory() |
+ .Append(FILE_PATH_LITERAL("_platform_specific")) |
+ .Append(FILE_PATH_LITERAL("all")) |
+ .Append(chrome::kCLDDataFilename); |
+ } |
+ |
+ base::FilePath GetStandaloneDataFileDestination() { |
+ base::FilePath result; |
+ DCHECK(PathService::Get(chrome::DIR_USER_DATA, &result)); |
+ return result.Append(chrome::kCLDDataFilename); |
+ } |
+ |
+ 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 DeleteStandaloneFile() { |
+ base::DeleteFile(GetStandaloneDataFileDestination(), false); |
+ } |
+ void CopyStandaloneFile() { |
+ DeleteStandaloneFile(); // sanity: blow away any old copies |
+ const base::FilePath target_file = GetStandaloneDataFileDestination(); |
+ const base::FilePath target_dir = target_file.DirName(); |
+ DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL)); |
+ DCHECK(base::CopyFile(GetStandaloneFileSource(), target_file)); |
+ DCHECK(base::PathExists(target_file)); |
+ } |
+ |
+ void DeleteComponentTree() { |
+ base::DeleteFile(GetExtractedComponentDestination(), true); |
+ } |
+ |
+ void CopyComponentTree() { |
+ DeleteComponentTree(); // sanity: blow away any old copies |
+ 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 { |
+ |
+ ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { |
+ MakeDynamicCLDDataAvailableForTest(); |
+ } |
+ |
+ ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
+ MakeDynamicCLDDataUnavailableForTest(); |
+ } |
+ |
+#if CLD_VERSION==1 || !defined(CLD2_DYNAMIC_MODE) |
+ |
+ // No dynamic data capabilities; methods do nothing. |
+ void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() {} |
+ void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() {} |
+ |
+#elif defined(CLD2_IS_COMPONENT) |
+ |
+ // Dynamic data mode is enabled and we are using the component updater |
+ void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() { |
+ CopyComponentTree(); |
+ component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
+ GetComponentDataFileDestination()); |
+ } |
+ |
+ void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() { |
+ component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
+ base::FilePath()); |
+ DeleteComponentTree(); |
+ } |
+ |
+#else |
+ |
+ // Dynamic data mode is enabled and we are using a standalone file |
+ void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() { |
+ TranslateTabHelper::ClearCLDDataForTest; |
+ CopyStandaloneFile(); |
+ } |
+ |
+ void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() { |
+ TranslateTabHelper::ClearCLDDataForTest; |
+ DeleteStandaloneFile(); |
+ } |
+ |
+#endif |
+ |
+} // namespace test |