OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 CHROME_TEST_TEST_NOTIFICATION_TRACKER_H_ | |
6 #define CHROME_TEST_TEST_NOTIFICATION_TRACKER_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 | |
11 #include "content/common/notification_details.h" | |
12 #include "content/common/notification_observer.h" | |
13 #include "content/common/notification_registrar.h" | |
14 #include "content/common/notification_source.h" | |
15 | |
16 // Provides an easy way for tests to verify that a given set of notifications | |
17 // was received during test execution. | |
18 class TestNotificationTracker : public NotificationObserver { | |
19 public: | |
20 // Records one received notification. | |
21 struct Event { | |
22 Event(); | |
23 Event(int t, NotificationSource s, NotificationDetails d); | |
24 | |
25 int type; | |
26 NotificationSource source; | |
27 NotificationDetails details; | |
28 }; | |
29 | |
30 // By default, it won't listen for any notifications. You'll need to call | |
31 // ListenFor for the notifications you are interested in. | |
32 TestNotificationTracker(); | |
33 | |
34 virtual ~TestNotificationTracker(); | |
35 | |
36 // Makes this object listen for the given notification with the given source. | |
37 void ListenFor(int type, const NotificationSource& source); | |
38 | |
39 // Makes this object listen for notifications of the given type coming from | |
40 // any source. | |
41 void ListenForAll(int type); | |
42 | |
43 // Clears the list of events. | |
44 void Reset(); | |
45 | |
46 // Given notifications type(sp, returns true if the list of notifications | |
47 // were exactly those listed in the given arg(s), and in the same order. | |
48 // | |
49 // This will also reset the list so that the next call will only check for | |
50 // new notifications. Example: | |
51 // <do stuff> | |
52 // Check1AndReset(NOTIFY_A); | |
53 // <do stuff> | |
54 // Check2AndReset(NOTIFY_B, NOTIFY_C) | |
55 bool Check1AndReset(int type); | |
56 bool Check2AndReset(int type1, | |
57 int type2); | |
58 bool Check3AndReset(int type1, | |
59 int type2, | |
60 int type3); | |
61 | |
62 // Returns the number of notifications received since the last reset. | |
63 size_t size() const { return events_.size(); } | |
64 | |
65 // Returns the information about the event at the given index. The index must | |
66 // be in [0, size). | |
67 const Event& at(size_t i) const { return events_[i]; } | |
68 | |
69 protected: | |
70 virtual void Observe(int type, | |
71 const NotificationSource& source, | |
72 const NotificationDetails& details); | |
73 private: | |
74 NotificationRegistrar registrar_; | |
75 | |
76 // Lists all received since last cleared, in the order they were received. | |
77 std::vector<Event> events_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(TestNotificationTracker); | |
80 }; | |
81 | |
82 #endif // CHROME_TEST_TEST_NOTIFICATION_TRACKER_H_ | |
OLD | NEW |