Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 1 | // Copyright 2017 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_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_TESTER_H_ |
| 6 | #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_TESTER_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
Peter Beverloo | b3310e6 | 2017-08-23 18:52:54 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
Evan Stade | 29036d5 | 2017-11-14 18:11:42 | [diff] [blame] | 11 | #include "base/optional.h" |
Peter Beverloo | 1d480b2a | 2017-12-05 16:36:00 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 13 | #include "chrome/browser/notifications/notification_common.h" |
Finnur Thorarinsson | d0ce7c5 | 2018-01-06 14:39:12 | [diff] [blame] | 14 | #include "chrome/browser/notifications/stub_notification_display_service.h" |
Evan Stade | 7a379f7 | 2018-01-25 18:16:52 | [diff] [blame] | 15 | #include "components/keyed_service/core/keyed_service_shutdown_notifier.h" |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 16 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 17 | class Profile; |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 18 | |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 19 | namespace message_center { |
| 20 | class Notification; |
| 21 | } |
| 22 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 23 | // Helper class that enables use of the NotificationDisplayService in tests. The |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 24 | // Profile* passed when constructing an instance may outlive this class. |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 25 | // |
Evan Stade | 5d6e469 | 2017-12-08 17:43:52 | [diff] [blame] | 26 | // This class must only be used for testing purposes. Unlike most production |
| 27 | // NotificationDisplayService implementations, all operations on this tester are |
| 28 | // synchronous. |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 29 | class NotificationDisplayServiceTester { |
| 30 | public: |
Toni Barzic | b598380e | 2018-11-26 19:14:10 | [diff] [blame] | 31 | // |profile| - the profile with which |display_service_| will be associated. |
| 32 | // It can be nullptr, in which case |display_service_| will be set up as a |
| 33 | // system notification display service. |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 34 | explicit NotificationDisplayServiceTester(Profile* profile); |
Sundoo Kim | dd91d95 | 2020-08-26 17:11:20 | [diff] [blame] | 35 | NotificationDisplayServiceTester(const NotificationDisplayServiceTester&) = |
| 36 | delete; |
| 37 | NotificationDisplayServiceTester& operator=( |
| 38 | const NotificationDisplayServiceTester&) = delete; |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 39 | ~NotificationDisplayServiceTester(); |
| 40 | |
Evan Stade | 29036d5 | 2017-11-14 18:11:42 | [diff] [blame] | 41 | // Returns the currently active tester, if any. |
| 42 | static NotificationDisplayServiceTester* Get(); |
| 43 | |
Peter Beverloo | b3310e6 | 2017-08-23 18:52:54 | [diff] [blame] | 44 | // Sets |closure| to be invoked when any notification has been added. |
| 45 | void SetNotificationAddedClosure(base::RepeatingClosure closure); |
| 46 | |
Richard Knoll | b8e6c98 | 2019-04-16 09:07:02 | [diff] [blame] | 47 | // Sets |closure| to be invoked when any notification has been closed. |
| 48 | void SetNotificationClosedClosure(base::RepeatingClosure closure); |
| 49 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 50 | // Synchronously gets a vector of the displayed Notifications for the |type|. |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 51 | std::vector<message_center::Notification> GetDisplayedNotificationsForType( |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 52 | NotificationHandler::Type type); |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 53 | |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 54 | const NotificationCommon::Metadata* GetMetadataForNotification( |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 55 | const message_center::Notification& notification); |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 56 | |
Evan Stade | 29036d5 | 2017-11-14 18:11:42 | [diff] [blame] | 57 | base::Optional<message_center::Notification> GetNotification( |
Evan Stade | 5bd7d064 | 2017-12-11 18:12:30 | [diff] [blame] | 58 | const std::string& notification_id) const; |
Evan Stade | 29036d5 | 2017-11-14 18:11:42 | [diff] [blame] | 59 | |
Peter Beverloo | 1d480b2a | 2017-12-05 16:36:00 | [diff] [blame] | 60 | // Simulates the notification identified by |notification_id| being clicked |
| 61 | // on, optionally with the given |action_index| and |reply|. |
| 62 | void SimulateClick(NotificationHandler::Type notification_type, |
| 63 | const std::string& notification_id, |
| 64 | base::Optional<int> action_index, |
| 65 | base::Optional<base::string16> reply); |
| 66 | |
| 67 | // Simulates a click on the settings button of the notification identified by |
| 68 | // |notification_id|. |
| 69 | void SimulateSettingsClick(NotificationHandler::Type notification_type, |
| 70 | const std::string& notification_id); |
| 71 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 72 | // Simulates the notification identified by |notification_id| being closed due |
| 73 | // to external events, such as the user dismissing it when |by_user| is set. |
| 74 | // When |silent| is set, the notification handlers won't be informed of the |
| 75 | // change to immitate behaviour of operating systems that don't inform apps |
| 76 | // about removed notifications. |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 77 | void RemoveNotification(NotificationHandler::Type type, |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 78 | const std::string& notification_id, |
| 79 | bool by_user, |
| 80 | bool silent = false); |
| 81 | |
| 82 | // Removes all notifications of the given |type|. |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 83 | void RemoveAllNotifications(NotificationHandler::Type type, bool by_user); |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 84 | |
Finnur Thorarinsson | d0ce7c5 | 2018-01-06 14:39:12 | [diff] [blame] | 85 | // Sets a |delegate| to notify when ProcessNotificationOperation is called. |
| 86 | void SetProcessNotificationOperationDelegate( |
| 87 | const StubNotificationDisplayService:: |
| 88 | ProcessNotificationOperationCallback& delegate); |
| 89 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 90 | private: |
Evan Stade | 7a379f7 | 2018-01-25 18:16:52 | [diff] [blame] | 91 | void OnProfileShutdown(); |
| 92 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 93 | Profile* profile_; |
| 94 | StubNotificationDisplayService* display_service_; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame^] | 95 | base::CallbackListSubscription profile_shutdown_subscription_; |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_TESTER_H_ |