Home | History | Annotate | Download | only in translate
      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.
      4 
      5 #include "component_cld_data_harness.h"
      6 
      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 "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace {
     16 
     17 // This has to match what's in cld_component_installer.cc.
     18 const base::FilePath::CharType kComponentDataFileName[] =
     19     FILE_PATH_LITERAL("cld2_data.bin");
     20 
     21 }  // namespace
     22 
     23 namespace test {
     24 
     25 ComponentCldDataHarness::ComponentCldDataHarness() {
     26   // Constructor does nothing in all cases. See Init() for initialization.
     27 }
     28 
     29 ComponentCldDataHarness::~ComponentCldDataHarness() {
     30   VLOG(1) << "Tearing down CLD data harness";
     31   // Dynamic data mode is enabled and we are using the component updater.
     32   component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
     33       base::FilePath());
     34   ClearComponentDataFileState();
     35   DeleteComponentTree();
     36 }
     37 
     38 void ComponentCldDataHarness::Init() {
     39   VLOG(1) << "Initializing CLD data harness";
     40   // Dynamic data mode is enabled and we are using the component updater.
     41   ASSERT_NO_FATAL_FAILURE(CopyComponentTree());
     42   base::FilePath data_file;
     43   GetComponentDataFileDestination(&data_file);
     44   component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(
     45       data_file);
     46 }
     47 
     48 void ComponentCldDataHarness::ClearComponentDataFileState() {
     49   VLOG(1) << "Clearing component CLD data file state";
     50   base::FilePath nothing;
     51   component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(nothing);
     52 }
     53 
     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(chrome::DIR_COMPONENT_CLD2, out_path));
     60 }
     61 
     62 void ComponentCldDataHarness::GetComponentDataFileDestination(
     63     base::FilePath* out_path) {
     64   GetExtractedComponentDestination(out_path);
     65   *out_path = out_path->Append(CldDataHarness::GetTestDataSourceCrxVersion())
     66                   .Append(FILE_PATH_LITERAL("_platform_specific"))
     67                   .Append(FILE_PATH_LITERAL("all"))
     68                   .Append(kComponentDataFileName);
     69 }
     70 
     71 void ComponentCldDataHarness::DeleteComponentTree() {
     72   base::FilePath tree_path;
     73   ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path));
     74   VLOG(1) << "Deleting CLD component test files from " << tree_path.value();
     75   base::DeleteFile(tree_path, true);
     76 }
     77 
     78 void ComponentCldDataHarness::CopyComponentTree() {
     79   DeleteComponentTree();  // sanity: blow away any old copies.
     80   base::FilePath target_dir;
     81   GetExtractedComponentDestination(&target_dir);
     82   base::FilePath source_dir;
     83   CldDataHarness::GetTestDataSourceDirectory(&source_dir);
     84   VLOG(1) << "Copying CLD component test files from " << source_dir.value()
     85           << " to " << target_dir.value();
     86   ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL));
     87   ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true));
     88   ASSERT_TRUE(base::PathExists(target_dir));
     89   base::FilePath check_path;
     90   GetComponentDataFileDestination(&check_path);
     91   ASSERT_TRUE(base::PathExists(check_path));
     92 }
     93 
     94 scoped_ptr<CldDataHarness> CreateCldDataHarness() {
     95   scoped_ptr<CldDataHarness> result(new ComponentCldDataHarness());
     96   return result.Pass();
     97 }
     98 
     99 }  // namespace test
    100