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_NOTIFICATION_TRACKER_H_ 6 #define CONTENT_PUBLIC_TEST_TEST_NOTIFICATION_TRACKER_H_ 7 8 #include <vector> 9 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" 15 16 namespace content { 17 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); 26 27 int type; 28 NotificationSource source; 29 NotificationDetails details; 30 }; 31 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(); 35 36 virtual ~TestNotificationTracker(); 37 38 // Makes this object listen for the given notification with the given source. 39 void ListenFor(int type, const NotificationSource& source); 40 41 // Makes this object listen for notifications of the given type coming from 42 // any source. 43 void ListenForAll(int type); 44 45 // Clears the list of events. 46 void Reset(); 47 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. 50 // 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); 63 64 // Returns the number of notifications received since the last reset. size()65 size_t size() const { return events_.size(); } 66 67 // Returns the information about the event at the given index. The index must 68 // be in [0, size). at(size_t i)69 const Event& at(size_t i) const { return events_[i]; } 70 71 protected: 72 virtual void Observe(int type, 73 const NotificationSource& source, 74 const NotificationDetails& details) OVERRIDE; 75 private: 76 NotificationRegistrar registrar_; 77 78 // Lists all received since last cleared, in the order they were received. 79 std::vector<Event> events_; 80 81 DISALLOW_COPY_AND_ASSIGN(TestNotificationTracker); 82 }; 83 84 } // namespace content 85 86 #endif // CONTENT_PUBLIC_TEST_TEST_NOTIFICATION_TRACKER_H_ 87