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.
5 #ifndef CHROME_TEST_BASE_WEB_UI_BROWSERTEST_H_
6 #define CHROME_TEST_BASE_WEB_UI_BROWSERTEST_H_
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string16.h"
14 #include "chrome/test/base/in_process_browser_test.h"
23 class WebUIMessageHandler
;
26 class TestChromeWebUIControllerFactory
;
27 class WebUITestHandler
;
29 // This macro simplifies the declaration of simple javascript unit tests.
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)); \
37 // The runner of WebUI javascript based tests.
38 // See chrome/test/data/webui/test_api.js for the javascript side test API's.
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
: public InProcessBrowserTest
{
45 typedef ScopedVector
<const base::Value
> ConstValueVector
;
46 virtual ~WebUIBrowserTest();
48 // Add a custom helper JS library for your test.
49 // If a relative path is specified, it'll be read
50 // as relative to the test data dir.
51 void AddLibrary(const base::FilePath
& library_path
);
53 // Runs a javascript function in the context of all libraries.
54 // Note that calls to functions in test_api.js are not supported.
55 // Takes ownership of Value* arguments.
56 bool RunJavascriptFunction(const std::string
& function_name
);
57 bool RunJavascriptFunction(const std::string
& function_name
,
59 bool RunJavascriptFunction(const std::string
& function_name
,
62 bool RunJavascriptFunction(const std::string
& function_name
,
63 const ConstValueVector
& function_arguments
);
65 // Runs a test fixture that may include calls to functions in test_api.js.
66 bool RunJavascriptTestF(bool is_async
,
67 const std::string
& test_fixture
,
68 const std::string
& test_name
);
70 // Runs a test that may include calls to functions in test_api.js.
71 // Takes ownership of Value* arguments.
72 bool RunJavascriptTest(const std::string
& test_name
);
73 bool RunJavascriptTest(const std::string
& test_name
,
75 bool RunJavascriptTest(const std::string
& test_name
,
78 bool RunJavascriptTest(const std::string
& test_name
,
79 const ConstValueVector
& test_arguments
);
81 // Runs a test that may include calls to functions in test_api.js, and waits
82 // for call to testDone(). Takes ownership of Value* arguments.
83 bool RunJavascriptAsyncTest(const std::string
& test_name
);
84 bool RunJavascriptAsyncTest(const std::string
& test_name
,
86 bool RunJavascriptAsyncTest(const std::string
& test_name
,
89 bool RunJavascriptAsyncTest(const std::string
& test_name
,
93 bool RunJavascriptAsyncTest(const std::string
& test_name
,
94 const ConstValueVector
& test_arguments
);
96 // Sends message through |preload_host| to preload javascript libraries and
97 // sets the |libraries_preloaded| flag to prevent re-loading at next
98 // javascript invocation.
99 void PreLoadJavascriptLibraries(const std::string
& preload_test_fixture
,
100 const std::string
& preload_test_name
,
101 content::RenderViewHost
* preload_host
);
103 // Called by javascript-generated test bodies to browse to a page and preload
104 // the javascript for the given |preload_test_fixture| and
105 // |preload_test_name|. chrome.send will be overridden to allow javascript
107 void BrowsePreload(const GURL
& browse_to
);
109 // Called by javascript-generated test bodies to browse to a page and preload
110 // the javascript for the given |preload_test_fixture| and
111 // |preload_test_name|. chrome.send will be overridden to allow javascript
113 void BrowsePrintPreload(const GURL
& browse_to
);
116 // URL to dummy WebUI page for testing framework.
117 static const char kDummyURL
[];
121 // Accessors for preload test fixture and name.
122 void set_preload_test_fixture(const std::string
& preload_test_fixture
);
123 void set_preload_test_name(const std::string
& preload_test_name
);
125 // Set up & tear down console error catching.
126 virtual void SetUpOnMainThread() OVERRIDE
;
127 virtual void CleanUpOnMainThread() OVERRIDE
;
129 // Set a WebUI instance to run tests on.
130 void SetWebUIInstance(content::WebUI
* web_ui
);
132 // Returns a mock WebUI object under test (if any).
133 virtual content::WebUIMessageHandler
* GetMockMessageHandler();
135 // Returns a file:// GURL constructed from |path| inside the test data dir for
137 static GURL
WebUITestDataPathToURL(const base::FilePath::StringType
& path
);
140 // Builds a string containing all added javascript libraries.
141 void BuildJavascriptLibraries(base::string16
* content
);
143 // Builds a string with a call to the runTest JS function, passing the
144 // given |is_async|, |test_name| and its |args|.
145 base::string16
BuildRunTestJSCall(
147 const std::string
& test_name
,
148 const WebUIBrowserTest::ConstValueVector
& args
);
150 // Loads all libraries added with AddLibrary(), and calls |function_name| with
151 // |function_arguments|. When |is_test| is true, the framework wraps
152 // |function_name| with a test helper function, which waits for completion,
153 // logging an error message on failure, otherwise |function_name| is called
154 // asynchronously. When |preload_host| is non-NULL, sends the javascript to
155 // the RenderView for evaluation at the appropriate time before the onload
156 // call is made. Passes |is_async| along to runTest wrapper.
157 bool RunJavascriptUsingHandler(const std::string
& function_name
,
158 const ConstValueVector
& function_arguments
,
161 content::RenderViewHost
* preload_host
);
163 // Attaches mock and test handlers.
164 void SetupHandlers();
166 // Handles test framework messages.
167 scoped_ptr
<WebUITestHandler
> test_handler_
;
169 // Location of test data (currently test/data/webui).
170 base::FilePath test_data_directory_
;
172 // Location of generated test data (<(PROGRAM_DIR)/test_data).
173 base::FilePath gen_test_data_directory_
;
175 // User added libraries
176 std::vector
<base::FilePath
> user_libraries_
;
178 // Indicates that the libraries have been pre-loaded and to not load them
180 bool libraries_preloaded_
;
182 // Saves the states of |test_fixture| and |test_name| for calling
183 // PreloadJavascriptLibraries().
184 std::string preload_test_fixture_
;
185 std::string preload_test_name_
;
187 // When this is non-NULL, this is The WebUI instance used for testing.
188 // Otherwise the selected tab's web_ui is used.
189 content::WebUI
* override_selected_web_ui_
;
191 scoped_ptr
<TestChromeWebUIControllerFactory
> test_factory_
;
194 #endif // CHROME_TEST_BASE_WEB_UI_BROWSERTEST_H_