Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / test_notification_tracker.h
blob786ec147e05ebc6c095f3cf3ca979f44c3822d05
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_NOTIFICATION_TRACKER_H_
6 #define CONTENT_PUBLIC_TEST_TEST_NOTIFICATION_TRACKER_H_
8 #include <vector>
10 #include "base/compiler_specific.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"
16 namespace content {
18 // Provides an easy way for tests to verify that a given set of notifications
19 // was received during test execution.
20 class TestNotificationTracker : public NotificationObserver {
21 public:
22 // Records one received notification.
23 struct Event {
24 Event();
25 Event(int t, NotificationSource s, NotificationDetails d);
27 int type;
28 NotificationSource source;
29 NotificationDetails details;
32 // By default, it won't listen for any notifications. You'll need to call
33 // ListenFor for the notifications you are interested in.
34 TestNotificationTracker();
36 ~TestNotificationTracker() override;
38 // Makes this object listen for the given notification with the given source.
39 void ListenFor(int type, const NotificationSource& source);
41 // Makes this object listen for notifications of the given type coming from
42 // any source.
43 void ListenForAll(int type);
45 // Clears the list of events.
46 void Reset();
48 // Given notifications type(sp, returns true if the list of notifications
49 // were exactly those listed in the given arg(s), and in the same order.
51 // This will also reset the list so that the next call will only check for
52 // new notifications. Example:
53 // <do stuff>
54 // Check1AndReset(NOTIFY_A);
55 // <do stuff>
56 // Check2AndReset(NOTIFY_B, NOTIFY_C)
57 bool Check1AndReset(int type);
58 bool Check2AndReset(int type1,
59 int type2);
60 bool Check3AndReset(int type1,
61 int type2,
62 int type3);
64 // Returns the number of notifications received since the last reset.
65 size_t size() const { return events_.size(); }
67 // Returns the information about the event at the given index. The index must
68 // be in [0, size).
69 const Event& at(size_t i) const { return events_[i]; }
71 protected:
72 void Observe(int type,
73 const NotificationSource& source,
74 const NotificationDetails& details) override;
76 private:
77 NotificationRegistrar registrar_;
79 // Lists all received since last cleared, in the order they were received.
80 std::vector<Event> events_;
82 DISALLOW_COPY_AND_ASSIGN(TestNotificationTracker);
85 } // namespace content
87 #endif // CONTENT_PUBLIC_TEST_TEST_NOTIFICATION_TRACKER_H_