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.
5 #ifndef CONTENT_PUBLIC_TEST_TEST_UTILS_H_
6 #define CONTENT_PUBLIC_TEST_TEST_UTILS_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "content/public/browser/browser_child_process_observer.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/notification_source.h"
23 // A collection of functions designed for use with unit and browser tests.
27 class RenderFrameHost
;
29 // Turns on nestable tasks, runs the message loop, then resets nestable tasks
30 // to what they were originally. Prefer this over MessageLoop::Run for in
31 // process browser tests that need to block until a condition is met.
32 void RunMessageLoop();
34 // Variant of RunMessageLoop that takes RunLoop.
35 void RunThisRunLoop(base::RunLoop
* run_loop
);
37 // Turns on nestable tasks, runs all pending tasks in the message loop,
38 // then resets nestable tasks to what they were originally. Prefer this
39 // over MessageLoop::RunAllPending for in process browser tests to run
41 void RunAllPendingInMessageLoop();
43 // Blocks the current thread until all the pending messages in the loop of the
44 // thread |thread_id| have been processed.
45 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id
);
47 // Runs until both the blocking pool and the current message loop are empty
48 // (have no more scheduled tasks) and no tasks are running.
49 void RunAllBlockingPoolTasksUntilIdle();
51 // Get task to quit the given RunLoop. It allows a few generations of pending
52 // tasks to run as opposed to run_loop->QuitClosure().
53 base::Closure
GetQuitTaskForRunLoop(base::RunLoop
* run_loop
);
55 // Executes the specified JavaScript in the specified frame, and runs a nested
56 // MessageLoop. When the result is available, it is returned.
57 // This should not be used; the use of the ExecuteScript functions in
58 // browser_test_utils is preferable.
59 scoped_ptr
<base::Value
> ExecuteScriptAndGetValue(
60 RenderFrameHost
* render_frame_host
, const std::string
& script
);
62 // Helper class to Run and Quit the message loop. Run and Quit can only happen
63 // once per instance. Make a new instance for each use. Calling Quit after Run
64 // has returned is safe and has no effect.
65 class MessageLoopRunner
: public base::RefCounted
<MessageLoopRunner
> {
69 // Run the current MessageLoop unless the quit closure
70 // has already been called.
73 // Quit the matching call to Run (nested MessageLoops are unaffected).
76 // Hand this closure off to code that uses callbacks to notify completion.
78 // scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
79 // kick_off_some_api(runner->QuitClosure());
81 base::Closure
QuitClosure();
83 bool loop_running() const { return loop_running_
; }
86 friend class base::RefCounted
<MessageLoopRunner
>;
89 // True when the message loop is running.
92 // True after closure returned by |QuitClosure| has been called.
93 bool quit_closure_called_
;
95 base::RunLoop run_loop_
;
97 DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner
);
100 // A WindowedNotificationObserver allows code to wait until a condition is met.
101 // Simple conditions are specified by providing a |notification_type| and a
102 // |source|. When a notification of the expected type from the expected source
103 // is received, the condition is met.
104 // More complex conditions can be specified by providing a |notification_type|
105 // and a |callback|. The callback is called whenever the notification is fired.
106 // If the callback returns |true|, the condition is met. Otherwise, the
107 // condition is not yet met and the callback will be invoked again every time a
108 // notification of the expected type is received until the callback returns
109 // |true|. For convenience, two callback types are defined, one that is provided
110 // with the notification source and details, and one that is not.
112 // This helper class exists to avoid the following common pattern in tests:
114 // WaitForCompletionNotification()
115 // The pattern leads to flakiness as there is a window between PerformAction
116 // returning and the observers getting registered, where a notification will be
119 // Rather, one can do this:
120 // WindowedNotificationObserver signal(...)
123 class WindowedNotificationObserver
: public NotificationObserver
{
125 // Callback invoked on notifications. Should return |true| when the condition
126 // being waited for is met. For convenience, there is a choice between two
127 // callback types, one that is provided with the notification source and
128 // details, and one that is not.
129 typedef base::Callback
<bool(const NotificationSource
&,
130 const NotificationDetails
&)>
131 ConditionTestCallback
;
132 typedef base::Callback
<bool(void)>
133 ConditionTestCallbackWithoutSourceAndDetails
;
135 // Set up to wait for a simple condition. The condition is met when a
136 // notification of the given |notification_type| from the given |source| is
137 // received. To accept notifications from all sources, specify
138 // NotificationService::AllSources() as |source|.
139 WindowedNotificationObserver(int notification_type
,
140 const NotificationSource
& source
);
142 // Set up to wait for a complex condition. The condition is met when
143 // |callback| returns |true|. The callback is invoked whenever a notification
144 // of |notification_type| from any source is received.
145 WindowedNotificationObserver(int notification_type
,
146 const ConditionTestCallback
& callback
);
147 WindowedNotificationObserver(
148 int notification_type
,
149 const ConditionTestCallbackWithoutSourceAndDetails
& callback
);
151 ~WindowedNotificationObserver() override
;
153 // Adds an additional notification type to wait for. The condition will be met
154 // if any of the registered notification types from their respective sources
156 void AddNotificationType(int notification_type
,
157 const NotificationSource
& source
);
159 // Wait until the specified condition is met. If the condition is already met
160 // (that is, the expected notification has already been received or the
161 // given callback returns |true| already), Wait() returns immediately.
164 // Returns NotificationService::AllSources() if we haven't observed a
166 const NotificationSource
& source() const {
170 const NotificationDetails
& details() const {
174 // NotificationObserver:
175 void Observe(int type
,
176 const NotificationSource
& source
,
177 const NotificationDetails
& details
) override
;
182 NotificationRegistrar registrar_
;
184 ConditionTestCallback callback_
;
186 NotificationSource source_
;
187 NotificationDetails details_
;
188 scoped_refptr
<MessageLoopRunner
> message_loop_runner_
;
190 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver
);
193 // Unit tests can use code which runs in the utility process by having it run on
194 // an in-process utility thread. This eliminates having two code paths in
195 // production code to deal with unit tests, and also helps with the binary
196 // separation on Windows since chrome.dll doesn't need to call into Blink code
197 // for some utility code to handle the single process case.
198 // Include this class as a member variable in your test harness if you take
199 // advantage of this functionality to ensure that the in-process utility thread
200 // is torn down correctly. See http://crbug.com/316919 for more information.
201 // Note: this class should be declared after the TestBrowserThreadBundle and
202 // ShadowingAtExitManager (if it exists) as it will need to be run before they
204 class InProcessUtilityThreadHelper
: public BrowserChildProcessObserver
{
206 InProcessUtilityThreadHelper();
207 ~InProcessUtilityThreadHelper() override
;
210 void BrowserChildProcessHostConnected(const ChildProcessData
& data
) override
;
211 void BrowserChildProcessHostDisconnected(
212 const ChildProcessData
& data
) override
;
214 int child_thread_count_
;
215 scoped_refptr
<MessageLoopRunner
> runner_
;
217 DISALLOW_COPY_AND_ASSIGN(InProcessUtilityThreadHelper
);
220 } // namespace content
222 #endif // CONTENT_PUBLIC_TEST_TEST_UTILS_H_