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 : public InProcessBrowserTest, |
34 public TestNavigationObserver::LoadStartObserver { | |
33 public: | 35 public: |
34 typedef std::vector<const Value*> ConstValueVector; | 36 typedef std::vector<const Value*> ConstValueVector; |
35 virtual ~WebUIBrowserTest(); | 37 virtual ~WebUIBrowserTest(); |
36 | 38 |
37 // Add a custom helper JS library for your test. | 39 // Add a custom helper JS library for your test. |
38 // If a relative path is specified, it'll be read | 40 // If a relative path is specified, it'll be read |
39 // as relative to the test data dir. | 41 // as relative to the test data dir. |
40 void AddLibrary(const FilePath& library_path); | 42 void AddLibrary(const FilePath& library_path); |
41 | 43 |
42 // Runs a javascript function in the context of all libraries. | 44 // Runs a javascript function in the context of all libraries. |
43 // 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. |
44 bool RunJavascriptFunction(const std::string& function_name); | 46 bool RunJavascriptFunction(const std::string& function_name); |
45 bool RunJavascriptFunction(const std::string& function_name, | 47 bool RunJavascriptFunction(const std::string& function_name, |
46 const Value& arg); | 48 const Value& arg); |
47 bool RunJavascriptFunction(const std::string& function_name, | 49 bool RunJavascriptFunction(const std::string& function_name, |
48 const Value& arg1, | 50 const Value& arg1, |
49 const Value& arg2); | 51 const Value& arg2); |
50 bool RunJavascriptFunction(const std::string& function_name, | 52 bool RunJavascriptFunction(const std::string& function_name, |
51 const ConstValueVector& function_arguments); | 53 const ConstValueVector& function_arguments); |
52 | 54 |
55 // Runs a test fixture that may include calls to functions in test_api.js. | |
56 bool RunJavascriptTestF(const std::string& test_fixture, | |
57 const std::string& test_name); | |
58 | |
53 // Runs a test that may include calls to functions in test_api.js. | 59 // Runs a test that may include calls to functions in test_api.js. |
54 bool RunJavascriptTest(const std::string& test_name); | 60 bool RunJavascriptTest(const std::string& test_name); |
55 bool RunJavascriptTest(const std::string& test_name, | 61 bool RunJavascriptTest(const std::string& test_name, |
56 const Value& arg); | 62 const Value& arg); |
57 bool RunJavascriptTest(const std::string& test_name, | 63 bool RunJavascriptTest(const std::string& test_name, |
58 const Value& arg1, | 64 const Value& arg1, |
59 const Value& arg2); | 65 const Value& arg2); |
60 bool RunJavascriptTest(const std::string& test_name, | 66 bool RunJavascriptTest(const std::string& test_name, |
61 const ConstValueVector& test_arguments); | 67 const ConstValueVector& test_arguments); |
62 | 68 |
63 // Preloads the javascript libraries and sets the |libraries_preloaded| flag | 69 // Preloads the javascript libraries and sets the |libraries_preloaded| flag |
64 // to prevent re-loading at next javascript invocation. If | 70 // to prevent re-loading at next javascript invocation. If |
65 // |override_chrome_send| is true, then chrome.send is overridden for | 71 // |override_chrome_send| is true, then chrome.send is overridden for |
66 // javascript to register handlers. | 72 // javascript to register handlers. |
67 void PreLoadJavascriptLibraries(bool override_chrome_send); | 73 void PreLoadJavascriptLibraries(const std::string& preload_test_fixture, |
74 const std::string& preload_test_name); | |
75 | |
76 // Called by javascript-generated test bodies to browse to a page and preload | |
77 // the javascript for the given |preload_test_fixture| and | |
78 // |preload_test_name|. chrome.send will be overridden to allow javascript | |
79 // handler mocking. | |
80 void BrowsePreload(const GURL& browse_to, | |
81 const std::string& preload_test_fixture, | |
82 const std::string& preload_test_name); | |
83 | |
84 // Called by javascript-generated test bodies to browse to a page and preload | |
85 // the javascript for the given |preload_test_fixture| and | |
86 // |preload_test_name|. chrome.send will be overridden to allow javascript | |
87 // handler mocking. | |
88 void BrowsePrintPreload(const GURL& browse_to, | |
89 const std::string& preload_test_fixture, | |
90 const std::string& preload_test_name); | |
68 | 91 |
69 protected: | 92 protected: |
70 WebUIBrowserTest(); | 93 WebUIBrowserTest(); |
71 | 94 |
72 // Setup test path. | 95 // Setup test path. |
73 virtual void SetUpInProcessBrowserTestFixture(); | 96 virtual void SetUpInProcessBrowserTestFixture(); |
74 | 97 |
75 // Returns a mock WebUI object under test (if any). | 98 // Returns a mock WebUI object under test (if any). |
76 virtual WebUIMessageHandler* GetMockMessageHandler(); | 99 virtual WebUIMessageHandler* GetMockMessageHandler(); |
77 | 100 |
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 | 101 // Returns a file:// GURL constructed from |path| inside the test data dir for |
82 // webui tests. | 102 // webui tests. |
83 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); | 103 static GURL WebUITestDataPathToURL(const FilePath::StringType& path); |
84 | 104 |
105 // Saves the states of |test_fixture| and |test_name| for calling | |
106 // PreloadJavascriptLibraries(). | |
107 std::string preload_test_fixture_; | |
sky
2011/07/11 20:50:22
style guide says no public/protected fields.
Sheridan Rawlins
2011/07/11 22:22:41
Done.
| |
108 std::string preload_test_name_; | |
109 | |
85 private: | 110 private: |
111 // TestNavigationObserver::LoadStartObserver implementation. | |
112 virtual void OnLoadStart() OVERRIDE; | |
113 | |
86 // Builds a string containing all added javascript libraries. | 114 // Builds a string containing all added javascript libraries. |
87 void BuildJavascriptLibraries(std::string* content); | 115 void BuildJavascriptLibraries(std::string* content); |
88 | 116 |
89 // Builds a string with a call to the runTest JS function, passing the | 117 // Builds a string with a call to the runTest JS function, passing the |
90 // given test and its arguments. | 118 // given test and its arguments. |
91 string16 BuildRunTestJSCall(const std::string& test_name, | 119 string16 BuildRunTestJSCall(const std::string& test_name, |
92 const WebUIBrowserTest::ConstValueVector& args); | 120 const WebUIBrowserTest::ConstValueVector& args); |
93 | 121 |
94 // Calls the specified function with all libraries available. If |is_test| | 122 // Calls the specified function with all libraries available. If |is_test| |
95 // is true, the framework listens for pass fail messages from javascript. | 123 // is true, the framework listens for pass fail messages from javascript. |
(...skipping 10 matching lines...) Expand all Loading... | |
106 | 134 |
107 // Location of test data (currently test/data/webui). | 135 // Location of test data (currently test/data/webui). |
108 FilePath test_data_directory_; | 136 FilePath test_data_directory_; |
109 | 137 |
110 // User added libraries | 138 // User added libraries |
111 std::vector<FilePath> user_libraries_; | 139 std::vector<FilePath> user_libraries_; |
112 | 140 |
113 // Indicates that the libraries have been pre-loaded and to not load them | 141 // Indicates that the libraries have been pre-loaded and to not load them |
114 // again. | 142 // again. |
115 bool libraries_preloaded_; | 143 bool libraries_preloaded_; |
116 | |
117 bool skip_test_; | |
118 std::string skip_test_message_; | |
119 }; | 144 }; |
120 | 145 |
121 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ | 146 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
OLD | NEW |