OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
6 #define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "chrome/browser/ui/webui/web_ui_test_handler.h" | 12 #include "chrome/browser/ui/webui/web_ui_test_handler.h" |
13 #include "chrome/test/in_process_browser_test.h" | 13 #include "chrome/test/in_process_browser_test.h" |
| 14 #include "chrome/test/test_navigation_observer.h" |
14 | 15 |
15 class Value; | 16 class Value; |
16 class WebUIMessageHandler; | 17 class WebUIMessageHandler; |
17 | 18 |
18 // This macro simplifies the declaration of simple javascript unit tests. | 19 // This macro simplifies the declaration of simple javascript unit tests. |
19 // Use: | 20 // Use: |
20 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); | 21 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); |
21 #define WEB_UI_UNITTEST_F(x, y) \ | 22 #define WEB_UI_UNITTEST_F(x, y) \ |
22 IN_PROC_BROWSER_TEST_F(x, y) { \ | 23 IN_PROC_BROWSER_TEST_F(x, y) { \ |
23 ASSERT_TRUE(RunJavascriptTest(#y)); \ | 24 ASSERT_TRUE(RunJavascriptTest(#y)); \ |
24 } | 25 } |
25 | 26 |
26 // The runner of WebUI javascript based tests. | 27 // The runner of WebUI javascript based tests. |
27 // See chrome/test/data/webui/test_api.js for the javascript side test API's. | 28 // See chrome/test/data/webui/test_api.js for the javascript side test API's. |
28 // | 29 // |
29 // These tests should follow the form given in: | 30 // These tests should follow the form given in: |
30 // chrome/test/data/webui/sample_downloads.js. | 31 // chrome/test/data/webui/sample_downloads.js. |
31 // and the lone test within this class. | 32 // and the lone test within this class. |
32 class WebUIBrowserTest : public InProcessBrowserTest { | 33 class WebUIBrowserTest |
| 34 : public InProcessBrowserTest, |
| 35 public TestNavigationObserver::JsInjectionReadyObserver { |
33 public: | 36 public: |
34 typedef std::vector<const Value*> ConstValueVector; | 37 typedef std::vector<const Value*> ConstValueVector; |
35 virtual ~WebUIBrowserTest(); | 38 virtual ~WebUIBrowserTest(); |
36 | 39 |
37 // Add a custom helper JS library for your test. | 40 // Add a custom helper JS library for your test. |
38 // If a relative path is specified, it'll be read | 41 // If a relative path is specified, it'll be read |
39 // as relative to the test data dir. | 42 // as relative to the test data dir. |
40 void AddLibrary(const FilePath& library_path); | 43 void AddLibrary(const FilePath& library_path); |
41 | 44 |
42 // Runs a javascript function in the context of all libraries. | 45 // Runs a javascript function in the context of all libraries. |
43 // Note that calls to functions in test_api.js are not supported. | 46 // Note that calls to functions in test_api.js are not supported. |
44 bool RunJavascriptFunction(const std::string& function_name); | 47 bool RunJavascriptFunction(const std::string& function_name); |
45 bool RunJavascriptFunction(const std::string& function_name, | 48 bool RunJavascriptFunction(const std::string& function_name, |
46 const Value& arg); | 49 const Value& arg); |
47 bool RunJavascriptFunction(const std::string& function_name, | 50 bool RunJavascriptFunction(const std::string& function_name, |
48 const Value& arg1, | 51 const Value& arg1, |
49 const Value& arg2); | 52 const Value& arg2); |
50 bool RunJavascriptFunction(const std::string& function_name, | 53 bool RunJavascriptFunction(const std::string& function_name, |
51 const ConstValueVector& function_arguments); | 54 const ConstValueVector& function_arguments); |
52 | 55 |
| 56 // Runs a test fixture that may include calls to functions in test_api.js. |
| 57 bool RunJavascriptTestF(const std::string& test_fixture, |
| 58 const std::string& test_name); |
| 59 |
53 // Runs a test that may include calls to functions in test_api.js. | 60 // Runs a test that may include calls to functions in test_api.js. |
54 bool RunJavascriptTest(const std::string& test_name); | 61 bool RunJavascriptTest(const std::string& test_name); |
55 bool RunJavascriptTest(const std::string& test_name, | 62 bool RunJavascriptTest(const std::string& test_name, |
56 const Value& arg); | 63 const Value& arg); |
57 bool RunJavascriptTest(const std::string& test_name, | 64 bool RunJavascriptTest(const std::string& test_name, |
58 const Value& arg1, | 65 const Value& arg1, |
59 const Value& arg2); | 66 const Value& arg2); |
60 bool RunJavascriptTest(const std::string& test_name, | 67 bool RunJavascriptTest(const std::string& test_name, |
61 const ConstValueVector& test_arguments); | 68 const ConstValueVector& test_arguments); |
62 | 69 |
63 // Preloads the javascript libraries and sets the |libraries_preloaded| flag | 70 // Preloads the javascript libraries and sets the |libraries_preloaded| flag |
64 // to prevent re-loading at next javascript invocation. If | 71 // to prevent re-loading at next javascript invocation. If |
65 // |override_chrome_send| is true, then chrome.send is overridden for | 72 // |override_chrome_send| is true, then chrome.send is overridden for |
66 // javascript to register handlers. | 73 // javascript to register handlers. |
67 void PreLoadJavascriptLibraries(bool override_chrome_send); | 74 void PreLoadJavascriptLibraries(const std::string& preload_test_fixture, |
| 75 const std::string& preload_test_name); |
| 76 |
| 77 // Called by javascript-generated test bodies to browse to a page and preload |
| 78 // the javascript for the given |preload_test_fixture| and |
| 79 // |preload_test_name|. chrome.send will be overridden to allow javascript |
| 80 // handler mocking. |
| 81 void BrowsePreload(const GURL& browse_to, |
| 82 const std::string& preload_test_fixture, |
| 83 const std::string& preload_test_name); |
| 84 |
| 85 // Called by javascript-generated test bodies to browse to a page and preload |
| 86 // the javascript for the given |preload_test_fixture| and |
| 87 // |preload_test_name|. chrome.send will be overridden to allow javascript |
| 88 // handler mocking. |
| 89 void BrowsePrintPreload(const GURL& browse_to, |
| 90 const std::string& preload_test_fixture, |
| 91 const std::string& preload_test_name); |
68 | 92 |
69 protected: | 93 protected: |
70 WebUIBrowserTest(); | 94 WebUIBrowserTest(); |
71 | 95 |
72 // Setup test path. | 96 // Setup test path. |
73 virtual void SetUpInProcessBrowserTestFixture(); | 97 virtual void SetUpInProcessBrowserTestFixture(); |
74 | 98 |
75 // Returns a mock WebUI object under test (if any). | 99 // Returns a mock WebUI object under test (if any). |
76 virtual WebUIMessageHandler* GetMockMessageHandler(); | 100 virtual WebUIMessageHandler* GetMockMessageHandler(); |
77 | 101 |
78 // Skip this test with |skip_test_message|. | |
79 void skipTest(const std::string& skip_test_message); | |
80 | |
81 // Returns a file:// GURL constructed from |path| inside the test data dir for | 102 // Returns a file:// GURL constructed from |path| inside the test data dir for |
82 // webui tests. | 103 // webui tests. |
83 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); | 104 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); |
84 | 105 |
85 private: | 106 private: |
| 107 // TestNavigationObserver::JsInjectionReadyObserver implementation. |
| 108 virtual void OnJsInjectionReady() OVERRIDE; |
| 109 |
86 // Builds a string containing all added javascript libraries. | 110 // Builds a string containing all added javascript libraries. |
87 void BuildJavascriptLibraries(std::string* content); | 111 void BuildJavascriptLibraries(std::string* content); |
88 | 112 |
89 // Builds a string with a call to the runTest JS function, passing the | 113 // Builds a string with a call to the runTest JS function, passing the |
90 // given test and its arguments. | 114 // given test and its arguments. |
91 string16 BuildRunTestJSCall(const std::string& test_name, | 115 string16 BuildRunTestJSCall(const std::string& test_name, |
92 const WebUIBrowserTest::ConstValueVector& args); | 116 const WebUIBrowserTest::ConstValueVector& args); |
93 | 117 |
94 // Calls the specified function with all libraries available. If |is_test| | 118 // Calls the specified function with all libraries available. If |is_test| |
95 // is true, the framework listens for pass fail messages from javascript. | 119 // is true, the framework listens for pass fail messages from javascript. |
(...skipping 11 matching lines...) Expand all Loading... |
107 // Location of test data (currently test/data/webui). | 131 // Location of test data (currently test/data/webui). |
108 FilePath test_data_directory_; | 132 FilePath test_data_directory_; |
109 | 133 |
110 // User added libraries | 134 // User added libraries |
111 std::vector<FilePath> user_libraries_; | 135 std::vector<FilePath> user_libraries_; |
112 | 136 |
113 // Indicates that the libraries have been pre-loaded and to not load them | 137 // Indicates that the libraries have been pre-loaded and to not load them |
114 // again. | 138 // again. |
115 bool libraries_preloaded_; | 139 bool libraries_preloaded_; |
116 | 140 |
117 bool skip_test_; | 141 // Saves the states of |test_fixture| and |test_name| for calling |
118 std::string skip_test_message_; | 142 // PreloadJavascriptLibraries(). |
| 143 std::string preload_test_fixture_; |
| 144 std::string preload_test_name_; |
119 }; | 145 }; |
120 | 146 |
121 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | 147 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
OLD | NEW |