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.
5 #include "chrome/browser/translate/component_cld_data_harness.h"
7 #include "base/base_paths.h"
8 #include "base/files/file_util.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "chrome/browser/component_updater/cld_component_installer.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "components/component_updater/component_updater_paths.h"
14 #include "components/translate/content/common/cld_data_source.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 // This has to match what's in cld_component_installer.cc.
20 const base::FilePath::CharType kComponentDataFileName
[] =
21 FILE_PATH_LITERAL("cld2_data.bin");
27 ComponentCldDataHarness::~ComponentCldDataHarness() {
28 DVLOG(1) << "Tearing down CLD data harness";
29 // Dynamic data mode is enabled and we are using the component updater.
30 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
32 ClearComponentDataFileState();
33 DeleteComponentTree();
36 void ComponentCldDataHarness::Init() {
37 DVLOG(1) << "Initializing CLD data harness";
38 // Dynamic data mode is enabled and we are using the component updater.
39 translate::CldDataSource::Set(
40 translate::CldDataSource::GetComponentDataSource());
41 ASSERT_NO_FATAL_FAILURE(CopyComponentTree());
42 base::FilePath data_file
;
43 GetComponentDataFileDestination(&data_file
);
44 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
48 void ComponentCldDataHarness::ClearComponentDataFileState() {
49 DVLOG(1) << "Clearing component CLD data file state";
50 base::FilePath nothing
;
51 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(nothing
);
54 // DIR_COMPONENT_CLD2 is also defined as being relative to USER_DATA_DIR, so
55 // like GetStandaloneDataFileDestination, this is safe to run in multiple
56 // parallel test processes.
57 void ComponentCldDataHarness::GetExtractedComponentDestination(
58 base::FilePath
* out_path
) {
59 ASSERT_TRUE(PathService::Get(component_updater::DIR_COMPONENT_CLD2
,
63 void ComponentCldDataHarness::GetComponentDataFileDestination(
64 base::FilePath
* out_path
) {
65 GetExtractedComponentDestination(out_path
);
66 *out_path
= out_path
->Append(CldDataHarness::GetTestDataSourceCrxVersion())
67 .Append(FILE_PATH_LITERAL("_platform_specific"))
68 .Append(FILE_PATH_LITERAL("all"))
69 .Append(kComponentDataFileName
);
72 void ComponentCldDataHarness::DeleteComponentTree() {
73 base::FilePath tree_path
;
74 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path
));
75 DVLOG(1) << "Deleting CLD component test files from " << tree_path
.value();
76 base::DeleteFile(tree_path
, true);
79 void ComponentCldDataHarness::CopyComponentTree() {
80 DeleteComponentTree(); // sanity: blow away any old copies.
81 base::FilePath target_dir
;
82 GetExtractedComponentDestination(&target_dir
);
83 base::FilePath source_dir
;
84 CldDataHarness::GetTestDataSourceDirectory(&source_dir
);
85 DVLOG(1) << "Copying CLD component test files from " << source_dir
.value()
86 << " to " << target_dir
.value();
87 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir
, NULL
));
88 ASSERT_TRUE(base::CopyDirectory(source_dir
, target_dir
, true));
89 ASSERT_TRUE(base::PathExists(target_dir
));
90 base::FilePath check_path
;
91 GetComponentDataFileDestination(&check_path
);
92 ASSERT_TRUE(base::PathExists(check_path
));