Chromium Code Reviews| 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..4072bbc1f154db7569b53db3737bf1a58a57d63c |
| --- /dev/null |
| +++ b/chrome/browser/translate/translate_browser_test_utils.cc |
| @@ -0,0 +1,167 @@ |
| +// 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" |
| +#include "testing/gtest/include/gtest/gtest.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"); |
| + |
| +void GetTestDataSourceDirectory(base::FilePath* out_path) { |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, out_path)); |
| + *out_path = out_path->Append(FILE_PATH_LITERAL("cld2_component")) |
| + .Append(kCrxVersion); |
| +} |
| + |
| +void GetStandaloneDataFileSource(base::FilePath* out_path) { |
| + 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
|
| + *out_path = out_path->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. |
| +void GetStandaloneDataFileDestination(base::FilePath* out_path) { |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, out_path)); |
| + *out_path = out_path->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. |
| +void GetExtractedComponentDestination(base::FilePath* out_path) { |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); |
| +} |
| + |
| +void GetComponentDataFileDestination(base::FilePath* out_path) { |
| + ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(out_path)); |
| + *out_path = out_path->Append(kCrxVersion) |
| + .Append(FILE_PATH_LITERAL("_platform_specific")) |
| + .Append(FILE_PATH_LITERAL("all")) |
| + .Append(chrome::kCLDDataFilename); |
| +} |
| + |
| +void DeleteStandaloneDataFile() { |
| + base::FilePath path; |
| + ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&path)); |
| + base::DeleteFile(path, false); |
| +} |
| + |
| +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
|
| + DeleteStandaloneDataFile(); // sanity: blow away any old copies. |
| + base::FilePath target_file; |
| + ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&target_file)); |
| + base::FilePath target_dir = target_file.DirName(); |
| + ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); |
| + base::FilePath source_file; |
| + ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileSource(&source_file)); |
| + ASSERT_TRUE(base::CopyFile(source_file, target_file)); |
| + ASSERT_TRUE(base::PathExists(target_file)); |
| +} |
| + |
| +void DeleteComponentTree() { |
| + base::FilePath tree_path; |
| + ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); |
| + base::DeleteFile(tree_path, true); |
| +} |
| + |
| +void CopyComponentTree() { |
| + DeleteComponentTree(); // sanity: blow away any old copies. |
| + base::FilePath target_dir; |
| + ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&target_dir)); |
| + base::FilePath source_dir; |
| + ASSERT_NO_FATAL_FAILURE(GetTestDataSourceDirectory(&source_dir)); |
| + ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); |
| + ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); |
| + ASSERT_TRUE(base::PathExists(target_dir)); |
| + base::FilePath check_path; |
| + ASSERT_NO_FATAL_FAILURE(GetComponentDataFileDestination(&check_path)); |
| + ASSERT_TRUE(base::PathExists(check_path)); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace test { |
| + |
| +#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.
|
| +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 |
| + |
| +// Constructor does nothing in all cases. See #ifdefs for Init() and dtor. |
| +ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { |
| +} |
| + |
| +#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.
|
| + |
| +// No dynamic data capabilities. |
| +void ScopedCLDDynamicDataHarness::Init() { |
| +} |
| +ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
| +} |
| + |
| +#elif defined(CLD2_IS_COMPONENT) |
| + |
| +// Dynamic data mode is enabled and we are using the component updater. |
| +void ScopedCLDDynamicDataHarness::Init() { |
| + ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); |
| + base::FilePath data_file; |
| + ASSERT_NO_FATAL_FAILURE(GetComponentDataFileDestination(&data_file)); |
| + component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
| + data_file); |
| + base::FilePath result = component_updater::GetLatestCldDataFile(); |
| + ASSERT_EQ(data_file, result); |
| +} |
| + |
| +ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
| + component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
| + base::FilePath()); |
| + DeleteComponentTree(); |
| +} |
| + |
| +#else |
| + |
| +// Dynamic data mode is enabled and we are using a standalone file. |
| +void ScopedCLDDynamicDataHarness::Init() { |
| + ASSERT_NO_FATAL_FAILURE(ClearStandaloneDataFileState()); |
| + ASSERT_NO_FATAL_FAILURE(CopyStandaloneDataFile()); |
| +} |
| + |
| +ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { |
| + ClearStandaloneDataFileState(); |
| + DeleteStandaloneDataFile(); |
| +} |
| + |
| +#endif |
| + |
| +} // namespace test |