| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/public/test/test_utils.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "content/public/browser/notification_service.h" |
| 11 #include "content/public/test/test_launcher.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Number of times to repost a Quit task so that the MessageLoop finishes up |
| 17 // pending tasks and tasks posted by those pending tasks without risking the |
| 18 // potential hang behavior of MessageLoop::QuitWhenIdle. |
| 19 // The criteria for choosing this number: it should be high enough to make the |
| 20 // quit act like QuitWhenIdle, while taking into account that any page which is |
| 21 // animating may be rendering another frame for each quit deferral. For an |
| 22 // animating page, the potential delay to quitting the RunLoop would be |
| 23 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as |
| 24 // 200ms/frame. |
| 25 static const int kNumQuitDeferrals = 10; |
| 26 |
| 27 static void DeferredQuitRunLoop(const base::Closure& quit_task, |
| 28 int num_quit_deferrals) { |
| 29 if (num_quit_deferrals <= 0) { |
| 30 quit_task.Run(); |
| 31 } else { |
| 32 MessageLoop::current()->PostTask(FROM_HERE, |
| 33 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); |
| 34 } |
| 35 } |
| 36 |
| 37 } |
| 38 |
| 39 namespace content { |
| 40 |
| 41 void RunThisRunLoop(base::RunLoop* run_loop) { |
| 42 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 43 |
| 44 // If we're running inside a browser test, we might need to allow the test |
| 45 // launcher to do extra work before/after running a nested message loop. |
| 46 test_launcher::TestLauncherDelegate* delegate = |
| 47 test_launcher::GetCurrentTestLauncherDelegate(); |
| 48 if (delegate) |
| 49 delegate->PreRunMessageLoop(run_loop); |
| 50 run_loop->Run(); |
| 51 if (delegate) |
| 52 delegate->PostRunMessageLoop(); |
| 53 } |
| 54 |
| 55 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { |
| 56 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), |
| 57 kNumQuitDeferrals); |
| 58 } |
| 59 |
| 60 MessageLoopRunner::MessageLoopRunner() { |
| 61 } |
| 62 |
| 63 MessageLoopRunner::~MessageLoopRunner() { |
| 64 } |
| 65 |
| 66 void MessageLoopRunner::Run() { |
| 67 RunThisRunLoop(&run_loop_); |
| 68 } |
| 69 |
| 70 base::Closure MessageLoopRunner::QuitClosure() { |
| 71 return base::Bind(&MessageLoopRunner::Quit, this); |
| 72 } |
| 73 |
| 74 void MessageLoopRunner::Quit() { |
| 75 GetQuitTaskForRunLoop(&run_loop_).Run(); |
| 76 } |
| 77 |
| 78 WindowedNotificationObserver::WindowedNotificationObserver( |
| 79 int notification_type, |
| 80 const NotificationSource& source) |
| 81 : seen_(false), |
| 82 running_(false), |
| 83 source_(NotificationService::AllSources()) { |
| 84 registrar_.Add(this, notification_type, source); |
| 85 } |
| 86 |
| 87 WindowedNotificationObserver::~WindowedNotificationObserver() {} |
| 88 |
| 89 void WindowedNotificationObserver::Wait() { |
| 90 if (seen_) |
| 91 return; |
| 92 |
| 93 running_ = true; |
| 94 message_loop_runner_ = new MessageLoopRunner; |
| 95 message_loop_runner_->Run(); |
| 96 EXPECT_TRUE(seen_); |
| 97 } |
| 98 |
| 99 void WindowedNotificationObserver::Observe( |
| 100 int type, |
| 101 const NotificationSource& source, |
| 102 const NotificationDetails& details) { |
| 103 source_ = source; |
| 104 details_ = details; |
| 105 seen_ = true; |
| 106 if (!running_) |
| 107 return; |
| 108 |
| 109 message_loop_runner_->Quit(); |
| 110 running_ = false; |
| 111 } |
| 112 |
| 113 } // namespace content |
| OLD | NEW |