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(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 string16 BuildRunTestJSCall(bool is_async,
146 const std::string& test_name,
147 const WebUIBrowserTest::ConstValueVector& args);
149 // Loads all libraries added with AddLibrary(), and calls |function_name| with
150 // |function_arguments|. When |is_test| is true, the framework wraps
151 // |function_name| with a test helper function, which waits for completion,
152 // logging an error message on failure, otherwise |function_name| is called
153 // asynchronously. When |preload_host| is non-NULL, sends the javascript to
154 // the RenderView for evaluation at the appropriate time before the onload
155 // call is made. Passes |is_async| along to runTest wrapper.
156 bool RunJavascriptUsingHandler(const std::string& function_name,
157 const ConstValueVector& function_arguments,
160 content::RenderViewHost* preload_host);
162 // Attaches mock and test handlers.
163 void SetupHandlers();
165 // Handles test framework messages.
166 scoped_ptr<WebUITestHandler> test_handler_;
168 // Location of test data (currently test/data/webui).
169 base::FilePath test_data_directory_;
171 // Location of generated test data (<(PROGRAM_DIR)/test_data).
172 base::FilePath gen_test_data_directory_;
174 // User added libraries
175 std::vector<base::FilePath> user_libraries_;
177 // Indicates that the libraries have been pre-loaded and to not load them
179 bool libraries_preloaded_;
181 // Saves the states of |test_fixture| and |test_name| for calling
182 // PreloadJavascriptLibraries().
183 std::string preload_test_fixture_;
184 std::string preload_test_name_;
186 // When this is non-NULL, this is The WebUI instance used for testing.
187 // Otherwise the selected tab's web_ui is used.
188 content::WebUI* override_selected_web_ui_;
190 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
193 #endif // CHROME_TEST_BASE_WEB_UI_BROWSERTEST_H_