| 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 #ifndef CONTENT_PUBLIC_TEST_TEST_UTILS_H_ |
| 6 #define CONTENT_PUBLIC_TEST_TEST_UTILS_H_ |
| 7 |
| 8 #include "base/callback_forward.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "content/public/browser/notification_details.h" |
| 12 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" |
| 14 #include "content/public/browser/notification_source.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 |
| 17 // A collections of functions designed for use with unit and browser tests. |
| 18 |
| 19 namespace content { |
| 20 |
| 21 // Variant of RunMessageLoop that takes RunLoop. |
| 22 void RunThisRunLoop(base::RunLoop* run_loop); |
| 23 |
| 24 // Get task to quit the given RunLoop. It allows a few generations of pending |
| 25 // tasks to run as opposed to run_loop->QuitClosure(). |
| 26 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop); |
| 27 |
| 28 // Helper class to Run and Quit the message loop. Run and Quit can only happen |
| 29 // once per instance. Make a new instance for each use. Calling Quit after Run |
| 30 // has returned is safe and has no effect. |
| 31 class MessageLoopRunner : public base::RefCounted<MessageLoopRunner> { |
| 32 public: |
| 33 MessageLoopRunner(); |
| 34 |
| 35 // Run the current MessageLoop. |
| 36 void Run(); |
| 37 |
| 38 // Quit the matching call to Run (nested MessageLoops are unaffected). |
| 39 void Quit(); |
| 40 |
| 41 // Hand this closure off to code that uses callbacks to notify completion. |
| 42 // Example: |
| 43 // scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 44 // kick_off_some_api(runner.QuitNowClosure()); |
| 45 // runner.Run(); |
| 46 base::Closure QuitClosure(); |
| 47 |
| 48 private: |
| 49 friend class base::RefCounted<MessageLoopRunner>; |
| 50 ~MessageLoopRunner(); |
| 51 |
| 52 base::RunLoop run_loop_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner); |
| 55 }; |
| 56 |
| 57 // A WindowedNotificationObserver allows code to watch for a notification |
| 58 // over a window of time. Typically testing code will need to do something |
| 59 // like this: |
| 60 // PerformAction() |
| 61 // WaitForCompletionNotification() |
| 62 // This leads to flakiness as there's a window between PerformAction returning |
| 63 // and the observers getting registered, where a notification will be missed. |
| 64 // |
| 65 // Rather, one can do this: |
| 66 // WindowedNotificationObserver signal(...) |
| 67 // PerformAction() |
| 68 // signal.Wait() |
| 69 class WindowedNotificationObserver : public NotificationObserver { |
| 70 public: |
| 71 // Register to listen for notifications of the given type from either a |
| 72 // specific source, or from all sources if |source| is |
| 73 // NotificationService::AllSources(). |
| 74 WindowedNotificationObserver(int notification_type, |
| 75 const NotificationSource& source); |
| 76 virtual ~WindowedNotificationObserver(); |
| 77 |
| 78 // Wait until the specified notification occurs. If the notification was |
| 79 // emitted between the construction of this object and this call then it |
| 80 // returns immediately. |
| 81 void Wait(); |
| 82 |
| 83 // Returns NotificationService::AllSources() if we haven't observed a |
| 84 // notification yet. |
| 85 const NotificationSource& source() const { |
| 86 return source_; |
| 87 } |
| 88 |
| 89 const NotificationDetails& details() const { |
| 90 return details_; |
| 91 } |
| 92 |
| 93 // NotificationObserver: |
| 94 virtual void Observe(int type, |
| 95 const NotificationSource& source, |
| 96 const NotificationDetails& details) OVERRIDE; |
| 97 |
| 98 private: |
| 99 bool seen_; |
| 100 bool running_; |
| 101 NotificationRegistrar registrar_; |
| 102 |
| 103 NotificationSource source_; |
| 104 NotificationDetails details_; |
| 105 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); |
| 108 }; |
| 109 |
| 110 } // namespace content |
| 111 |
| 112 #endif // CONTENT_PUBLIC_TEST_TEST_UTILS_H_ |
| OLD | NEW |