| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "component_cld_data_harness.h" | 5 #include "component_cld_data_harness.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "chrome/browser/component_updater/cld_component_installer.h" | 11 #include "chrome/browser/component_updater/cld_component_installer.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // This has to match what's in cld_component_installer.cc. | 17 // This has to match what's in cld_component_installer.cc. |
| 18 const base::FilePath::CharType kComponentDataFileName[] = | 18 const base::FilePath::CharType kComponentDataFileName[] = |
| 19 FILE_PATH_LITERAL("cld2_data.bin"); | 19 FILE_PATH_LITERAL("cld2_data.bin"); |
| 20 | 20 |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 namespace test { | 23 namespace test { |
| 24 | 24 |
| 25 ComponentCldDataHarness::ComponentCldDataHarness() { | 25 ComponentCldDataHarness::ComponentCldDataHarness() { |
| 26 // Constructor does nothing in all cases. See Init() for initialization. | 26 // Constructor does nothing in all cases. See Init() for initialization. |
| 27 } | 27 } |
| 28 | 28 |
| 29 ComponentCldDataHarness::~ComponentCldDataHarness() { | 29 ComponentCldDataHarness::~ComponentCldDataHarness() { |
| 30 VLOG(1) << "Tearing down CLD data harness"; | 30 DVLOG(1) << "Tearing down CLD data harness"; |
| 31 // Dynamic data mode is enabled and we are using the component updater. | 31 // Dynamic data mode is enabled and we are using the component updater. |
| 32 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | 32 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
| 33 base::FilePath()); | 33 base::FilePath()); |
| 34 ClearComponentDataFileState(); | 34 ClearComponentDataFileState(); |
| 35 DeleteComponentTree(); | 35 DeleteComponentTree(); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void ComponentCldDataHarness::Init() { | 38 void ComponentCldDataHarness::Init() { |
| 39 VLOG(1) << "Initializing CLD data harness"; | 39 DVLOG(1) << "Initializing CLD data harness"; |
| 40 // Dynamic data mode is enabled and we are using the component updater. | 40 // Dynamic data mode is enabled and we are using the component updater. |
| 41 ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); | 41 ASSERT_NO_FATAL_FAILURE(CopyComponentTree()); |
| 42 base::FilePath data_file; | 42 base::FilePath data_file; |
| 43 GetComponentDataFileDestination(&data_file); | 43 GetComponentDataFileDestination(&data_file); |
| 44 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | 44 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( |
| 45 data_file); | 45 data_file); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void ComponentCldDataHarness::ClearComponentDataFileState() { | 48 void ComponentCldDataHarness::ClearComponentDataFileState() { |
| 49 VLOG(1) << "Clearing component CLD data file state"; | 49 DVLOG(1) << "Clearing component CLD data file state"; |
| 50 base::FilePath nothing; | 50 base::FilePath nothing; |
| 51 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(nothing); | 51 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile(nothing); |
| 52 } | 52 } |
| 53 | 53 |
| 54 // DIR_COMPONENT_CLD2 is also defined as being relative to USER_DATA_DIR, so | 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 | 55 // like GetStandaloneDataFileDestination, this is safe to run in multiple |
| 56 // parallel test processes. | 56 // parallel test processes. |
| 57 void ComponentCldDataHarness::GetExtractedComponentDestination( | 57 void ComponentCldDataHarness::GetExtractedComponentDestination( |
| 58 base::FilePath* out_path) { | 58 base::FilePath* out_path) { |
| 59 ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); | 59 ASSERT_TRUE(PathService::Get(chrome::DIR_COMPONENT_CLD2, out_path)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void ComponentCldDataHarness::GetComponentDataFileDestination( | 62 void ComponentCldDataHarness::GetComponentDataFileDestination( |
| 63 base::FilePath* out_path) { | 63 base::FilePath* out_path) { |
| 64 GetExtractedComponentDestination(out_path); | 64 GetExtractedComponentDestination(out_path); |
| 65 *out_path = out_path->Append(CldDataHarness::GetTestDataSourceCrxVersion()) | 65 *out_path = out_path->Append(CldDataHarness::GetTestDataSourceCrxVersion()) |
| 66 .Append(FILE_PATH_LITERAL("_platform_specific")) | 66 .Append(FILE_PATH_LITERAL("_platform_specific")) |
| 67 .Append(FILE_PATH_LITERAL("all")) | 67 .Append(FILE_PATH_LITERAL("all")) |
| 68 .Append(kComponentDataFileName); | 68 .Append(kComponentDataFileName); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void ComponentCldDataHarness::DeleteComponentTree() { | 71 void ComponentCldDataHarness::DeleteComponentTree() { |
| 72 base::FilePath tree_path; | 72 base::FilePath tree_path; |
| 73 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); | 73 ASSERT_NO_FATAL_FAILURE(GetExtractedComponentDestination(&tree_path)); |
| 74 VLOG(1) << "Deleting CLD component test files from " << tree_path.value(); | 74 DVLOG(1) << "Deleting CLD component test files from " << tree_path.value(); |
| 75 base::DeleteFile(tree_path, true); | 75 base::DeleteFile(tree_path, true); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void ComponentCldDataHarness::CopyComponentTree() { | 78 void ComponentCldDataHarness::CopyComponentTree() { |
| 79 DeleteComponentTree(); // sanity: blow away any old copies. | 79 DeleteComponentTree(); // sanity: blow away any old copies. |
| 80 base::FilePath target_dir; | 80 base::FilePath target_dir; |
| 81 GetExtractedComponentDestination(&target_dir); | 81 GetExtractedComponentDestination(&target_dir); |
| 82 base::FilePath source_dir; | 82 base::FilePath source_dir; |
| 83 CldDataHarness::GetTestDataSourceDirectory(&source_dir); | 83 CldDataHarness::GetTestDataSourceDirectory(&source_dir); |
| 84 VLOG(1) << "Copying CLD component test files from " << source_dir.value() | 84 DVLOG(1) << "Copying CLD component test files from " << source_dir.value() |
| 85 << " to " << target_dir.value(); | 85 << " to " << target_dir.value(); |
| 86 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); | 86 ASSERT_TRUE(base::CreateDirectoryAndGetError(target_dir, NULL)); |
| 87 ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); | 87 ASSERT_TRUE(base::CopyDirectory(source_dir, target_dir, true)); |
| 88 ASSERT_TRUE(base::PathExists(target_dir)); | 88 ASSERT_TRUE(base::PathExists(target_dir)); |
| 89 base::FilePath check_path; | 89 base::FilePath check_path; |
| 90 GetComponentDataFileDestination(&check_path); | 90 GetComponentDataFileDestination(&check_path); |
| 91 ASSERT_TRUE(base::PathExists(check_path)); | 91 ASSERT_TRUE(base::PathExists(check_path)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 scoped_ptr<CldDataHarness> CreateCldDataHarness() { | 94 scoped_ptr<CldDataHarness> CreateCldDataHarness() { |
| 95 scoped_ptr<CldDataHarness> result(new ComponentCldDataHarness()); | 95 scoped_ptr<CldDataHarness> result(new ComponentCldDataHarness()); |
| 96 return result.Pass(); | 96 return result.Pass(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 } // namespace test | 99 } // namespace test |
| OLD | NEW |