Chromium Code Reviews| 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 CONTENT_BROWSER_WEBUI_WEB_UI_BROWSERTEST_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
| 6 #define CONTENT_BROWSER_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 | 14 |
| 15 class Value; | 15 class Value; |
| 16 class WebUIMessageHandler; | 16 class WebUIMessageHandler; |
| 17 | 17 |
| 18 // This macro simplifies the declaration of simple javascript unit tests. | 18 // This macro simplifies the declaration of simple javascript unit tests. |
| 19 // Use: | 19 // Use: |
| 20 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); | 20 // WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); |
| 21 #define WEB_UI_UNITTEST_F(x, y) \ | 21 #define WEB_UI_UNITTEST_F(x, y) \ |
| 22 IN_PROC_BROWSER_TEST_F(x, y) { \ | 22 IN_PROC_BROWSER_TEST_F(x, y) { \ |
| 23 ASSERT_TRUE(RunJavascriptTest(#y)); \ | 23 ASSERT_TRUE(RunJavascriptTest(#y)); \ |
| 24 } | 24 } |
| 25 | 25 |
| 26 #define WEB_UI_BROWSER_TEST_JS_(test_case_name, test_name, parent_class, \ | |
| 27 parent_id, assertion, jsfile) \ | |
| 28 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ | |
| 29 : public parent_class, public ::testing::WithParamInterface<std::string> {\ | |
| 30 public:\ | |
| 31 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ | |
| 32 protected:\ | |
| 33 virtual void PrepForJSOnMainThread();\ | |
| 34 virtual void RunTestOnMainThread() {\ | |
| 35 AddLibrary(FilePath(jsfile));\ | |
| 36 PrepForJSOnMainThread();\ | |
| 37 assertion(RunJavascriptTest(GetParam()));\ | |
| 38 }\ | |
| 39 private:\ | |
| 40 static ::testing::internal::TestMetaFactory< \ | |
| 41 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)> meta_factory_; \ | |
| 42 virtual void TestBody() {}\ | |
| 43 static int const gtest_registering_dummy_;\ | |
| 44 GTEST_DISALLOW_COPY_AND_ASSIGN_(\ | |
| 45 GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ | |
| 46 };\ | |
| 47 \ | |
| 48 ::testing::internal::TestMetaFactory< \ | |
| 49 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>\ | |
| 50 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::meta_factory_; \ | |
| 51 int const GTEST_TEST_CLASS_NAME_(test_case_name, \ | |
| 52 test_name)::gtest_registering_dummy_ = \ | |
| 53 ::parent_class::AddJSToRegistry(\ | |
| 54 FilePath(jsfile),\ | |
| 55 #test_case_name, #test_name, "", "",\ | |
|
David Tseng
2011/05/30 20:44:49
Do we need the |test_name| parameter? It is alrea
Sheridan Rawlins
2011/05/31 23:15:00
I include the test_name after the slash so that it
| |
| 56 (parent_id),\ | |
| 57 parent_class::SetUpTestCase,\ | |
| 58 parent_class::TearDownTestCase,\ | |
| 59 &meta_factory_);\ | |
| 60 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::PrepForJSOnMainThread() | |
| 61 | |
| 62 #define WEB_UI_BROWSER_TEST_JS_TRUE(test_fixture, test_name, jsfile) \ | |
| 63 WEB_UI_BROWSER_TEST_JS_(test_fixture, test_name, test_fixture, \ | |
| 64 ::testing::internal::GetTypeId<test_fixture>(), \ | |
| 65 ASSERT_TRUE, jsfile) | |
| 66 | |
| 67 #define WEB_UI_BROWSER_TEST_JS_FALSE(test_fixture, test_name, jsfile) \ | |
| 68 WEB_UI_BROWSER_TEST_JS_(test_fixture, test_name, test_fixture, \ | |
| 69 ::testing::internal::GetTypeId<test_fixture>(), \ | |
| 70 ASSERT_FALSE, jsfile) | |
| 71 | |
| 72 #define WEB_UI_BROWSER_TEST_JS WEB_UI_BROWSER_TEST_JS_TRUE | |
| 73 | |
| 26 // The runner of WebUI javascript based tests. | 74 // The runner of WebUI javascript based tests. |
| 27 // See chrome/test/data/webui/test_api.js for the javascript side test API's. | 75 // See chrome/test/data/webui/test_api.js for the javascript side test API's. |
| 28 // | 76 // |
| 29 // These tests should follow the form given in: | 77 // These tests should follow the form given in: |
| 30 // chrome/test/data/webui/sample_downloads.js. | 78 // chrome/test/data/webui/sample_downloads.js. |
| 31 // and the lone test within this class. | 79 // and the lone test within this class. |
| 32 class WebUIBrowserTest : public InProcessBrowserTest { | 80 class WebUIBrowserTest : public InProcessBrowserTest { |
| 33 public: | 81 public: |
| 34 typedef std::vector<const Value*> ConstValueVector; | 82 typedef std::vector<const Value*> ConstValueVector; |
| 35 virtual ~WebUIBrowserTest(); | 83 virtual ~WebUIBrowserTest(); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 62 | 110 |
| 63 protected: | 111 protected: |
| 64 WebUIBrowserTest(); | 112 WebUIBrowserTest(); |
| 65 | 113 |
| 66 // Setup test path. | 114 // Setup test path. |
| 67 virtual void SetUpInProcessBrowserTestFixture(); | 115 virtual void SetUpInProcessBrowserTestFixture(); |
| 68 | 116 |
| 69 // Returns a mock WebUI object under test (if any). | 117 // Returns a mock WebUI object under test (if any). |
| 70 virtual WebUIMessageHandler* GetMockMessageHandler(); | 118 virtual WebUIMessageHandler* GetMockMessageHandler(); |
| 71 | 119 |
| 120 static int AddJSToRegistry( | |
| 121 const FilePath& library_path, | |
| 122 const char* test_case_name, const char* name, | |
| 123 const char* type_param, | |
| 124 const char* value_param, | |
| 125 ::testing::internal::TypeId fixture_class_id, | |
| 126 ::testing::internal::SetUpTestCaseFunc set_up_tc, | |
| 127 ::testing::internal::TearDownTestCaseFunc tear_down_tc, | |
| 128 ::testing::internal::TestMetaFactoryBase<std::string>* meta_factory); | |
| 129 | |
| 72 private: | 130 private: |
| 73 // Builds a string containing all added javascript libraries. | 131 // Builds a string containing all added javascript libraries. |
| 74 void BuildJavascriptLibraries(std::string* content); | 132 void BuildJavascriptLibraries(std::string* content) const; |
| 75 | 133 |
| 76 // Builds a string with a call to the runTest JS function, passing the | 134 // Builds a string with a call to the runTest JS function, passing the |
| 77 // given test and its arguments. | 135 // given test and its arguments. |
| 78 string16 BuildRunTestJSCall(const std::string& test_name, | 136 string16 BuildRunTestJSCall(const std::string& test_name, |
| 79 const WebUIBrowserTest::ConstValueVector& args); | 137 const WebUIBrowserTest::ConstValueVector& args); |
| 80 | 138 |
| 81 // Calls the specified function with all libraries available. If |is_test| | 139 // Calls the specified function with all libraries available. If |is_test| |
| 82 // is true, the framework listens for pass fail messages from javascript. | 140 // is true, the framework listens for pass fail messages from javascript. |
| 83 // The provided arguments vector is passed to |function_name|. | 141 // The provided arguments vector is passed to |function_name|. |
| 84 bool RunJavascriptUsingHandler(const std::string& function_name, | 142 bool RunJavascriptUsingHandler(const std::string& function_name, |
| 85 const ConstValueVector& function_arguments, | 143 const ConstValueVector& function_arguments, |
| 86 bool is_test); | 144 bool is_test); |
| 87 | 145 |
| 88 // Attaches mock and test handlers. | 146 // Attaches mock and test handlers. |
| 89 void SetupHandlers(); | 147 void SetupHandlers(); |
| 90 | 148 |
| 91 // Handles test framework messages. | 149 // Handles test framework messages. |
| 92 scoped_ptr<WebUITestHandler> test_handler_; | 150 scoped_ptr<WebUITestHandler> test_handler_; |
| 93 | 151 |
| 94 // Location of test data (currently test/data/webui). | 152 // Location of test data (currently test/data/webui). |
| 95 FilePath test_data_directory_; | 153 FilePath test_data_directory_; |
| 96 | 154 |
| 97 // User added libraries | 155 // User added libraries |
| 98 std::vector<FilePath> user_libraries; | 156 std::vector<FilePath> user_libraries_; |
| 99 }; | 157 }; |
| 100 | 158 |
| 101 #endif // CONTENT_BROWSER_WEBUI_WEB_UI_BROWSERTEST_H_ | 159 #endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ |
| OLD | NEW |