OLD | NEW |
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/base/js_injection_ready_observer.h" |
| 16 #include "chrome/test/base/test_navigation_observer.h" |
16 | 17 |
17 class RenderViewHost; | 18 class RenderViewHost; |
18 class WebUIMessageHandler; | 19 class WebUIMessageHandler; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 class Value; | 22 class Value; |
22 } | 23 } |
23 | 24 |
24 // This macro simplifies the declaration of simple javascript unit tests. | 25 // This macro simplifies the declaration of simple javascript unit tests. |
25 // Use: | 26 // Use: |
26 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); | 27 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); |
27 #define WEB_UI_UNITTEST_F(x, y) \ | 28 #define WEB_UI_UNITTEST_F(x, y) \ |
28 IN_PROC_BROWSER_TEST_F(x, y) { \ | 29 IN_PROC_BROWSER_TEST_F(x, y) { \ |
29 ASSERT_TRUE(RunJavascriptTest(#y)); \ | 30 ASSERT_TRUE(RunJavascriptTest(#y)); \ |
30 } | 31 } |
31 | 32 |
32 // The runner of WebUI javascript based tests. | 33 // The runner of WebUI javascript based tests. |
33 // See chrome/test/data/webui/test_api.js for the javascript side test API's. | 34 // See chrome/test/data/webui/test_api.js for the javascript side test API's. |
34 // | 35 // |
35 // These tests should follow the form given in: | 36 // These tests should follow the form given in: |
36 // chrome/test/data/webui/sample_downloads.js. | 37 // chrome/test/data/webui/sample_downloads.js. |
37 // and the lone test within this class. | 38 // and the lone test within this class. |
38 class WebUIBrowserTest | 39 class WebUIBrowserTest |
39 : public InProcessBrowserTest, | 40 : public InProcessBrowserTest, |
40 public TestNavigationObserver::JsInjectionReadyObserver { | 41 public JsInjectionReadyObserver { |
41 public: | 42 public: |
42 typedef std::vector<const base::Value*> ConstValueVector; | 43 typedef std::vector<const base::Value*> ConstValueVector; |
43 virtual ~WebUIBrowserTest(); | 44 virtual ~WebUIBrowserTest(); |
44 | 45 |
45 // Add a custom helper JS library for your test. | 46 // Add a custom helper JS library for your test. |
46 // If a relative path is specified, it'll be read | 47 // If a relative path is specified, it'll be read |
47 // as relative to the test data dir. | 48 // as relative to the test data dir. |
48 void AddLibrary(const FilePath& library_path); | 49 void AddLibrary(const FilePath& library_path); |
49 | 50 |
50 // Runs a javascript function in the context of all libraries. | 51 // Runs a javascript function in the context of all libraries. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 // sets the |libraries_preloaded| flag to prevent re-loading at next | 95 // sets the |libraries_preloaded| flag to prevent re-loading at next |
95 // javascript invocation. | 96 // javascript invocation. |
96 void PreLoadJavascriptLibraries(const std::string& preload_test_fixture, | 97 void PreLoadJavascriptLibraries(const std::string& preload_test_fixture, |
97 const std::string& preload_test_name, | 98 const std::string& preload_test_name, |
98 RenderViewHost* preload_host); | 99 RenderViewHost* preload_host); |
99 | 100 |
100 // Called by javascript-generated test bodies to browse to a page and preload | 101 // Called by javascript-generated test bodies to browse to a page and preload |
101 // the javascript for the given |preload_test_fixture| and | 102 // the javascript for the given |preload_test_fixture| and |
102 // |preload_test_name|. chrome.send will be overridden to allow javascript | 103 // |preload_test_name|. chrome.send will be overridden to allow javascript |
103 // handler mocking. | 104 // handler mocking. |
104 void BrowsePreload(const GURL& browse_to, | 105 void BrowsePreload(const GURL& browse_to); |
105 const std::string& preload_test_fixture, | |
106 const std::string& preload_test_name); | |
107 | 106 |
108 // Called by javascript-generated test bodies to browse to a page and preload | 107 // Called by javascript-generated test bodies to browse to a page and preload |
109 // the javascript for the given |preload_test_fixture| and | 108 // the javascript for the given |preload_test_fixture| and |
110 // |preload_test_name|. chrome.send will be overridden to allow javascript | 109 // |preload_test_name|. chrome.send will be overridden to allow javascript |
111 // handler mocking. | 110 // handler mocking. |
112 void BrowsePrintPreload(const GURL& browse_to, | 111 void BrowsePrintPreload(const GURL& browse_to); |
113 const std::string& preload_test_fixture, | |
114 const std::string& preload_test_name); | |
115 | 112 |
116 protected: | 113 protected: |
117 // URL to dummy WebUI page for testing framework. | 114 // URL to dummy WebUI page for testing framework. |
118 static const char kDummyURL[]; | 115 static const char kDummyURL[]; |
119 | 116 |
120 WebUIBrowserTest(); | 117 WebUIBrowserTest(); |
121 | 118 |
| 119 // Accessors for preload test fixture and name. |
| 120 void set_preload_test_fixture(const std::string& preload_test_fixture); |
| 121 void set_preload_test_name(const std::string& preload_test_name); |
| 122 |
122 // Set up & tear down console error catching. | 123 // Set up & tear down console error catching. |
123 virtual void SetUpOnMainThread() OVERRIDE; | 124 virtual void SetUpOnMainThread() OVERRIDE; |
124 virtual void CleanUpOnMainThread() OVERRIDE; | 125 virtual void CleanUpOnMainThread() OVERRIDE; |
125 | 126 |
126 // Set up test path & override for |kDummyURL|. | 127 // Set up test path & override for |kDummyURL|. |
127 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | 128 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; |
128 | 129 |
129 // Tear down override for |kDummyURL|. | 130 // Tear down override for |kDummyURL|. |
130 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; | 131 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; |
131 | 132 |
132 // Set a WebUI instance to run tests on. | 133 // Set a WebUI instance to run tests on. |
133 void SetWebUIInstance(WebUI* web_ui); | 134 void SetWebUIInstance(WebUI* web_ui); |
134 | 135 |
135 // Returns a mock WebUI object under test (if any). | 136 // Returns a mock WebUI object under test (if any). |
136 virtual WebUIMessageHandler* GetMockMessageHandler(); | 137 virtual WebUIMessageHandler* GetMockMessageHandler(); |
137 | 138 |
138 // Returns a file:// GURL constructed from |path| inside the test data dir for | 139 // Returns a file:// GURL constructed from |path| inside the test data dir for |
139 // webui tests. | 140 // webui tests. |
140 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); | 141 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); |
141 | 142 |
142 private: | 143 private: |
143 // TestNavigationObserver::JsInjectionReadyObserver implementation. | 144 // JsInjectionReadyObserver implementation. |
144 virtual void OnJsInjectionReady(RenderViewHost* render_view_host) OVERRIDE; | 145 virtual void OnJsInjectionReady(RenderViewHost* render_view_host) OVERRIDE; |
145 | 146 |
146 // Builds a string containing all added javascript libraries. | 147 // Builds a string containing all added javascript libraries. |
147 void BuildJavascriptLibraries(string16* content); | 148 void BuildJavascriptLibraries(string16* content); |
148 | 149 |
149 // Builds a string with a call to the runTest JS function, passing the | 150 // Builds a string with a call to the runTest JS function, passing the |
150 // given |is_async|, |test_name| and its |args|. | 151 // given |is_async|, |test_name| and its |args|. |
151 string16 BuildRunTestJSCall(bool is_async, | 152 string16 BuildRunTestJSCall(bool is_async, |
152 const std::string& test_name, | 153 const std::string& test_name, |
153 const WebUIBrowserTest::ConstValueVector& args); | 154 const WebUIBrowserTest::ConstValueVector& args); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // PreloadJavascriptLibraries(). | 189 // PreloadJavascriptLibraries(). |
189 std::string preload_test_fixture_; | 190 std::string preload_test_fixture_; |
190 std::string preload_test_name_; | 191 std::string preload_test_name_; |
191 | 192 |
192 // When this is non-NULL, this is The WebUI instance used for testing. | 193 // When this is non-NULL, this is The WebUI instance used for testing. |
193 // Otherwise the selected tab's web_ui is used. | 194 // Otherwise the selected tab's web_ui is used. |
194 WebUI* override_selected_web_ui_; | 195 WebUI* override_selected_web_ui_; |
195 }; | 196 }; |
196 | 197 |
197 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | 198 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
OLD | NEW |