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_
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"
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
{
22 // Records one received notification.
25 Event(int t
, NotificationSource s
, NotificationDetails d
);
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
43 void ListenForAll(int type
);
45 // Clears the list of events.
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:
54 // Check1AndReset(NOTIFY_A);
56 // Check2AndReset(NOTIFY_B, NOTIFY_C)
57 bool Check1AndReset(int type
);
58 bool Check2AndReset(int type1
,
60 bool Check3AndReset(int type1
,
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
69 const Event
& at(size_t i
) const { return events_
[i
]; }
72 void Observe(int type
,
73 const NotificationSource
& source
,
74 const NotificationDetails
& details
) override
;
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_