Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/translate/translate_browser_test_utils.h" | |
| 6 | |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/platform_file.h" | |
| 11 #include "chrome/browser/component_updater/cld_component_installer.h" | |
| 12 #include "chrome/common/chrome_constants.h" | |
| 13 #include "chrome/common/chrome_paths.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // This constant yields the version of the CRX that has been extracted into | |
|
Sorin Jianu
2014/05/15 17:16:13
Inside a namespace, the code is indented starting
Andrew Hayden (chromium.org)
2014/05/16 17:53:20
I actually thought we just didn't indent it at all
| |
| 18 // the test data directory, and must be kept in sync with what is there. | |
| 19 // A reciprocal comment has been placed in | |
| 20 // chrome/test/data/cld2_component/README.chromium; don't update one without | |
| 21 // updating the other. | |
| 22 const base::FilePath::CharType kCrxVersion[] = FILE_PATH_LITERAL("160"); | |
| 23 | |
| 24 base::FilePath GetTestDataSourceDirectory() { | |
| 25 base::FilePath result; | |
| 26 DCHECK(PathService::Get(chrome::DIR_TEST_DATA, &result)); | |
| 27 return result | |
| 28 .Append(FILE_PATH_LITERAL("cld2_component")) | |
| 29 .Append(kCrxVersion); | |
| 30 } | |
| 31 | |
| 32 base::FilePath GetStandaloneFileSource() { | |
| 33 return GetTestDataSourceDirectory() | |
| 34 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 35 .Append(FILE_PATH_LITERAL("all")) | |
| 36 .Append(chrome::kCLDDataFilename); | |
| 37 } | |
| 38 | |
| 39 base::FilePath GetStandaloneDataFileDestination() { | |
| 40 base::FilePath result; | |
| 41 DCHECK(PathService::Get(chrome::DIR_USER_DATA, &result)); | |
| 42 return result.Append(chrome::kCLDDataFilename); | |
| 43 } | |
| 44 | |
| 45 base::FilePath GetExtractedComponentDestination() { | |
| 46 base::FilePath result; | |
| 47 DCHECK(PathService::Get(chrome::DIR_COMPONENT_CLD2, &result)); | |
| 48 return result; | |
| 49 } | |
| 50 | |
| 51 base::FilePath GetComponentDataFileDestination() { | |
| 52 return GetExtractedComponentDestination() | |
| 53 .Append(kCrxVersion) | |
| 54 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 55 .Append(FILE_PATH_LITERAL("all")) | |
| 56 .Append(chrome::kCLDDataFilename); | |
| 57 } | |
| 58 | |
| 59 void DeleteStandaloneFile() { | |
| 60 base::DeleteFile(GetStandaloneDataFileDestination(), false); | |
| 61 } | |
| 62 void CopyStandaloneFile() { | |
| 63 DeleteStandaloneFile(); // sanity: blow away any old copies | |
| 64 const base::FilePath target_file = GetStandaloneDataFileDestination(); | |
| 65 const base::FilePath target_dir = target_file.DirName(); | |
| 66 DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL)); | |
| 67 DCHECK(base::CopyFile(GetStandaloneFileSource(), target_file)); | |
| 68 DCHECK(base::PathExists(target_file)); | |
| 69 } | |
| 70 | |
| 71 void DeleteComponentTree() { | |
| 72 base::DeleteFile(GetExtractedComponentDestination(), true); | |
| 73 } | |
| 74 | |
| 75 void CopyComponentTree() { | |
| 76 DeleteComponentTree(); // sanity: blow away any old copies | |
| 77 const base::FilePath target_dir = GetExtractedComponentDestination(); | |
| 78 const base::FilePath source_dir = GetTestDataSourceDirectory(); | |
| 79 DCHECK(base::CreateDirectoryAndGetError(target_dir, NULL)); | |
| 80 DCHECK(base::CopyDirectory(source_dir, target_dir, true)); | |
| 81 DCHECK(base::PathExists(target_dir)); | |
| 82 DCHECK(base::PathExists(GetComponentDataFileDestination())); | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 87 namespace test { | |
| 88 | |
| 89 ScopedCLDDynamicDataHarness::ScopedCLDDynamicDataHarness() { | |
| 90 MakeDynamicCLDDataAvailableForTest(); | |
| 91 } | |
| 92 | |
| 93 ScopedCLDDynamicDataHarness::~ScopedCLDDynamicDataHarness() { | |
| 94 MakeDynamicCLDDataUnavailableForTest(); | |
| 95 } | |
| 96 | |
| 97 #if CLD_VERSION==1 || !defined(CLD2_DYNAMIC_MODE) | |
| 98 | |
| 99 // No dynamic data capabilities; methods do nothing. | |
| 100 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() {} | |
| 101 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() {} | |
| 102 | |
| 103 #elif defined(CLD2_IS_COMPONENT) | |
| 104 | |
| 105 // Dynamic data mode is enabled and we are using the component updater | |
| 106 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() { | |
| 107 CopyComponentTree(); | |
| 108 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | |
| 109 GetComponentDataFileDestination()); | |
| 110 } | |
| 111 | |
| 112 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() { | |
| 113 component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( | |
| 114 base::FilePath()); | |
| 115 DeleteComponentTree(); | |
| 116 } | |
| 117 | |
| 118 #else | |
| 119 | |
| 120 // Dynamic data mode is enabled and we are using a standalone file | |
| 121 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataAvailableForTest() { | |
| 122 TranslateTabHelper::ClearCLDDataForTest; | |
| 123 CopyStandaloneFile(); | |
| 124 } | |
| 125 | |
| 126 void ScopedCLDDynamicDataHarness::MakeDynamicCLDDataUnavailableForTest() { | |
| 127 TranslateTabHelper::ClearCLDDataForTest; | |
| 128 DeleteStandaloneFile(); | |
| 129 } | |
| 130 | |
| 131 #endif | |
| 132 | |
| 133 } // namespace test | |
| OLD | NEW |