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