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 CONTENT_PUBLIC_TEST_TEST_UTILS_H_ | 5 #ifndef CONTENT_PUBLIC_TEST_TEST_UTILS_H_ |
6 #define CONTENT_PUBLIC_TEST_TEST_UTILS_H_ | 6 #define CONTENT_PUBLIC_TEST_TEST_UTILS_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 // A WindowedNotificationObserver allows code to wait until a condition is met. | 94 // A WindowedNotificationObserver allows code to wait until a condition is met. |
95 // Simple conditions are specified by providing a |notification_type| and a | 95 // Simple conditions are specified by providing a |notification_type| and a |
96 // |source|. When a notification of the expected type from the expected source | 96 // |source|. When a notification of the expected type from the expected source |
97 // is received, the condition is met. | 97 // is received, the condition is met. |
98 // More complex conditions can be specified by providing a |notification_type| | 98 // More complex conditions can be specified by providing a |notification_type| |
99 // and a |callback|. The callback is called whenever the notification is fired. | 99 // and a |callback|. The callback is called whenever the notification is fired. |
100 // If the callback returns |true|, the condition is met. Otherwise, the | 100 // If the callback returns |true|, the condition is met. Otherwise, the |
101 // condition is not yet met and the callback will be invoked again every time a | 101 // condition is not yet met and the callback will be invoked again every time a |
102 // notification of the expected type is received until the callback returns | 102 // notification of the expected type is received until the callback returns |
103 // |true|. | 103 // |true|. For convenience, two callback types are defined, one that is provided |
| 104 // with the notification source and details, and one that is not. |
104 // | 105 // |
105 // This helper class exists to avoid the following common pattern in tests: | 106 // This helper class exists to avoid the following common pattern in tests: |
106 // PerformAction() | 107 // PerformAction() |
107 // WaitForCompletionNotification() | 108 // WaitForCompletionNotification() |
108 // The pattern leads to flakiness as there is a window between PerformAction | 109 // The pattern leads to flakiness as there is a window between PerformAction |
109 // returning and the observers getting registered, where a notification will be | 110 // returning and the observers getting registered, where a notification will be |
110 // missed. | 111 // missed. |
111 // | 112 // |
112 // Rather, one can do this: | 113 // Rather, one can do this: |
113 // WindowedNotificationObserver signal(...) | 114 // WindowedNotificationObserver signal(...) |
114 // PerformAction() | 115 // PerformAction() |
115 // signal.Wait() | 116 // signal.Wait() |
116 class WindowedNotificationObserver : public NotificationObserver { | 117 class WindowedNotificationObserver : public NotificationObserver { |
117 public: | 118 public: |
118 // Callback invoked on notifications. Should return |true| when the condition | 119 // Callback invoked on notifications. Should return |true| when the condition |
119 // being waited for is met. | 120 // being waited for is met. For convenience, there is a choice between two |
120 typedef base::Callback<bool(void)> ConditionTestCallback; | 121 // callback types, one that is provided with the notification source and |
| 122 // details, and one that is not. |
| 123 typedef base::Callback<bool(const NotificationSource&, |
| 124 const NotificationDetails&)> |
| 125 ConditionTestCallback; |
| 126 typedef base::Callback<bool(void)> |
| 127 ConditionTestCallbackWithoutSourceAndDetails; |
121 | 128 |
122 // Set up to wait for a simple condition. The condition is met when a | 129 // Set up to wait for a simple condition. The condition is met when a |
123 // notification of the given |notification_type| from the given |source| is | 130 // notification of the given |notification_type| from the given |source| is |
124 // received. To accept notifications from all sources, specify | 131 // received. To accept notifications from all sources, specify |
125 // NotificationService::AllSources() as |source|. | 132 // NotificationService::AllSources() as |source|. |
126 WindowedNotificationObserver(int notification_type, | 133 WindowedNotificationObserver(int notification_type, |
127 const NotificationSource& source); | 134 const NotificationSource& source); |
128 | 135 |
129 // Set up to wait for a complex condition. The condition is met when | 136 // Set up to wait for a complex condition. The condition is met when |
130 // |callback| returns |true|. The callback is invoked whenever a notification | 137 // |callback| returns |true|. The callback is invoked whenever a notification |
131 // of |notification_type| from any source is received. | 138 // of |notification_type| from any source is received. |
132 WindowedNotificationObserver(int notification_type, | 139 WindowedNotificationObserver(int notification_type, |
133 const ConditionTestCallback& callback); | 140 const ConditionTestCallback& callback); |
| 141 WindowedNotificationObserver( |
| 142 int notification_type, |
| 143 const ConditionTestCallbackWithoutSourceAndDetails& callback); |
134 | 144 |
135 virtual ~WindowedNotificationObserver(); | 145 virtual ~WindowedNotificationObserver(); |
136 | 146 |
137 // Adds an additional notification type to wait for. The condition will be met | 147 // Adds an additional notification type to wait for. The condition will be met |
138 // if any of the registered notification types from their respective sources | 148 // if any of the registered notification types from their respective sources |
139 // is received. | 149 // is received. |
140 void AddNotificationType(int notification_type, | 150 void AddNotificationType(int notification_type, |
141 const NotificationSource& source); | 151 const NotificationSource& source); |
142 | 152 |
143 // Wait until the specified condition is met. If the condition is already met | 153 // Wait until the specified condition is met. If the condition is already met |
(...skipping 26 matching lines...) Expand all Loading... |
170 NotificationSource source_; | 180 NotificationSource source_; |
171 NotificationDetails details_; | 181 NotificationDetails details_; |
172 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 182 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
173 | 183 |
174 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); | 184 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); |
175 }; | 185 }; |
176 | 186 |
177 } // namespace content | 187 } // namespace content |
178 | 188 |
179 #endif // CONTENT_PUBLIC_TEST_TEST_UTILS_H_ | 189 #endif // CONTENT_PUBLIC_TEST_TEST_UTILS_H_ |
OLD | NEW |