Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / base / web_ui_browser_test.h
blob0bc0d5ffaaee94a36eb24249d8228a1a21fcacec
1 // Copyright 2014 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_BROWSER_TEST_H_
6 #define CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_
8 #include <string>
9 #include <vector>
11 #include "base/files/file_path.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/javascript_browser_test.h"
15 namespace base {
16 class Value;
19 namespace content {
20 class RenderViewHost;
21 class WebUI;
22 class WebUIMessageHandler;
25 class TestChromeWebUIControllerFactory;
26 class WebUITestHandler;
28 // The runner of WebUI javascript based tests.
29 // See chrome/test/data/webui/test_api.js for the javascript side test API's.
31 // These tests should follow the form given in:
32 // chrome/test/data/webui/sample_downloads.js.
33 // and the lone test within this class.
34 class WebUIBrowserTest : public JavaScriptBrowserTest {
35 public:
36 ~WebUIBrowserTest() override;
38 // Runs a javascript function in the context of all libraries.
39 // Note that calls to functions in test_api.js are not supported.
40 // Takes ownership of Value* arguments.
41 bool RunJavascriptFunction(const std::string& function_name);
42 bool RunJavascriptFunction(const std::string& function_name,
43 base::Value* arg);
44 bool RunJavascriptFunction(const std::string& function_name,
45 base::Value* arg1,
46 base::Value* arg2);
47 bool RunJavascriptFunction(const std::string& function_name,
48 const ConstValueVector& function_arguments);
50 // Runs a test fixture that may include calls to functions in test_api.js.
51 bool RunJavascriptTestF(bool is_async,
52 const std::string& test_fixture,
53 const std::string& test_name);
55 // Runs a test that may include calls to functions in test_api.js.
56 // Takes ownership of Value* arguments.
57 bool RunJavascriptTest(const std::string& test_name);
58 bool RunJavascriptTest(const std::string& test_name, base::Value* arg);
59 bool RunJavascriptTest(const std::string& test_name,
60 base::Value* arg1,
61 base::Value* arg2);
62 bool RunJavascriptTest(const std::string& test_name,
63 const ConstValueVector& test_arguments);
65 // Runs a test that may include calls to functions in test_api.js, and waits
66 // for call to testDone(). Takes ownership of Value* arguments.
67 bool RunJavascriptAsyncTest(const std::string& test_name);
68 bool RunJavascriptAsyncTest(const std::string& test_name, base::Value* arg);
69 bool RunJavascriptAsyncTest(const std::string& test_name,
70 base::Value* arg1,
71 base::Value* arg2);
72 bool RunJavascriptAsyncTest(const std::string& test_name,
73 base::Value* arg1,
74 base::Value* arg2,
75 base::Value* arg3);
76 bool RunJavascriptAsyncTest(const std::string& test_name,
77 const ConstValueVector& test_arguments);
79 // Sends message through |preload_host| to preload javascript libraries and
80 // sets the |libraries_preloaded| flag to prevent re-loading at next
81 // javascript invocation.
82 void PreLoadJavascriptLibraries(const std::string& preload_test_fixture,
83 const std::string& preload_test_name,
84 content::RenderViewHost* preload_host);
86 // Called by javascript-generated test bodies to browse to a page and preload
87 // the javascript for the given |preload_test_fixture| and
88 // |preload_test_name|. chrome.send will be overridden to allow javascript
89 // handler mocking.
90 void BrowsePreload(const GURL& browse_to);
92 // Called by javascript-generated test bodies to browse to a page and preload
93 // the javascript for the given |preload_test_fixture| and
94 // |preload_test_name|. chrome.send will be overridden to allow javascript
95 // handler mocking.
96 void BrowsePrintPreload(const GURL& browse_to);
98 protected:
99 // URL to dummy WebUI page for testing framework.
100 static const char kDummyURL[];
102 WebUIBrowserTest();
104 // Accessors for preload test fixture and name.
105 void set_preload_test_fixture(const std::string& preload_test_fixture);
106 void set_preload_test_name(const std::string& preload_test_name);
108 // Set up & tear down console error catching.
109 void SetUpOnMainThread() override;
110 void TearDownOnMainThread() override;
112 // Set a WebUI instance to run tests on.
113 void SetWebUIInstance(content::WebUI* web_ui);
115 // Returns a mock WebUI object under test (if any).
116 virtual content::WebUIMessageHandler* GetMockMessageHandler();
118 // Returns a file:// GURL constructed from |path| inside the test data dir for
119 // webui tests.
120 static GURL WebUITestDataPathToURL(const base::FilePath::StringType& path);
122 private:
123 // Loads all libraries added with AddLibrary(), and calls |function_name| with
124 // |function_arguments|. When |is_test| is true, the framework wraps
125 // |function_name| with a test helper function, which waits for completion,
126 // logging an error message on failure, otherwise |function_name| is called
127 // asynchronously. When |preload_host| is non-NULL, sends the javascript to
128 // the RenderView for evaluation at the appropriate time before the onload
129 // call is made. Passes |is_async| along to runTest wrapper.
130 bool RunJavascriptUsingHandler(const std::string& function_name,
131 const ConstValueVector& function_arguments,
132 bool is_test,
133 bool is_async,
134 content::RenderViewHost* preload_host);
136 // Attaches mock and test handlers.
137 void SetupHandlers();
139 // Handles test framework messages.
140 scoped_ptr<WebUITestHandler> test_handler_;
142 // Indicates that the libraries have been pre-loaded and to not load them
143 // again.
144 bool libraries_preloaded_;
146 // Saves the states of |test_fixture| and |test_name| for calling
147 // PreloadJavascriptLibraries().
148 std::string preload_test_fixture_;
149 std::string preload_test_name_;
151 // When this is non-NULL, this is The WebUI instance used for testing.
152 // Otherwise the selected tab's web_ui is used.
153 content::WebUI* override_selected_web_ui_;
155 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
158 #endif // CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_