• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_
6 #define CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_
7 
8 #include <string>
9 #include <vector>
10 
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"
14 
15 namespace base {
16 class Value;
17 }
18 
19 namespace content {
20 class RenderViewHost;
21 class WebUI;
22 class WebUIMessageHandler;
23 }
24 
25 class TestChromeWebUIControllerFactory;
26 class WebUITestHandler;
27 
28 // This macro simplifies the declaration of simple javascript unit tests.
29 // Use:
30 //   WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest);
31 #define WEB_UI_UNITTEST_F(x, y) \
32   IN_PROC_BROWSER_TEST_F(x, y) { ASSERT_TRUE(RunJavascriptTest(#y)); }
33 
34 // The runner of WebUI javascript based tests.
35 // See chrome/test/data/webui/test_api.js for the javascript side test API's.
36 //
37 // These tests should follow the form given in:
38 // chrome/test/data/webui/sample_downloads.js.
39 // and the lone test within this class.
40 class WebUIBrowserTest : public JavaScriptBrowserTest {
41  public:
42   virtual ~WebUIBrowserTest();
43 
44   // Runs a javascript function in the context of all libraries.
45   // Note that calls to functions in test_api.js are not supported.
46   // Takes ownership of Value* arguments.
47   bool RunJavascriptFunction(const std::string& function_name);
48   bool RunJavascriptFunction(const std::string& function_name,
49                              base::Value* arg);
50   bool RunJavascriptFunction(const std::string& function_name,
51                              base::Value* arg1,
52                              base::Value* arg2);
53   bool RunJavascriptFunction(const std::string& function_name,
54                              const ConstValueVector& function_arguments);
55 
56   // Runs a test fixture that may include calls to functions in test_api.js.
57   bool RunJavascriptTestF(bool is_async,
58                           const std::string& test_fixture,
59                           const std::string& test_name);
60 
61   // Runs a test that may include calls to functions in test_api.js.
62   // Takes ownership of Value* arguments.
63   bool RunJavascriptTest(const std::string& test_name);
64   bool RunJavascriptTest(const std::string& test_name, base::Value* arg);
65   bool RunJavascriptTest(const std::string& test_name,
66                          base::Value* arg1,
67                          base::Value* arg2);
68   bool RunJavascriptTest(const std::string& test_name,
69                          const ConstValueVector& test_arguments);
70 
71   // Runs a test that may include calls to functions in test_api.js, and waits
72   // for call to testDone().  Takes ownership of Value* arguments.
73   bool RunJavascriptAsyncTest(const std::string& test_name);
74   bool RunJavascriptAsyncTest(const std::string& test_name, base::Value* arg);
75   bool RunJavascriptAsyncTest(const std::string& test_name,
76                               base::Value* arg1,
77                               base::Value* arg2);
78   bool RunJavascriptAsyncTest(const std::string& test_name,
79                               base::Value* arg1,
80                               base::Value* arg2,
81                               base::Value* arg3);
82   bool RunJavascriptAsyncTest(const std::string& test_name,
83                               const ConstValueVector& test_arguments);
84 
85   // Sends message through |preload_host| to preload javascript libraries and
86   // sets the |libraries_preloaded| flag to prevent re-loading at next
87   // javascript invocation.
88   void PreLoadJavascriptLibraries(const std::string& preload_test_fixture,
89                                   const std::string& preload_test_name,
90                                   content::RenderViewHost* preload_host);
91 
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 BrowsePreload(const GURL& browse_to);
97 
98   // Called by javascript-generated test bodies to browse to a page and preload
99   // the javascript for the given |preload_test_fixture| and
100   // |preload_test_name|. chrome.send will be overridden to allow javascript
101   // handler mocking.
102   void BrowsePrintPreload(const GURL& browse_to);
103 
104  protected:
105   // URL to dummy WebUI page for testing framework.
106   static const char kDummyURL[];
107 
108   WebUIBrowserTest();
109 
110   // Accessors for preload test fixture and name.
111   void set_preload_test_fixture(const std::string& preload_test_fixture);
112   void set_preload_test_name(const std::string& preload_test_name);
113 
114   // Set up & tear down console error catching.
115   virtual void SetUpOnMainThread() OVERRIDE;
116   virtual void CleanUpOnMainThread() OVERRIDE;
117 
118   // Set a WebUI instance to run tests on.
119   void SetWebUIInstance(content::WebUI* web_ui);
120 
121   // Returns a mock WebUI object under test (if any).
122   virtual content::WebUIMessageHandler* GetMockMessageHandler();
123 
124   // Returns a file:// GURL constructed from |path| inside the test data dir for
125   // webui tests.
126   static GURL WebUITestDataPathToURL(const base::FilePath::StringType& path);
127 
128  private:
129   // Loads all libraries added with AddLibrary(), and calls |function_name| with
130   // |function_arguments|. When |is_test| is true, the framework wraps
131   // |function_name| with a test helper function, which waits for completion,
132   // logging an error message on failure, otherwise |function_name| is called
133   // asynchronously. When |preload_host| is non-NULL, sends the javascript to
134   // the RenderView for evaluation at the appropriate time before the onload
135   // call is made. Passes |is_async| along to runTest wrapper.
136   bool RunJavascriptUsingHandler(const std::string& function_name,
137                                  const ConstValueVector& function_arguments,
138                                  bool is_test,
139                                  bool is_async,
140                                  content::RenderViewHost* preload_host);
141 
142   // Attaches mock and test handlers.
143   void SetupHandlers();
144 
145   // Handles test framework messages.
146   scoped_ptr<WebUITestHandler> test_handler_;
147 
148   // Indicates that the libraries have been pre-loaded and to not load them
149   // again.
150   bool libraries_preloaded_;
151 
152   // Saves the states of |test_fixture| and |test_name| for calling
153   // PreloadJavascriptLibraries().
154   std::string preload_test_fixture_;
155   std::string preload_test_name_;
156 
157   // When this is non-NULL, this is The WebUI instance used for testing.
158   // Otherwise the selected tab's web_ui is used.
159   content::WebUI* override_selected_web_ui_;
160 
161   scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
162 };
163 
164 #endif  // CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_
165