Chromium Code Reviews| Index: chrome/browser/translate/scoped_cld_standalone_data_harness.cc |
| diff --git a/chrome/browser/translate/scoped_cld_standalone_data_harness.cc b/chrome/browser/translate/scoped_cld_standalone_data_harness.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a8aa219c8c82e6a144503554a8855ed114d4ceee |
| --- /dev/null |
| +++ b/chrome/browser/translate/scoped_cld_standalone_data_harness.cc |
| @@ -0,0 +1,84 @@ |
| +// 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 "scoped_cld_standalone_data_harness.h" |
| + |
| +#include "base/base_paths.h" |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/path_service.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| + |
| +namespace { |
| + |
| +// This comes from chrome_translate_client.cc |
| +const base::FilePath::CharType kStandaloneDataFileName[] = |
| + FILE_PATH_LITERAL("cld2_data.bin"); |
| + |
| +} // namespace |
| + |
| +namespace test { |
| + |
| +ScopedCldStandaloneDataHarness::ScopedCldStandaloneDataHarness() { |
| + // Constructor does nothing in all cases. See Init() for initialization. |
| +} |
| + |
| +ScopedCldStandaloneDataHarness::~ScopedCldStandaloneDataHarness() { |
| + VLOG(1) << "Tearing down CLD data harness"; |
| + DeleteStandaloneDataFile(); |
| +} |
| + |
| +void ScopedCldStandaloneDataHarness::Init() { |
| + VLOG(1) << "Initializing CLD data harness"; |
| + // Dynamic data mode is enabled and we are using a standalone file. |
| + ASSERT_NO_FATAL_FAILURE(CopyStandaloneDataFile()); |
| +} |
| + |
| +void ScopedCldStandaloneDataHarness::GetStandaloneDataFileSource( |
| + base::FilePath* out_path) { |
| + ScopedCldDataHarness::GetTestDataSourceDirectory(out_path); |
| + *out_path = out_path->Append(FILE_PATH_LITERAL("_platform_specific")) |
| + .Append(FILE_PATH_LITERAL("all")) |
| + .Append(kStandaloneDataFileName); |
| +} |
| + |
| +// 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 ScopedCldStandaloneDataHarness::GetStandaloneDataFileDestination( |
| + base::FilePath* out_path) { |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, out_path)); |
| + *out_path = out_path->Append(kStandaloneDataFileName); |
| +} |
| + |
| +void ScopedCldStandaloneDataHarness::DeleteStandaloneDataFile() { |
| + base::FilePath path; |
| + ASSERT_NO_FATAL_FAILURE(GetStandaloneDataFileDestination(&path)); |
| + VLOG(1) << "Deleting CLD test data file from " << path.value(); |
| + base::DeleteFile(path, false); |
| +} |
| + |
| +void ScopedCldStandaloneDataHarness::CopyStandaloneDataFile() { |
| + DeleteStandaloneDataFile(); // sanity: blow away any old copies. |
| + base::FilePath target_file; |
| + GetStandaloneDataFileDestination(&target_file); |
| + base::FilePath target_dir = target_file.DirName(); |
| + ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); |
| + base::FilePath source_file; |
| + GetStandaloneDataFileSource(&source_file); |
| + VLOG(1) << "Copying CLD test data file from " << source_file.value() |
| + << " to " << target_file.value(); |
|
Takashi Toyoshima
2014/06/23 08:50:01
The first '<<' should be aligned with the first '<
Andrew Hayden (chromium.org)
2014/06/23 13:20:35
Sorry, this is due to refactoring DLOG(INFO) -> VL
|
| + ASSERT_TRUE(base::CopyFile(source_file, target_file)); |
| + ASSERT_TRUE(base::PathExists(target_file)); |
| +} |
| + |
| +scoped_ptr<ScopedCldDataHarness> |
|
Takashi Toyoshima
2014/06/23 08:50:01
no line break here
Andrew Hayden (chromium.org)
2014/06/23 13:20:36
Done.
|
| + CreateScopedCldDataHarness() { |
| + scoped_ptr<ScopedCldDataHarness> result(new ScopedCldStandaloneDataHarness()); |
| + return result.Pass(); |
| +} |
| + |
| +} // namespace test |