Chromium Code Reviews| 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 CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_ | 5 #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_ |
| 6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_ | 6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 10 | 12 |
| 11 #include "chrome/browser/notifications/notification.h" | 13 #include "chrome/browser/notifications/notification.h" |
| 12 #include "chrome/browser/notifications/notification_object_proxy.h" | |
| 13 #include "chrome/browser/notifications/notification_ui_manager.h" | 14 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 14 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" | 15 |
| 15 #include "chrome/browser/profiles/profile.h" | 16 class Profile; |
| 16 #include "ui/gfx/size.h" | |
| 17 | 17 |
| 18 // NotificationDelegate which does nothing, useful for testing when | 18 // NotificationDelegate which does nothing, useful for testing when |
| 19 // the notification events are not important. | 19 // the notification events are not important. |
| 20 class MockNotificationDelegate : public NotificationDelegate { | 20 class MockNotificationDelegate : public NotificationDelegate { |
| 21 public: | 21 public: |
| 22 explicit MockNotificationDelegate(const std::string& id); | 22 explicit MockNotificationDelegate(const std::string& id); |
| 23 | 23 |
| 24 // NotificationDelegate interface. | 24 // NotificationDelegate interface. |
| 25 std::string id() const override; | 25 std::string id() const override; |
| 26 | 26 |
| 27 private: | 27 private: |
| 28 ~MockNotificationDelegate() override; | 28 ~MockNotificationDelegate() override; |
| 29 | 29 |
| 30 std::string id_; | 30 std::string id_; |
| 31 | 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(MockNotificationDelegate); | 32 DISALLOW_COPY_AND_ASSIGN(MockNotificationDelegate); |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 // Mock implementation of Javascript object proxy which logs events that | |
| 36 // would have been fired on it. Useful for tests where the sequence of | |
| 37 // notification events needs to be verified. | |
| 38 // | |
| 39 // |Logger| class provided in template must implement method | |
| 40 // static void log(string); | |
| 41 template<class Logger> | |
| 42 class LoggingNotificationDelegate : public NotificationDelegate { | |
|
Peter Beverloo
2014/12/10 19:32:27
note: This was unused.
| |
| 43 public: | |
| 44 explicit LoggingNotificationDelegate(std::string id) : notification_id_(id) {} | |
| 45 | |
| 46 // NotificationObjectProxy override | |
| 47 virtual void Display() override { | |
| 48 Logger::log("notification displayed\n"); | |
| 49 } | |
| 50 virtual void Click() override { | |
| 51 Logger::log("notification clicked\n"); | |
| 52 } | |
| 53 virtual void ButtonClick(int index) override { | |
| 54 Logger::log("notification button clicked\n"); | |
| 55 } | |
| 56 virtual void Close(bool by_user) override { | |
| 57 if (by_user) | |
| 58 Logger::log("notification closed by user\n"); | |
| 59 else | |
| 60 Logger::log("notification closed by script\n"); | |
| 61 } | |
| 62 virtual std::string id() const override { | |
| 63 return notification_id_; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 std::string notification_id_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(LoggingNotificationDelegate); | |
| 70 }; | |
| 71 | |
| 72 class StubNotificationUIManager : public NotificationUIManager { | 35 class StubNotificationUIManager : public NotificationUIManager { |
|
Peter Beverloo
2014/12/10 19:32:27
note: This was unused, so I changed it to suit my
| |
| 73 public: | 36 public: |
| 74 explicit StubNotificationUIManager(const GURL& welcome_origin); | 37 StubNotificationUIManager(); |
| 75 ~StubNotificationUIManager() override; | 38 ~StubNotificationUIManager() override; |
| 76 | 39 |
| 77 // Adds a notification to be displayed. Virtual for unit test override. | 40 // Returns the number of currently active notifications. |
| 41 unsigned int GetNotificationCount() const; | |
| 42 | |
| 43 // Returns a reference to the notification at index |index|. | |
| 44 const Notification& GetNotificationAt(unsigned int index) const; | |
| 45 | |
| 46 // NotificationUIManager implementation. | |
| 78 void Add(const Notification& notification, Profile* profile) override; | 47 void Add(const Notification& notification, Profile* profile) override; |
| 79 bool Update(const Notification& notification, Profile* profile) override; | 48 bool Update(const Notification& notification, Profile* profile) override; |
| 80 | |
| 81 // Returns NULL if no notifications match the supplied ID, either currently | |
| 82 // displayed or in the queue. | |
| 83 const Notification* FindById(const std::string& delegate_id, | 49 const Notification* FindById(const std::string& delegate_id, |
| 84 ProfileID profile_id) const override; | 50 ProfileID profile_id) const override; |
| 85 | |
| 86 // Removes any notifications matching the supplied ID, either currently | |
| 87 // displayed or in the queue. Returns true if anything was removed. | |
| 88 bool CancelById(const std::string& delegate_id, | 51 bool CancelById(const std::string& delegate_id, |
| 89 ProfileID profile_id) override; | 52 ProfileID profile_id) override; |
| 90 | |
| 91 // Adds the delegate_id for each outstanding notification to the set | |
| 92 // |delegate_ids| (must not be NULL). | |
| 93 std::set<std::string> GetAllIdsByProfileAndSourceOrigin( | 53 std::set<std::string> GetAllIdsByProfileAndSourceOrigin( |
| 94 Profile* profile, | 54 Profile* profile, |
| 95 const GURL& source) override; | 55 const GURL& source) override; |
| 96 | |
| 97 // Removes notifications matching the |source_origin| (which could be an | |
| 98 // extension ID). Returns true if anything was removed. | |
| 99 bool CancelAllBySourceOrigin(const GURL& source_origin) override; | 56 bool CancelAllBySourceOrigin(const GURL& source_origin) override; |
| 100 | |
| 101 // Removes notifications matching |profile|. Returns true if any were removed. | |
| 102 bool CancelAllByProfile(ProfileID profile_id) override; | 57 bool CancelAllByProfile(ProfileID profile_id) override; |
| 103 | |
| 104 // Cancels all pending notifications and closes anything currently showing. | |
| 105 // Used when the app is terminating. | |
| 106 void CancelAll() override; | 58 void CancelAll() override; |
| 107 | 59 |
| 108 // Test hook to get the notification so we can check it | 60 private: |
| 109 const Notification& notification() const { return notification_; } | 61 using NotificationPair = std::pair<Notification, ProfileID>; |
| 62 std::vector<NotificationPair> notifications_; | |
| 110 | 63 |
| 111 // Test hook to check the ID of the last notification cancelled. | |
| 112 std::string& dismissed_id() { return dismissed_id_; } | |
| 113 | |
| 114 size_t added_notifications() const { return added_notifications_; } | |
| 115 bool welcomed() const { return welcomed_; } | |
| 116 | |
| 117 private: | |
| 118 DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager); | 64 DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager); |
| 119 Notification notification_; | |
| 120 Profile* profile_; | |
| 121 std::string dismissed_id_; | |
| 122 GURL welcome_origin_; | |
| 123 bool welcomed_; | |
| 124 size_t added_notifications_; | |
| 125 }; | 65 }; |
| 126 | 66 |
| 127 #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_ | 67 #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_ |
| OLD | NEW |