Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: chrome/browser/ui/webui/web_ui_browsertest.h

Issue 7861024: Adds testing infrastructure to web_ui_browsertest to support testing HtmlDialogs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_UI_WEBUI_WEB_UI_BROWSERTEST_H_ 5 #ifndef CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_
6 #define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ 6 #define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/string16.h" 12 #include "base/string16.h"
13 #include "chrome/browser/ui/webui/web_ui_test_handler.h" 13 #include "chrome/browser/ui/webui/web_ui_test_handler.h"
14 #include "chrome/test/base/in_process_browser_test.h" 14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/test_navigation_observer.h" 15 #include "chrome/test/test_navigation_observer.h"
16 16
17 class RenderViewHost; 17 class RenderViewHost;
18 class WebUIMessageHandler; 18 class WebUIMessageHandler;
19 19
20 namespace base { 20 namespace base {
21 class Value; 21 class Value;
22 } 22 }
23 23
24 // This macro simplifies the declaration of simple javascript unit tests. 24 // These macros simplify the declaration of simple javascript unit tests.
Sheridan Rawlins 2011/09/09 15:47:50 We shouldn't need any WEB_UI macros. These macros
flackr 2011/09/15 18:41:52 Done.
25 // Use: 25 // Use:
26 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); 26 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest);
27 // WEB_UI_UNITTEST_F1(MyWebUIPageTest, myJavascriptUnittest, myArg);
28 // WEB_UI_ASYNC_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest);
29 // WEB_UI_ASYNC_UNITTEST_F1(MyWebUIPageTest, myJavascriptUnittest, myArg);
27 #define WEB_UI_UNITTEST_F(x, y) \ 30 #define WEB_UI_UNITTEST_F(x, y) \
28 IN_PROC_BROWSER_TEST_F(x, y) { \ 31 IN_PROC_BROWSER_TEST_F(x, y) { \
29 ASSERT_TRUE(RunJavascriptTest(#y)); \ 32 ASSERT_TRUE(RunJavascriptTest(#y)); \
30 } 33 }
31 34
35 #define WEB_UI_UNITTEST_F1(x, y, z) \
36 IN_PROC_BROWSER_TEST_F(x, y) { \
37 ASSERT_TRUE(RunJavascriptTest(#y, z)); \
38 }
39
40 #define WEB_UI_ASYNC_UNITTEST_F(x, y) \
41 IN_PROC_BROWSER_TEST_F(x, y) { \
42 ASSERT_TRUE(RunJavascriptAsyncTest(#y)); \
43 }
44
45 #define WEB_UI_ASYNC_UNITTEST_F1(x, y, z) \
46 IN_PROC_BROWSER_TEST_F(x, y) { \
47 ASSERT_TRUE(RunJavascriptAsyncTest(#y, z)); \
48 }
49
32 // The runner of WebUI javascript based tests. 50 // The runner of WebUI javascript based tests.
33 // See chrome/test/data/webui/test_api.js for the javascript side test API's. 51 // See chrome/test/data/webui/test_api.js for the javascript side test API's.
34 // 52 //
35 // These tests should follow the form given in: 53 // These tests should follow the form given in:
36 // chrome/test/data/webui/sample_downloads.js. 54 // chrome/test/data/webui/sample_downloads.js.
37 // and the lone test within this class. 55 // and the lone test within this class.
38 class WebUIBrowserTest 56 class WebUIBrowserTest
39 : public InProcessBrowserTest, 57 : public InProcessBrowserTest,
40 public TestNavigationObserver::JsInjectionReadyObserver { 58 public TestNavigationObserver::JsInjectionReadyObserver,
59 public NotificationObserver {
41 public: 60 public:
42 typedef std::vector<const base::Value*> ConstValueVector; 61 typedef std::vector<const base::Value*> ConstValueVector;
43 virtual ~WebUIBrowserTest(); 62 virtual ~WebUIBrowserTest();
44 63
45 // Add a custom helper JS library for your test. 64 // Add a custom helper JS library for your test.
46 // If a relative path is specified, it'll be read 65 // If a relative path is specified, it'll be read
47 // as relative to the test data dir. 66 // as relative to the test data dir.
48 void AddLibrary(const FilePath& library_path); 67 void AddLibrary(const FilePath& library_path);
49 68
50 // Runs a javascript function in the context of all libraries. 69 // Runs a javascript function in the context of all libraries.
(...skipping 18 matching lines...) Expand all
69 bool RunJavascriptTest(const std::string& test_name); 88 bool RunJavascriptTest(const std::string& test_name);
70 bool RunJavascriptTest(const std::string& test_name, 89 bool RunJavascriptTest(const std::string& test_name,
71 base::Value* arg); 90 base::Value* arg);
72 bool RunJavascriptTest(const std::string& test_name, 91 bool RunJavascriptTest(const std::string& test_name,
73 base::Value* arg1, 92 base::Value* arg1,
74 base::Value* arg2); 93 base::Value* arg2);
75 bool RunJavascriptTest(const std::string& test_name, 94 bool RunJavascriptTest(const std::string& test_name,
76 const ConstValueVector& test_arguments); 95 const ConstValueVector& test_arguments);
77 96
78 // Runs a test that may include calls to functions in test_api.js, and waits 97 // Runs a test that may include calls to functions in test_api.js, and waits
79 // for call to asyncTestDone(). Takes ownership of Value arguments. 98 // for call to testDone(). Takes ownership of Value arguments.
80 bool RunJavascriptAsyncTest(const std::string& test_name); 99 bool RunJavascriptAsyncTest(const std::string& test_name);
81 bool RunJavascriptAsyncTest(const std::string& test_name, 100 bool RunJavascriptAsyncTest(const std::string& test_name,
82 base::Value* arg); 101 base::Value* arg);
83 bool RunJavascriptAsyncTest(const std::string& test_name, 102 bool RunJavascriptAsyncTest(const std::string& test_name,
84 base::Value* arg1, 103 base::Value* arg1,
85 base::Value* arg2); 104 base::Value* arg2);
86 bool RunJavascriptAsyncTest(const std::string& test_name, 105 bool RunJavascriptAsyncTest(const std::string& test_name,
87 base::Value* arg1, 106 base::Value* arg1,
88 base::Value* arg2, 107 base::Value* arg2,
89 base::Value* arg3); 108 base::Value* arg3);
(...skipping 28 matching lines...) Expand all
118 static const char kDummyURL[]; 137 static const char kDummyURL[];
119 138
120 WebUIBrowserTest(); 139 WebUIBrowserTest();
121 140
122 // Set up test path & override for |kDummyURL|. 141 // Set up test path & override for |kDummyURL|.
123 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; 142 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
124 143
125 // Tear down override for |kDummyURL|. 144 // Tear down override for |kDummyURL|.
126 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; 145 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE;
127 146
147 // Observes the next HtmlDialog to be created.
148 virtual void ObserveNextHtmlDialog();
149
150 // Wait for HtmlDialog to have loaded.
151 virtual void WaitForHtmlDialogLoad();
152
128 // Returns a mock WebUI object under test (if any). 153 // Returns a mock WebUI object under test (if any).
129 virtual WebUIMessageHandler* GetMockMessageHandler(); 154 virtual WebUIMessageHandler* GetMockMessageHandler();
130 155
131 // Returns a file:// GURL constructed from |path| inside the test data dir for 156 // Returns a file:// GURL constructed from |path| inside the test data dir for
132 // webui tests. 157 // webui tests.
133 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); 158 static GURL WebUITestDataPathToURL(const FilePath::StringType& path);
134 159
135 private: 160 private:
136 // TestNavigationObserver::JsInjectionReadyObserver implementation. 161 // TestNavigationObserver::JsInjectionReadyObserver implementation.
137 virtual void OnJsInjectionReady(RenderViewHost* render_view_host) OVERRIDE; 162 virtual void OnJsInjectionReady(RenderViewHost* render_view_host) OVERRIDE;
138 163
164 // NotificationObserver implementation.
165 virtual void Observe(int type,
166 const NotificationSource& source,
167 const NotificationDetails& details);
168
139 // Builds a string containing all added javascript libraries. 169 // Builds a string containing all added javascript libraries.
140 void BuildJavascriptLibraries(string16* content); 170 void BuildJavascriptLibraries(string16* content);
141 171
142 // Builds a string with a call to the runTest JS function, passing the 172 // Builds a string with a call to the runTest JS function, passing the
143 // given |is_async|, |test_name| and its |args|. 173 // given |is_async|, |test_name| and its |args|.
144 string16 BuildRunTestJSCall(bool is_async, 174 string16 BuildRunTestJSCall(bool is_async,
145 const std::string& test_name, 175 const std::string& test_name,
146 const WebUIBrowserTest::ConstValueVector& args); 176 const WebUIBrowserTest::ConstValueVector& args);
147 177
148 // Loads all libraries added with AddLibrary(), and calls |function_name| with 178 // Loads all libraries added with AddLibrary(), and calls |function_name| with
(...skipping 22 matching lines...) Expand all
171 std::vector<FilePath> user_libraries_; 201 std::vector<FilePath> user_libraries_;
172 202
173 // Indicates that the libraries have been pre-loaded and to not load them 203 // Indicates that the libraries have been pre-loaded and to not load them
174 // again. 204 // again.
175 bool libraries_preloaded_; 205 bool libraries_preloaded_;
176 206
177 // Saves the states of |test_fixture| and |test_name| for calling 207 // Saves the states of |test_fixture| and |test_name| for calling
178 // PreloadJavascriptLibraries(). 208 // PreloadJavascriptLibraries().
179 std::string preload_test_fixture_; 209 std::string preload_test_fixture_;
180 std::string preload_test_name_; 210 std::string preload_test_name_;
211
212 // The WebUI instance used for testing.
213 WebUI* web_ui_instance_;
214
215 // Track whether the custom WebUI instance has loaded.
216 bool web_ui_instance_ready_;
217
218 // Track if a message loop is running waiting on notification.
219 bool message_loop_running_;
220
221 NotificationRegistrar registrar_;
181 }; 222 };
182 223
183 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ 224 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698