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: |
49 // Reverses the work done by the Init() method: any files and/or directories | 59 // 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 | 60 // that would be created by Init() (whether it was called or not) are |
51 // immediately deleted. | 61 // immediately deleted. |
52 // If dynamic data is not currently available for any reason, this method has | 62 // If dynamic data is not currently available for any reason, this method has |
53 // no effect. | 63 // no effect. |
64 // The default implementation does nothing. | |
54 virtual ~CldDataHarness() {} | 65 virtual ~CldDataHarness() {} |
55 | 66 |
56 // Call this method, wrapping it in ASSERT_NO_FATAL_FAILURE, to initialize | 67 // Call this method, wrapping it in ASSERT_NO_FATAL_FAILURE, to initialize |
57 // the harness and trigger test failure if initialization fails. | 68 // the harness and trigger test failure if initialization fails. |
58 virtual void Init() = 0; | 69 // IMPORTANT: This method will unconditionally set the CLD data source using |
70 // translate::CldDataSource::Set(...). Any previously-configured CLD data | |
71 // source will be lost. This helps ensure a consistent test environment where | |
72 // the configured data source matches definitely matches the harness. | |
73 virtual void Init() {}; | |
74 | |
75 // Create and return a new instance of a data harness whose Init() method | |
76 // will configure the "none" CldDataSource. | |
77 static scoped_ptr<CldDataHarness> NONE(); | |
Takashi Toyoshima
2014/10/15 08:45:07
This kind of name conversion looks non-standard of
Andrew Hayden (chromium.org)
2014/10/30 16:56:31
This has been greatly cleaned up. I think you'll f
| |
78 | |
79 // Create and return a new instance of a data harness whose Init() method | |
80 // will configure the "static" CldDataSource. | |
81 static scoped_ptr<CldDataHarness> STATIC(); | |
82 | |
83 // Create and return a new instance of a data harness whose Init() method | |
84 // will configure the "standalone" CldDataSource. | |
85 // Unlike NONE() and STATIC(), this data hardness will perform work to allow | |
86 // CLD to load data from a file. | |
87 static scoped_ptr<CldDataHarness> STANDALONE(); | |
88 | |
89 // Create and return a new instance of a data harness whose Init() method | |
90 // will configure the "component" CldDataSource. | |
91 // Unlike NONE() and STATIC(), this data hardness will perform work to allow | |
92 // CLD to load data from a file. | |
93 static scoped_ptr<CldDataHarness> COMPONENT(); | |
59 | 94 |
60 protected: | 95 protected: |
61 // Returns the version number of the Component Updater "extension" in the | 96 // Returns the version number of the Component Updater "extension" in the |
62 // test directory. This generally corresponds the the revision of CLD2 that | 97 // 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 | 98 // 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 | 99 // would be present at runtime if the component installer was used as the |
65 // CLD2 data source. | 100 // CLD2 data source. |
66 const base::FilePath::CharType* GetTestDataSourceCrxVersion(); | 101 const base::FilePath::CharType* GetTestDataSourceCrxVersion(); |
67 | 102 |
68 // Returns the path to the Component Updater "extension" files in the test | 103 // 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 | 104 // directory. Within, there is a real copy of the CLD2 dynamic data that can |
70 // be used in testing scenarios without accessing the network. | 105 // be used in testing scenarios without accessing the network. |
71 void GetTestDataSourceDirectory(base::FilePath* out_path); | 106 void GetTestDataSourceDirectory(base::FilePath* out_path); |
72 }; | 107 }; |
73 | 108 |
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 | 109 } // namespace test |
79 | 110 |
80 #endif // CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_ | 111 #endif // CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_ |
OLD | NEW |