| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_TEST_BASE_WEB_UI_BROWSER_TEST_H_ | 5 #ifndef CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_ |
| 6 #define CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_ | 6 #define CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/files/file_path.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" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/javascript_browser_test.h" |
| 15 | 14 |
| 16 namespace base { | 15 namespace base { |
| 17 class Value; | 16 class Value; |
| 18 } | 17 } |
| 19 | 18 |
| 20 namespace content { | 19 namespace content { |
| 21 class RenderViewHost; | 20 class RenderViewHost; |
| 22 class WebUI; | 21 class WebUI; |
| 23 class WebUIMessageHandler; | 22 class WebUIMessageHandler; |
| 24 } | 23 } |
| 25 | 24 |
| 26 class TestChromeWebUIControllerFactory; | 25 class TestChromeWebUIControllerFactory; |
| 27 class WebUITestHandler; | 26 class WebUITestHandler; |
| 28 | 27 |
| 29 // This macro simplifies the declaration of simple javascript unit tests. | 28 // This macro simplifies the declaration of simple javascript unit tests. |
| 30 // Use: | 29 // Use: |
| 31 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); | 30 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); |
| 32 #define WEB_UI_UNITTEST_F(x, y) \ | 31 #define WEB_UI_UNITTEST_F(x, y) \ |
| 33 IN_PROC_BROWSER_TEST_F(x, y) { \ | 32 IN_PROC_BROWSER_TEST_F(x, y) { ASSERT_TRUE(RunJavascriptTest(#y)); } |
| 34 ASSERT_TRUE(RunJavascriptTest(#y)); \ | |
| 35 } | |
| 36 | 33 |
| 37 // The runner of WebUI javascript based tests. | 34 // The runner of WebUI javascript based tests. |
| 38 // See chrome/test/data/webui/test_api.js for the javascript side test API's. | 35 // See chrome/test/data/webui/test_api.js for the javascript side test API's. |
| 39 // | 36 // |
| 40 // These tests should follow the form given in: | 37 // These tests should follow the form given in: |
| 41 // chrome/test/data/webui/sample_downloads.js. | 38 // chrome/test/data/webui/sample_downloads.js. |
| 42 // and the lone test within this class. | 39 // and the lone test within this class. |
| 43 class WebUIBrowserTest : public InProcessBrowserTest { | 40 class WebUIBrowserTest : public JavaScriptBrowserTest { |
| 44 public: | 41 public: |
| 45 typedef ScopedVector<const base::Value> ConstValueVector; | |
| 46 virtual ~WebUIBrowserTest(); | 42 virtual ~WebUIBrowserTest(); |
| 47 | 43 |
| 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); | |
| 52 | |
| 53 // Runs a javascript function in the context of all libraries. | 44 // Runs a javascript function in the context of all libraries. |
| 54 // Note that calls to functions in test_api.js are not supported. | 45 // Note that calls to functions in test_api.js are not supported. |
| 55 // Takes ownership of Value* arguments. | 46 // Takes ownership of Value* arguments. |
| 56 bool RunJavascriptFunction(const std::string& function_name); | 47 bool RunJavascriptFunction(const std::string& function_name); |
| 57 bool RunJavascriptFunction(const std::string& function_name, | 48 bool RunJavascriptFunction(const std::string& function_name, |
| 58 base::Value* arg); | 49 base::Value* arg); |
| 59 bool RunJavascriptFunction(const std::string& function_name, | 50 bool RunJavascriptFunction(const std::string& function_name, |
| 60 base::Value* arg1, | 51 base::Value* arg1, |
| 61 base::Value* arg2); | 52 base::Value* arg2); |
| 62 bool RunJavascriptFunction(const std::string& function_name, | 53 bool RunJavascriptFunction(const std::string& function_name, |
| 63 const ConstValueVector& function_arguments); | 54 const ConstValueVector& function_arguments); |
| 64 | 55 |
| 65 // Runs a test fixture that may include calls to functions in test_api.js. | 56 // Runs a test fixture that may include calls to functions in test_api.js. |
| 66 bool RunJavascriptTestF(bool is_async, | 57 bool RunJavascriptTestF(bool is_async, |
| 67 const std::string& test_fixture, | 58 const std::string& test_fixture, |
| 68 const std::string& test_name); | 59 const std::string& test_name); |
| 69 | 60 |
| 70 // Runs a test that may include calls to functions in test_api.js. | 61 // Runs a test that may include calls to functions in test_api.js. |
| 71 // Takes ownership of Value* arguments. | 62 // Takes ownership of Value* arguments. |
| 72 bool RunJavascriptTest(const std::string& test_name); | 63 bool RunJavascriptTest(const std::string& test_name); |
| 73 bool RunJavascriptTest(const std::string& test_name, | 64 bool RunJavascriptTest(const std::string& test_name, base::Value* arg); |
| 74 base::Value* arg); | |
| 75 bool RunJavascriptTest(const std::string& test_name, | 65 bool RunJavascriptTest(const std::string& test_name, |
| 76 base::Value* arg1, | 66 base::Value* arg1, |
| 77 base::Value* arg2); | 67 base::Value* arg2); |
| 78 bool RunJavascriptTest(const std::string& test_name, | 68 bool RunJavascriptTest(const std::string& test_name, |
| 79 const ConstValueVector& test_arguments); | 69 const ConstValueVector& test_arguments); |
| 80 | 70 |
| 81 // Runs a test that may include calls to functions in test_api.js, and waits | 71 // 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. | 72 // for call to testDone(). Takes ownership of Value* arguments. |
| 83 bool RunJavascriptAsyncTest(const std::string& test_name); | 73 bool RunJavascriptAsyncTest(const std::string& test_name); |
| 84 bool RunJavascriptAsyncTest(const std::string& test_name, | 74 bool RunJavascriptAsyncTest(const std::string& test_name, base::Value* arg); |
| 85 base::Value* arg); | |
| 86 bool RunJavascriptAsyncTest(const std::string& test_name, | 75 bool RunJavascriptAsyncTest(const std::string& test_name, |
| 87 base::Value* arg1, | 76 base::Value* arg1, |
| 88 base::Value* arg2); | 77 base::Value* arg2); |
| 89 bool RunJavascriptAsyncTest(const std::string& test_name, | 78 bool RunJavascriptAsyncTest(const std::string& test_name, |
| 90 base::Value* arg1, | 79 base::Value* arg1, |
| 91 base::Value* arg2, | 80 base::Value* arg2, |
| 92 base::Value* arg3); | 81 base::Value* arg3); |
| 93 bool RunJavascriptAsyncTest(const std::string& test_name, | 82 bool RunJavascriptAsyncTest(const std::string& test_name, |
| 94 const ConstValueVector& test_arguments); | 83 const ConstValueVector& test_arguments); |
| 95 | 84 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 void SetWebUIInstance(content::WebUI* web_ui); | 119 void SetWebUIInstance(content::WebUI* web_ui); |
| 131 | 120 |
| 132 // Returns a mock WebUI object under test (if any). | 121 // Returns a mock WebUI object under test (if any). |
| 133 virtual content::WebUIMessageHandler* GetMockMessageHandler(); | 122 virtual content::WebUIMessageHandler* GetMockMessageHandler(); |
| 134 | 123 |
| 135 // Returns a file:// GURL constructed from |path| inside the test data dir for | 124 // Returns a file:// GURL constructed from |path| inside the test data dir for |
| 136 // webui tests. | 125 // webui tests. |
| 137 static GURL WebUITestDataPathToURL(const base::FilePath::StringType& path); | 126 static GURL WebUITestDataPathToURL(const base::FilePath::StringType& path); |
| 138 | 127 |
| 139 private: | 128 private: |
| 140 // Builds a vector of strings of all added javascript libraries. | |
| 141 void BuildJavascriptLibraries(std::vector<base::string16>* libraries); | |
| 142 | |
| 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( | |
| 146 bool is_async, | |
| 147 const std::string& test_name, | |
| 148 const WebUIBrowserTest::ConstValueVector& args); | |
| 149 | |
| 150 // Loads all libraries added with AddLibrary(), and calls |function_name| with | 129 // Loads all libraries added with AddLibrary(), and calls |function_name| with |
| 151 // |function_arguments|. When |is_test| is true, the framework wraps | 130 // |function_arguments|. When |is_test| is true, the framework wraps |
| 152 // |function_name| with a test helper function, which waits for completion, | 131 // |function_name| with a test helper function, which waits for completion, |
| 153 // logging an error message on failure, otherwise |function_name| is called | 132 // logging an error message on failure, otherwise |function_name| is called |
| 154 // asynchronously. When |preload_host| is non-NULL, sends the javascript to | 133 // asynchronously. When |preload_host| is non-NULL, sends the javascript to |
| 155 // the RenderView for evaluation at the appropriate time before the onload | 134 // the RenderView for evaluation at the appropriate time before the onload |
| 156 // call is made. Passes |is_async| along to runTest wrapper. | 135 // call is made. Passes |is_async| along to runTest wrapper. |
| 157 bool RunJavascriptUsingHandler(const std::string& function_name, | 136 bool RunJavascriptUsingHandler(const std::string& function_name, |
| 158 const ConstValueVector& function_arguments, | 137 const ConstValueVector& function_arguments, |
| 159 bool is_test, | 138 bool is_test, |
| 160 bool is_async, | 139 bool is_async, |
| 161 content::RenderViewHost* preload_host); | 140 content::RenderViewHost* preload_host); |
| 162 | 141 |
| 163 // Attaches mock and test handlers. | 142 // Attaches mock and test handlers. |
| 164 void SetupHandlers(); | 143 void SetupHandlers(); |
| 165 | 144 |
| 166 // Handles test framework messages. | 145 // Handles test framework messages. |
| 167 scoped_ptr<WebUITestHandler> test_handler_; | 146 scoped_ptr<WebUITestHandler> test_handler_; |
| 168 | 147 |
| 169 // Indicates that the libraries have been pre-loaded and to not load them | 148 // Indicates that the libraries have been pre-loaded and to not load them |
| 170 // again. | 149 // again. |
| 171 bool libraries_preloaded_; | 150 bool libraries_preloaded_; |
| 172 | 151 |
| 173 // Saves the states of |test_fixture| and |test_name| for calling | 152 // Saves the states of |test_fixture| and |test_name| for calling |
| 174 // PreloadJavascriptLibraries(). | 153 // PreloadJavascriptLibraries(). |
| 175 std::string preload_test_fixture_; | 154 std::string preload_test_fixture_; |
| 176 std::string preload_test_name_; | 155 std::string preload_test_name_; |
| 177 | 156 |
| 178 // User added libraries. | |
| 179 std::vector<base::FilePath> user_libraries_; | |
| 180 | |
| 181 // User library search paths. | |
| 182 std::vector<base::FilePath> library_search_paths_; | |
| 183 | |
| 184 // When this is non-NULL, this is The WebUI instance used for testing. | 157 // When this is non-NULL, this is The WebUI instance used for testing. |
| 185 // Otherwise the selected tab's web_ui is used. | 158 // Otherwise the selected tab's web_ui is used. |
| 186 content::WebUI* override_selected_web_ui_; | 159 content::WebUI* override_selected_web_ui_; |
| 187 | 160 |
| 188 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_; | 161 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_; |
| 189 }; | 162 }; |
| 190 | 163 |
| 191 #endif // CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_ | 164 #endif // CHROME_TEST_BASE_WEB_UI_BROWSER_TEST_H_ |
| OLD | NEW |