| 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 #ifndef CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_ | |
| 6 #define CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/macros.h" | |
| 12 | |
| 13 namespace test { | |
| 14 | |
| 15 // A utility class that sets up CLD dynamic data upon calling Init() and cleans | |
| 16 // it up when destroyed. Note that the Init() method will also configure the | |
| 17 // CLD data source using translate::CldDataSource::Set(), overriding any | |
| 18 // previously-set value unconditionally! | |
| 19 // | |
| 20 // Test data lives under: src/chrome/test/data/cld2_component | |
| 21 // | |
| 22 // This class is intended to be instantiated within IN_PROC_BROWSER_TEST_F | |
| 23 // test fixtures; it uses ASSERT macros for correctness, so that tests will | |
| 24 // fail gracefully in error conditions. Test code should generally use a | |
| 25 // CldDataHarnessFactory to create CldDataHarness objects since this allows | |
| 26 // the tests to run with whatever configuration is appropriate for the platform; | |
| 27 // If that's not enough power, the testing code can set the factory itself. | |
| 28 // | |
| 29 // Sample usage: | |
| 30 // | |
| 31 // IN_PROC_BROWSER_TEST_F(BrowserTest, PageLanguageDetection) { | |
| 32 // std::unique_ptr<test::CldDataHarness> cld_data_scope = | |
| 33 // test::CldDataHarnessFactory::Get()->CreateCldDataHarness(); | |
| 34 // ASSERT_NO_FATAL_FAILURE(cld_data_scope->Init()); | |
| 35 // // ... your code that depends on language detection goes here | |
| 36 // } | |
| 37 // | |
| 38 // If you have a lot of tests that need language translation features, you can | |
| 39 // add an instance of the CldDataHarness to your test class' private | |
| 40 // member variables and add the call to Init() into SetUpOnMainThread. | |
| 41 // Sample use: | |
| 42 // | |
| 43 // class MyTestClass : public InProcessBrowserTest { | |
| 44 // public: | |
| 45 // MyTestClass() : | |
| 46 // cld_data_scope( | |
| 47 // test::CldDataHarnessFactory::Get->CreateCldDataHarness()) { | |
| 48 // // (your additional setup code here) | |
| 49 // } | |
| 50 // void SetUpOnMainThread() override { | |
| 51 // cld_data_scope->Init(); | |
| 52 // InProcessBrowserTest::SetUpOnMainThread(); | |
| 53 // } | |
| 54 // private: | |
| 55 // std::unique_ptr<test::CldDataHarness> cld_data_scope; | |
| 56 // }; | |
| 57 // | |
| 58 class CldDataHarness { | |
| 59 public: | |
| 60 CldDataHarness() {} | |
| 61 | |
| 62 // Reverses the work done by the Init() method: any files and/or directories | |
| 63 // that would be created by Init() (whether it was called or not) are | |
| 64 // immediately deleted. | |
| 65 // If dynamic data is not currently available for any reason, this method has | |
| 66 // no effect. | |
| 67 // The default implementation does nothing. | |
| 68 virtual ~CldDataHarness() {} | |
| 69 | |
| 70 // Call this method, wrapping it in ASSERT_NO_FATAL_FAILURE, to initialize | |
| 71 // the harness and trigger test failure if initialization fails. | |
| 72 // IMPORTANT: This method will unconditionally set the CLD data source using | |
| 73 // translate::CldDataSource::Set(...). Any previously-configured CLD data | |
| 74 // source will be lost. This helps ensure a consistent test environment where | |
| 75 // the configured data source matches definitely matches the harness. | |
| 76 virtual void Init() {} | |
| 77 | |
| 78 // Create and return a new instance of a data harness whose Init() method | |
| 79 // will configure the "static" CldDataSource. | |
| 80 static std::unique_ptr<CldDataHarness> CreateStaticDataHarness(); | |
| 81 | |
| 82 // Create and return a new instance of a data harness whose Init() method | |
| 83 // will configure the "standalone" CldDataSource. | |
| 84 // Unlike NONE() and STATIC(), this data hardness will perform work to allow | |
| 85 // CLD to load data from a file. | |
| 86 static std::unique_ptr<CldDataHarness> CreateStandaloneDataHarness(); | |
| 87 | |
| 88 // Create and return a new instance of a data harness whose Init() method | |
| 89 // will configure the "component" CldDataSource. | |
| 90 // Unlike NONE() and STATIC(), this data hardness will perform work to allow | |
| 91 // CLD to load data from a file. | |
| 92 static std::unique_ptr<CldDataHarness> CreateComponentDataHarness(); | |
| 93 | |
| 94 protected: | |
| 95 // Returns the version number of the Component Updater "extension" in the | |
| 96 // test directory. This generally corresponds the the revision of CLD2 that | |
| 97 // the data was built from. The version number is also part of the path that | |
| 98 // would be present at runtime if the component installer was used as the | |
| 99 // CLD2 data source. | |
| 100 const base::FilePath::CharType* GetTestDataSourceCrxVersion(); | |
| 101 | |
| 102 // Returns the path to the Component Updater "extension" files in the test | |
| 103 // directory. Within, there is a real copy of the CLD2 dynamic data that can | |
| 104 // be used in testing scenarios without accessing the network. | |
| 105 void GetTestDataSourceDirectory(base::FilePath* out_path); | |
| 106 | |
| 107 private: | |
| 108 DISALLOW_COPY_AND_ASSIGN(CldDataHarness); | |
| 109 }; | |
| 110 | |
| 111 } // namespace test | |
| 112 | |
| 113 #endif // CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_ | |
| OLD | NEW |