OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | |
6 #define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/memory/scoped_vector.h" | |
13 #include "base/string16.h" | |
14 #include "chrome/test/base/in_process_browser_test.h" | |
15 #include "content/public/test/js_injection_ready_observer.h" | |
16 | |
17 namespace base { | |
18 class Value; | |
19 } | |
20 | |
21 namespace content { | |
22 class RenderViewHost; | |
23 class WebUI; | |
24 class WebUIMessageHandler; | |
25 } | |
26 | |
27 class WebUITestHandler; | |
28 | |
29 // This macro simplifies the declaration of simple javascript unit tests. | |
30 // Use: | |
31 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); | |
32 #define WEB_UI_UNITTEST_F(x, y) \ | |
33 IN_PROC_BROWSER_TEST_F(x, y) { \ | |
34 ASSERT_TRUE(RunJavascriptTest(#y)); \ | |
35 } | |
36 | |
37 // The runner of WebUI javascript based tests. | |
38 // See chrome/test/data/webui/test_api.js for the javascript side test API's. | |
39 // | |
40 // These tests should follow the form given in: | |
41 // chrome/test/data/webui/sample_downloads.js. | |
42 // and the lone test within this class. | |
43 class WebUIBrowserTest | |
44 : public InProcessBrowserTest, | |
45 public content::JsInjectionReadyObserver { | |
46 public: | |
47 typedef ScopedVector<const base::Value> ConstValueVector; | |
48 virtual ~WebUIBrowserTest(); | |
49 | |
50 // Add a custom helper JS library for your test. | |
51 // If a relative path is specified, it'll be read | |
52 // as relative to the test data dir. | |
53 void AddLibrary(const FilePath& library_path); | |
54 | |
55 // Runs a javascript function in the context of all libraries. | |
56 // Note that calls to functions in test_api.js are not supported. | |
57 // Takes ownership of Value* arguments. | |
58 bool RunJavascriptFunction(const std::string& function_name); | |
59 bool RunJavascriptFunction(const std::string& function_name, | |
60 base::Value* arg); | |
61 bool RunJavascriptFunction(const std::string& function_name, | |
62 base::Value* arg1, | |
63 base::Value* arg2); | |
64 bool RunJavascriptFunction(const std::string& function_name, | |
65 const ConstValueVector& function_arguments); | |
66 | |
67 // Runs a test fixture that may include calls to functions in test_api.js. | |
68 bool RunJavascriptTestF(bool is_async, | |
69 const std::string& test_fixture, | |
70 const std::string& test_name); | |
71 | |
72 // Runs a test that may include calls to functions in test_api.js. | |
73 // Takes ownership of Value* arguments. | |
74 bool RunJavascriptTest(const std::string& test_name); | |
75 bool RunJavascriptTest(const std::string& test_name, | |
76 base::Value* arg); | |
77 bool RunJavascriptTest(const std::string& test_name, | |
78 base::Value* arg1, | |
79 base::Value* arg2); | |
80 bool RunJavascriptTest(const std::string& test_name, | |
81 const ConstValueVector& test_arguments); | |
82 | |
83 // Runs a test that may include calls to functions in test_api.js, and waits | |
84 // for call to testDone(). Takes ownership of Value* arguments. | |
85 bool RunJavascriptAsyncTest(const std::string& test_name); | |
86 bool RunJavascriptAsyncTest(const std::string& test_name, | |
87 base::Value* arg); | |
88 bool RunJavascriptAsyncTest(const std::string& test_name, | |
89 base::Value* arg1, | |
90 base::Value* arg2); | |
91 bool RunJavascriptAsyncTest(const std::string& test_name, | |
92 base::Value* arg1, | |
93 base::Value* arg2, | |
94 base::Value* arg3); | |
95 bool RunJavascriptAsyncTest(const std::string& test_name, | |
96 const ConstValueVector& test_arguments); | |
97 | |
98 // Sends message through |preload_host| to preload javascript libraries and | |
99 // sets the |libraries_preloaded| flag to prevent re-loading at next | |
100 // javascript invocation. | |
101 void PreLoadJavascriptLibraries(const std::string& preload_test_fixture, | |
102 const std::string& preload_test_name, | |
103 content::RenderViewHost* preload_host); | |
104 | |
105 // Called by javascript-generated test bodies to browse to a page and preload | |
106 // the javascript for the given |preload_test_fixture| and | |
107 // |preload_test_name|. chrome.send will be overridden to allow javascript | |
108 // handler mocking. | |
109 void BrowsePreload(const GURL& browse_to); | |
110 | |
111 // Called by javascript-generated test bodies to browse to a page and preload | |
112 // the javascript for the given |preload_test_fixture| and | |
113 // |preload_test_name|. chrome.send will be overridden to allow javascript | |
114 // handler mocking. | |
115 void BrowsePrintPreload(const GURL& browse_to); | |
116 | |
117 protected: | |
118 // URL to dummy WebUI page for testing framework. | |
119 static const char kDummyURL[]; | |
120 | |
121 WebUIBrowserTest(); | |
122 | |
123 // Accessors for preload test fixture and name. | |
124 void set_preload_test_fixture(const std::string& preload_test_fixture); | |
125 void set_preload_test_name(const std::string& preload_test_name); | |
126 | |
127 // Set up & tear down console error catching. | |
128 virtual void SetUpOnMainThread() OVERRIDE; | |
129 virtual void CleanUpOnMainThread() OVERRIDE; | |
130 | |
131 // Set up test path & override for |kDummyURL|. | |
132 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | |
133 | |
134 // Tear down override for |kDummyURL|. | |
135 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; | |
136 | |
137 // Set a WebUI instance to run tests on. | |
138 void SetWebUIInstance(content::WebUI* web_ui); | |
139 | |
140 // Returns a mock WebUI object under test (if any). | |
141 virtual content::WebUIMessageHandler* GetMockMessageHandler(); | |
142 | |
143 // Returns a file:// GURL constructed from |path| inside the test data dir for | |
144 // webui tests. | |
145 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); | |
146 | |
147 private: | |
148 // content::JsInjectionReadyObserver implementation. | |
149 virtual void OnJsInjectionReady( | |
150 content::RenderViewHost* render_view_host) OVERRIDE; | |
151 | |
152 // Builds a string containing all added javascript libraries. | |
153 void BuildJavascriptLibraries(string16* content); | |
154 | |
155 // Builds a string with a call to the runTest JS function, passing the | |
156 // given |is_async|, |test_name| and its |args|. | |
157 string16 BuildRunTestJSCall(bool is_async, | |
158 const std::string& test_name, | |
159 const WebUIBrowserTest::ConstValueVector& args); | |
160 | |
161 // Loads all libraries added with AddLibrary(), and calls |function_name| with | |
162 // |function_arguments|. When |is_test| is true, the framework wraps | |
163 // |function_name| with a test helper function, which waits for completion, | |
164 // logging an error message on failure, otherwise |function_name| is called | |
165 // asynchronously. When |preload_host| is non-NULL, sends the javascript to | |
166 // the RenderView for evaluation at the appropriate time before the onload | |
167 // call is made. Passes |is_async| along to runTest wrapper. | |
168 bool RunJavascriptUsingHandler(const std::string& function_name, | |
169 const ConstValueVector& function_arguments, | |
170 bool is_test, | |
171 bool is_async, | |
172 content::RenderViewHost* preload_host); | |
173 | |
174 // Attaches mock and test handlers. | |
175 void SetupHandlers(); | |
176 | |
177 // Handles test framework messages. | |
178 scoped_ptr<WebUITestHandler> test_handler_; | |
179 | |
180 // Location of test data (currently test/data/webui). | |
181 FilePath test_data_directory_; | |
182 | |
183 // Location of generated test data (<(PROGRAM_DIR)/test_data). | |
184 FilePath gen_test_data_directory_; | |
185 | |
186 // User added libraries | |
187 std::vector<FilePath> user_libraries_; | |
188 | |
189 // Indicates that the libraries have been pre-loaded and to not load them | |
190 // again. | |
191 bool libraries_preloaded_; | |
192 | |
193 // Saves the states of |test_fixture| and |test_name| for calling | |
194 // PreloadJavascriptLibraries(). | |
195 std::string preload_test_fixture_; | |
196 std::string preload_test_name_; | |
197 | |
198 // When this is non-NULL, this is The WebUI instance used for testing. | |
199 // Otherwise the selected tab's web_ui is used. | |
200 content::WebUI* override_selected_web_ui_; | |
201 }; | |
202 | |
203 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | |
OLD | NEW |