| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ | 5 #ifndef CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ |
| 6 #define CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ | 6 #define CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include <atlbase.h> | 10 #include <atlbase.h> |
| 11 #include <atlwin.h> | 11 #include <atlwin.h> |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/cancelable_callback.h" |
| 16 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 18 #include "base/file_path.h" |
| 17 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
| 19 #include "base/process_util.h" | 21 #include "base/process_util.h" |
| 20 #include "base/test/test_reg_util_win.h" | 22 #include "base/test/test_reg_util_win.h" |
| 21 #include "base/win/registry.h" | 23 #include "base/win/registry.h" |
| 22 #include "base/win/scoped_comptr.h" | 24 #include "base/win/scoped_comptr.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" |
| 23 #include "chrome_frame/chrome_tab.h" | 26 #include "chrome_frame/chrome_tab.h" |
| 24 #include "chrome_frame/test/simulate_input.h" | 27 #include "chrome_frame/test/simulate_input.h" |
| 25 #include "chrome_frame/test_utils.h" | 28 #include "chrome_frame/test_utils.h" |
| 26 #include "chrome_frame/utils.h" | 29 #include "chrome_frame/utils.h" |
| 27 | 30 |
| 28 #include "gtest/gtest.h" | 31 #include "gtest/gtest.h" |
| 29 | 32 |
| 30 // Needed for CreateFunctor. | 33 // Needed for CreateFunctor. |
| 31 #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING | 34 #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING |
| 32 #include "testing/gmock_mutant.h" | 35 #include "testing/gmock_mutant.h" |
| 33 | 36 |
| 34 class FilePath; | 37 class FilePath; |
| 35 interface IWebBrowser2; | 38 interface IWebBrowser2; |
| 36 | 39 |
| 37 namespace chrome_frame_test { | 40 namespace chrome_frame_test { |
| 38 | 41 |
| 42 FilePath GetSnapshotDirectory(); |
| 43 |
| 39 int CloseVisibleWindowsOnAllThreads(HANDLE process); | 44 int CloseVisibleWindowsOnAllThreads(HANDLE process); |
| 40 | 45 |
| 41 base::ProcessHandle LaunchIE(const std::wstring& url); | 46 base::ProcessHandle LaunchIE(const std::wstring& url); |
| 42 base::ProcessHandle LaunchChrome(const std::wstring& url, | 47 base::ProcessHandle LaunchChrome(const std::wstring& url, |
| 43 const FilePath& user_data_dir); | 48 const FilePath& user_data_dir); |
| 44 | 49 |
| 45 // Attempts to close all open IE windows. | 50 // Attempts to close all open IE windows. |
| 46 // The return value is the number of windows closed. | 51 // The return value is the number of windows closed. |
| 47 // @note: this function requires COM to be initialized on the calling thread. | 52 // @note: this function requires COM to be initialized on the calling thread. |
| 48 // Since the caller might be running in either MTA or STA, the function does | 53 // Since the caller might be running in either MTA or STA, the function does |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 186 |
| 182 // used to detect if outgoing COM calls hung. | 187 // used to detect if outgoing COM calls hung. |
| 183 bool is_hung_; | 188 bool is_hung_; |
| 184 base::win::ScopedComPtr<IMessageFilter> prev_filter_; | 189 base::win::ScopedComPtr<IMessageFilter> prev_filter_; |
| 185 }; | 190 }; |
| 186 | 191 |
| 187 // MessageLoopForUI wrapper that runs only for a limited time. | 192 // MessageLoopForUI wrapper that runs only for a limited time. |
| 188 // We need a UI message loop in the main thread. | 193 // We need a UI message loop in the main thread. |
| 189 class TimedMsgLoop { | 194 class TimedMsgLoop { |
| 190 public: | 195 public: |
| 191 TimedMsgLoop() : quit_loop_invoked_(false) { | 196 TimedMsgLoop() : snapshot_on_timeout_(false), quit_loop_invoked_(false) { |
| 197 } |
| 198 |
| 199 void set_snapshot_on_timeout(bool value) { |
| 200 snapshot_on_timeout_ = value; |
| 192 } | 201 } |
| 193 | 202 |
| 194 void RunFor(int seconds) { | 203 void RunFor(int seconds) { |
| 195 QuitAfter(seconds); | |
| 196 quit_loop_invoked_ = false; | 204 quit_loop_invoked_ = false; |
| 205 if (snapshot_on_timeout_) |
| 206 timeout_closure_.Reset(base::Bind(&TimedMsgLoop::SnapshotAndQuit)); |
| 207 else |
| 208 timeout_closure_.Reset(MessageLoop::QuitClosure()); |
| 209 loop_.PostDelayedTask( |
| 210 FROM_HERE, timeout_closure_.callback(), 1000 * seconds); |
| 197 loop_.MessageLoop::Run(); | 211 loop_.MessageLoop::Run(); |
| 212 timeout_closure_.Cancel(); |
| 198 } | 213 } |
| 199 | 214 |
| 200 void PostTask(const tracked_objects::Location& from_here, | 215 void PostTask(const tracked_objects::Location& from_here, |
| 201 const base::Closure& task) { | 216 const base::Closure& task) { |
| 202 loop_.PostTask(from_here, task); | 217 loop_.PostTask(from_here, task); |
| 203 } | 218 } |
| 204 | 219 |
| 205 void PostDelayedTask(const tracked_objects::Location& from_here, | 220 void PostDelayedTask(const tracked_objects::Location& from_here, |
| 206 const base::Closure& task, int64 delay_ms) { | 221 const base::Closure& task, int64 delay_ms) { |
| 207 loop_.PostDelayedTask(from_here, task, delay_ms); | 222 loop_.PostDelayedTask(from_here, task, delay_ms); |
| 208 } | 223 } |
| 209 | 224 |
| 210 void Quit() { | 225 void Quit() { |
| 211 QuitAfter(0); | 226 QuitAfter(0); |
| 212 } | 227 } |
| 213 | 228 |
| 214 void QuitAfter(int seconds) { | 229 void QuitAfter(int seconds) { |
| 230 timeout_closure_.Cancel(); |
| 215 quit_loop_invoked_ = true; | 231 quit_loop_invoked_ = true; |
| 216 loop_.PostDelayedTask( | 232 loop_.PostDelayedTask( |
| 217 FROM_HERE, MessageLoop::QuitClosure(), 1000 * seconds); | 233 FROM_HERE, MessageLoop::QuitClosure(), 1000 * seconds); |
| 218 } | 234 } |
| 219 | 235 |
| 220 bool WasTimedOut() const { | 236 bool WasTimedOut() const { |
| 221 return !quit_loop_invoked_; | 237 return !quit_loop_invoked_; |
| 222 } | 238 } |
| 223 | 239 |
| 240 void RunAllPending() { |
| 241 loop_.RunAllPending(); |
| 242 } |
| 243 |
| 224 private: | 244 private: |
| 245 static void SnapshotAndQuit() { |
| 246 FilePath snapshot; |
| 247 if (ui_test_utils::SaveScreenSnapshotToDirectory(GetSnapshotDirectory(), |
| 248 &snapshot)) { |
| 249 testing::UnitTest* unit_test = testing::UnitTest::GetInstance(); |
| 250 const testing::TestInfo* test_info = unit_test->current_test_info(); |
| 251 std::string name; |
| 252 if (test_info != NULL) { |
| 253 name.append(test_info->test_case_name()) |
| 254 .append(1, '.') |
| 255 .append(test_info->name()); |
| 256 } else { |
| 257 name = "unknown test"; |
| 258 } |
| 259 LOG(ERROR) << name << " timed out. Screen snapshot saved to " |
| 260 << snapshot.value(); |
| 261 } |
| 262 MessageLoop::current()->Quit(); |
| 263 } |
| 264 |
| 225 MessageLoopForUI loop_; | 265 MessageLoopForUI loop_; |
| 266 base::CancelableClosure timeout_closure_; |
| 267 bool snapshot_on_timeout_; |
| 226 bool quit_loop_invoked_; | 268 bool quit_loop_invoked_; |
| 227 }; | 269 }; |
| 228 | 270 |
| 229 // Saves typing. It's somewhat hard to create a wrapper around | 271 // Saves typing. It's somewhat hard to create a wrapper around |
| 230 // testing::InvokeWithoutArgs since it returns a | 272 // testing::InvokeWithoutArgs since it returns a |
| 231 // non-public (testing::internal) type. | 273 // non-public (testing::internal) type. |
| 232 #define QUIT_LOOP(loop) testing::InvokeWithoutArgs(\ | 274 #define QUIT_LOOP(loop) testing::InvokeWithoutArgs(\ |
| 233 testing::CreateFunctor(&loop, &chrome_frame_test::TimedMsgLoop::Quit)) | 275 testing::CreateFunctor(&loop, &chrome_frame_test::TimedMsgLoop::Quit)) |
| 234 | 276 |
| 235 #define QUIT_LOOP_SOON(loop, seconds) testing::InvokeWithoutArgs(\ | 277 #define QUIT_LOOP_SOON(loop, seconds) testing::InvokeWithoutArgs(\ |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 } // namespace chrome_frame_test | 373 } // namespace chrome_frame_test |
| 332 | 374 |
| 333 // TODO(tommi): This is a temporary workaround while we're getting our | 375 // TODO(tommi): This is a temporary workaround while we're getting our |
| 334 // Singleton story straight. Ideally each test should clear up any singletons | 376 // Singleton story straight. Ideally each test should clear up any singletons |
| 335 // it might have created, but test cases do not implicitly have their own | 377 // it might have created, but test cases do not implicitly have their own |
| 336 // AtExitManager, so we have this workaround method for tests that depend on | 378 // AtExitManager, so we have this workaround method for tests that depend on |
| 337 // "fresh" singletons. The implementation is in chrome_frame_unittest_main.cc. | 379 // "fresh" singletons. The implementation is in chrome_frame_unittest_main.cc. |
| 338 void DeleteAllSingletons(); | 380 void DeleteAllSingletons(); |
| 339 | 381 |
| 340 #endif // CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ | 382 #endif // CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ |
| OLD | NEW |