| 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_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_ | 5 #ifndef CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_ |
| 6 #define CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_ | 6 #define CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "content/common/page_transition_types.h" | 13 #include "content/common/page_transition_types.h" |
| 14 #include "content/test/browser_test.h" |
| 15 #include "content/test/browser_test_base.h" |
| 14 #include "net/test/test_server.h" | 16 #include "net/test/test_server.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 #if defined(OS_CHROMEOS) | 19 #if defined(OS_CHROMEOS) |
| 18 #include "chrome/browser/chromeos/cros/cros_library.h" | 20 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 19 #endif // defined(OS_CHROMEOS) | 21 #endif // defined(OS_CHROMEOS) |
| 20 | 22 |
| 21 class Browser; | 23 class Browser; |
| 22 class CommandLine; | 24 class CommandLine; |
| 23 class Profile; | 25 class Profile; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // | 65 // |
| 64 // TearDownInProcessBrowserTestFixture() is called after BrowserMain() exits to | 66 // TearDownInProcessBrowserTestFixture() is called after BrowserMain() exits to |
| 65 // cleanup things setup for running the browser. | 67 // cleanup things setup for running the browser. |
| 66 // | 68 // |
| 67 // By default InProcessBrowserTest creates a single Browser (as returned from | 69 // By default InProcessBrowserTest creates a single Browser (as returned from |
| 68 // the CreateBrowser method). You can obviously create more as needed. | 70 // the CreateBrowser method). You can obviously create more as needed. |
| 69 | 71 |
| 70 // InProcessBrowserTest disables the sandbox when running. | 72 // InProcessBrowserTest disables the sandbox when running. |
| 71 // | 73 // |
| 72 // See ui_test_utils for a handful of methods designed for use with this class. | 74 // See ui_test_utils for a handful of methods designed for use with this class. |
| 73 class InProcessBrowserTest : public testing::Test { | 75 class InProcessBrowserTest : public BrowserTestBase { |
| 74 public: | 76 public: |
| 75 InProcessBrowserTest(); | 77 InProcessBrowserTest(); |
| 76 virtual ~InProcessBrowserTest(); | 78 virtual ~InProcessBrowserTest(); |
| 77 | 79 |
| 78 // We do this so we can be used in a Task. | |
| 79 void AddRef() {} | |
| 80 void Release() {} | |
| 81 static bool ImplementsThreadSafeReferenceCounting() { return false; } | |
| 82 | |
| 83 // Configures everything for an in process browser test, then invokes | 80 // Configures everything for an in process browser test, then invokes |
| 84 // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop. | 81 // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop. |
| 85 virtual void SetUp(); | 82 virtual void SetUp() OVERRIDE; |
| 86 | 83 |
| 87 // Restores state configured in SetUp. | 84 // Restores state configured in SetUp. |
| 88 virtual void TearDown(); | 85 virtual void TearDown() OVERRIDE; |
| 89 | 86 |
| 90 protected: | 87 protected: |
| 91 // Returns the browser created by CreateBrowser. | 88 // Returns the browser created by CreateBrowser. |
| 92 Browser* browser() const { return browser_; } | 89 Browser* browser() const { return browser_; } |
| 93 | 90 |
| 94 // Convenience methods for adding tabs to a Browser. | 91 // Convenience methods for adding tabs to a Browser. |
| 95 void AddTabAtIndexToBrowser(Browser* browser, | 92 void AddTabAtIndexToBrowser(Browser* browser, |
| 96 int index, | 93 int index, |
| 97 const GURL& url, | 94 const GURL& url, |
| 98 PageTransition::Type transition); | 95 PageTransition::Type transition); |
| 99 void AddTabAtIndex(int index, const GURL& url, | 96 void AddTabAtIndex(int index, const GURL& url, |
| 100 PageTransition::Type transition); | 97 PageTransition::Type transition); |
| 101 | 98 |
| 102 // Adds a selected tab at |index| to |url| with the specified |transition|. | 99 // Adds a selected tab at |index| to |url| with the specified |transition|. |
| 103 void AddTabAt(int index, const GURL& url, PageTransition::Type transition); | 100 void AddTabAt(int index, const GURL& url, PageTransition::Type transition); |
| 104 | 101 |
| 105 // Override this to add any custom setup code that needs to be done on the | 102 // Override this to add any custom setup code that needs to be done on the |
| 106 // main thread after the browser is created and just before calling | 103 // main thread after the browser is created and just before calling |
| 107 // RunTestOnMainThread(). | 104 // RunTestOnMainThread(). |
| 108 virtual void SetUpOnMainThread() {} | 105 virtual void SetUpOnMainThread() {} |
| 109 | 106 |
| 110 // Override this rather than TestBody. | |
| 111 virtual void RunTestOnMainThread() = 0; | |
| 112 | |
| 113 // Initializes the contents of the user data directory. Called by SetUp() | 107 // Initializes the contents of the user data directory. Called by SetUp() |
| 114 // after creating the user data directory, but before any browser is launched. | 108 // after creating the user data directory, but before any browser is launched. |
| 115 // If a test wishes to set up some initial non-empty state in the user data | 109 // If a test wishes to set up some initial non-empty state in the user data |
| 116 // directory before the browser starts up, it can do so here. Returns true if | 110 // directory before the browser starts up, it can do so here. Returns true if |
| 117 // successful. | 111 // successful. |
| 118 virtual bool SetUpUserDataDirectory() WARN_UNUSED_RESULT; | 112 virtual bool SetUpUserDataDirectory() WARN_UNUSED_RESULT; |
| 119 | 113 |
| 120 // We need these special methods because InProcessBrowserTest::SetUp is the | |
| 121 // bottom of the stack that winds up calling your test method, so it is not | |
| 122 // always an option to do what you want by overriding it and calling the | |
| 123 // superclass version. | |
| 124 // | |
| 125 // Override this for things you would normally override SetUp for. It will be | |
| 126 // called before your individual test fixture method is run, but after most | |
| 127 // of the overhead initialization has occured. | |
| 128 virtual void SetUpInProcessBrowserTestFixture() {} | |
| 129 | |
| 130 // Override this for things you would normally override TearDown for. | |
| 131 virtual void TearDownInProcessBrowserTestFixture() {} | |
| 132 | |
| 133 // Override this to add command line flags specific to your test. | 114 // Override this to add command line flags specific to your test. |
| 134 virtual void SetUpCommandLine(CommandLine* command_line) {} | 115 virtual void SetUpCommandLine(CommandLine* command_line) {} |
| 135 | 116 |
| 136 // Override this to add any custom cleanup code that needs to be done on the | 117 // Override this to add any custom cleanup code that needs to be done on the |
| 137 // main thread before the browser is torn down. | 118 // main thread before the browser is torn down. |
| 138 virtual void CleanUpOnMainThread() {} | 119 virtual void CleanUpOnMainThread() {} |
| 139 | 120 |
| 121 // BrowserTestBase: |
| 122 virtual void RunTestOnMainThreadLoop() OVERRIDE; |
| 123 |
| 140 // Returns the testing server. Guaranteed to be non-NULL. | 124 // Returns the testing server. Guaranteed to be non-NULL. |
| 141 net::TestServer* test_server() { return test_server_.get(); } | 125 net::TestServer* test_server() { return test_server_.get(); } |
| 142 | 126 |
| 143 // Creates a browser with a single tab (about:blank), waits for the tab to | 127 // Creates a browser with a single tab (about:blank), waits for the tab to |
| 144 // finish loading and shows the browser. | 128 // finish loading and shows the browser. |
| 145 // | 129 // |
| 146 // This is invoked from Setup. | 130 // This is invoked from Setup. |
| 147 virtual Browser* CreateBrowser(Profile* profile); | 131 virtual Browser* CreateBrowser(Profile* profile); |
| 148 | 132 |
| 149 // Similar to |CreateBrowser|, but creates an incognito browser. | 133 // Similar to |CreateBrowser|, but creates an incognito browser. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 169 void EnableDOMAutomation() { dom_automation_enabled_ = true; } | 153 void EnableDOMAutomation() { dom_automation_enabled_ = true; } |
| 170 void EnableTabCloseableStateWatcher() { | 154 void EnableTabCloseableStateWatcher() { |
| 171 tab_closeable_state_watcher_enabled_ = true; | 155 tab_closeable_state_watcher_enabled_ = true; |
| 172 } | 156 } |
| 173 | 157 |
| 174 private: | 158 private: |
| 175 // Creates a user data directory for the test if one is needed. Returns true | 159 // Creates a user data directory for the test if one is needed. Returns true |
| 176 // if successful. | 160 // if successful. |
| 177 virtual bool CreateUserDataDirectory() WARN_UNUSED_RESULT; | 161 virtual bool CreateUserDataDirectory() WARN_UNUSED_RESULT; |
| 178 | 162 |
| 179 // This is invoked from main after browser_init/browser_main have completed. | |
| 180 // This prepares for the test by creating a new browser, runs the test | |
| 181 // (RunTestOnMainThread), quits the browsers and returns. | |
| 182 void RunTestOnMainThreadLoop(); | |
| 183 | |
| 184 // Quits all open browsers and waits until there are no more browsers. | 163 // Quits all open browsers and waits until there are no more browsers. |
| 185 void QuitBrowsers(); | 164 void QuitBrowsers(); |
| 186 | 165 |
| 187 // Prepare command line that will be used to launch the child browser process | 166 // Prepare command line that will be used to launch the child browser process |
| 188 // with an in-process test. | 167 // with an in-process test. |
| 189 void PrepareTestCommandLine(CommandLine* command_line); | 168 void PrepareTestCommandLine(CommandLine* command_line); |
| 190 | 169 |
| 191 // Browser created from CreateBrowser. | 170 // Browser created from CreateBrowser. |
| 192 Browser* browser_; | 171 Browser* browser_; |
| 193 | 172 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 211 // Host resolver to use during the test. | 190 // Host resolver to use during the test. |
| 212 scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_; | 191 scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_; |
| 213 | 192 |
| 214 // Temporary user data directory. Used only when a user data directory is not | 193 // Temporary user data directory. Used only when a user data directory is not |
| 215 // specified in the command line. | 194 // specified in the command line. |
| 216 ScopedTempDir temp_user_data_dir_; | 195 ScopedTempDir temp_user_data_dir_; |
| 217 | 196 |
| 218 #if defined(OS_CHROMEOS) | 197 #if defined(OS_CHROMEOS) |
| 219 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 198 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
| 220 #endif // defined(OS_CHROMEOS) | 199 #endif // defined(OS_CHROMEOS) |
| 221 | |
| 222 DISALLOW_COPY_AND_ASSIGN(InProcessBrowserTest); | |
| 223 }; | 200 }; |
| 224 | 201 |
| 225 // We only want to use InProcessBrowserTest in test targets which properly | |
| 226 // isolate each test case by running each test in a separate process. | |
| 227 // This way if a test hangs the test launcher can reliably terminate it. | |
| 228 // | |
| 229 // InProcessBrowserTest cannot be run more than once in the same address space | |
| 230 // anyway - otherwise the second test crashes. | |
| 231 #if defined(HAS_OUT_OF_PROC_TEST_RUNNER) | |
| 232 | |
| 233 #define IN_PROC_BROWSER_TEST_(test_case_name, test_name, parent_class,\ | |
| 234 parent_id)\ | |
| 235 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\ | |
| 236 public:\ | |
| 237 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ | |
| 238 protected:\ | |
| 239 virtual void RunTestOnMainThread();\ | |
| 240 private:\ | |
| 241 virtual void TestBody() {}\ | |
| 242 static ::testing::TestInfo* const test_info_;\ | |
| 243 GTEST_DISALLOW_COPY_AND_ASSIGN_(\ | |
| 244 GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ | |
| 245 };\ | |
| 246 \ | |
| 247 ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ | |
| 248 ::test_info_ =\ | |
| 249 ::testing::internal::MakeAndRegisterTestInfo(\ | |
| 250 #test_case_name, #test_name, "", "", \ | |
| 251 (parent_id), \ | |
| 252 parent_class::SetUpTestCase, \ | |
| 253 parent_class::TearDownTestCase, \ | |
| 254 new ::testing::internal::TestFactoryImpl<\ | |
| 255 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\ | |
| 256 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::RunTestOnMainThread() | |
| 257 | |
| 258 #define IN_PROC_BROWSER_TEST_F(test_fixture, test_name)\ | |
| 259 IN_PROC_BROWSER_TEST_(test_fixture, test_name, test_fixture,\ | |
| 260 ::testing::internal::GetTypeId<test_fixture>()) | |
| 261 | |
| 262 #define IN_PROC_BROWSER_TEST_P(test_case_name, test_name) \ | |
| 263 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ | |
| 264 : public test_case_name { \ | |
| 265 public: \ | |
| 266 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {} \ | |
| 267 protected: \ | |
| 268 virtual void RunTestOnMainThread(); \ | |
| 269 private: \ | |
| 270 virtual void TestBody() {} \ | |
| 271 static int AddToRegistry() { \ | |
| 272 ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \ | |
| 273 GetTestCasePatternHolder<test_case_name>(\ | |
| 274 #test_case_name, __FILE__, __LINE__)->AddTestPattern(\ | |
| 275 #test_case_name, \ | |
| 276 #test_name, \ | |
| 277 new ::testing::internal::TestMetaFactory< \ | |
| 278 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>()); \ | |
| 279 return 0; \ | |
| 280 } \ | |
| 281 static int gtest_registering_dummy_; \ | |
| 282 GTEST_DISALLOW_COPY_AND_ASSIGN_(\ | |
| 283 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)); \ | |
| 284 }; \ | |
| 285 int GTEST_TEST_CLASS_NAME_(test_case_name, \ | |
| 286 test_name)::gtest_registering_dummy_ = \ | |
| 287 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \ | |
| 288 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::RunTestOnMainThread() | |
| 289 | |
| 290 #endif // defined(HAS_OUT_OF_PROC_TEST_RUNNER) | |
| 291 | |
| 292 #endif // CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_ | 202 #endif // CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_ |
| OLD | NEW |