component_cld_data_harness.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "component_cld_data_harness.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/base_paths.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/file_util.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/logging.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/path_service.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/component_updater/cld_component_installer.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/common/chrome_paths.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "testing/gtest/include/gtest/gtest.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This has to match what's in cld_component_installer.cc.
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst base::FilePath::CharType kComponentDataFileName[] =
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    FILE_PATH_LITERAL("cld2_data.bin");
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace test {
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
25116680a4aac90f2aa7413d9095a592090648e557Ben MurdochComponentCldDataHarness::ComponentCldDataHarness() {
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Constructor does nothing in all cases. See Init() for initialization.
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
29116680a4aac90f2aa7413d9095a592090648e557Ben MurdochComponentCldDataHarness::~ComponentCldDataHarness() {
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  VLOG(1) << "Tearing down CLD data harness";
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Dynamic data mode is enabled and we are using the component updater.
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::FilePath());
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ClearComponentDataFileState();
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DeleteComponentTree();
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ComponentCldDataHarness::Init() {
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  VLOG(1) << "Initializing CLD data harness";
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Dynamic data mode is enabled and we are using the component updater.
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_NO_FATAL_FAILURE(CopyComponentTree());
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath data_file;
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GetComponentDataFileDestination(&data_file);
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      data_file);
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ComponentCldDataHarness::ClearComponentDataFileState() {
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  VLOG(1) << "Clearing component CLD data file state";
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath nothing;
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(nothing);
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// DIR_COMPONENT_CLD2 is also defined as being relative to USER_DATA_DIR, so
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// like GetStandaloneDataFileDestination, this is safe to run in multiple
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// parallel test processes.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ComponentCldDataHarness::GetExtractedComponentDestination(
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    base::FilePath* out_path) {
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path));
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ComponentCldDataHarness::GetComponentDataFileDestination(
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    base::FilePath* out_path) {
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GetExtractedComponentDestination(out_path);
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  *out_path = out_path->Append(CldDataHarness::GetTestDataSourceCrxVersion())
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  .Append(FILE_PATH_LITERAL("_platform_specific"))
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  .Append(FILE_PATH_LITERAL("all"))
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  .Append(kComponentDataFileName);
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ComponentCldDataHarness::DeleteComponentTree() {
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath tree_path;
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path));
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  VLOG(1) << "Deleting CLD component test files from " << tree_path.value();
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::DeleteFile(tree_path, true);
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ComponentCldDataHarness::CopyComponentTree() {
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DeleteComponentTree();  // sanity: blow away any old copies.
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath target_dir;
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GetExtractedComponentDestination(&target_dir);
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath source_dir;
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CldDataHarness::GetTestDataSourceDirectory(&source_dir);
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  VLOG(1) << "Copying CLD component test files from " << source_dir.value()
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          << " to " << target_dir.value();
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL));
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true));
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(base::PathExists(target_dir));
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath check_path;
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GetComponentDataFileDestination(&check_path);
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(base::PathExists(check_path));
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdochscoped_ptr<CldDataHarness> CreateCldDataHarness() {
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<CldDataHarness> result(new ComponentCldDataHarness());
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return result.Pass();
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace test
100