blob: 738bd8f7f94c0a89773c47e958e7627070655bc1 [file] [log] [blame]
Peter Beverloo14d352b2017-08-22 19:32:411// 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 Beverloob3310e62017-08-23 18:52:5410#include "base/callback_forward.h"
Evan Stade29036d52017-11-14 18:11:4211#include "base/optional.h"
Peter Beverloo1d480b2a2017-12-05 16:36:0012#include "base/strings/string16.h"
Peter Beverloo14d352b2017-08-22 19:32:4113#include "chrome/browser/notifications/notification_common.h"
Finnur Thorarinssond0ce7c52018-01-06 14:39:1214#include "chrome/browser/notifications/stub_notification_display_service.h"
Evan Stade7a379f72018-01-25 18:16:5215#include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
Peter Beverloo14d352b2017-08-22 19:32:4116
Peter Beverloo14d352b2017-08-22 19:32:4117class Profile;
Peter Beverloo14d352b2017-08-22 19:32:4118
Evan Stade4755cf22017-10-17 18:35:4319namespace message_center {
20class Notification;
21}
22
Peter Beverloo14d352b2017-08-22 19:32:4123// Helper class that enables use of the NotificationDisplayService in tests. The
Evan Stade7af71992018-01-20 02:03:0624// Profile* passed when constructing an instance may outlive this class.
Peter Beverloo14d352b2017-08-22 19:32:4125//
Evan Stade5d6e4692017-12-08 17:43:5226// This class must only be used for testing purposes. Unlike most production
27// NotificationDisplayService implementations, all operations on this tester are
28// synchronous.
Peter Beverloo14d352b2017-08-22 19:32:4129class NotificationDisplayServiceTester {
30 public:
Toni Barzicb598380e2018-11-26 19:14:1031 // |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 Beverloo14d352b2017-08-22 19:32:4134 explicit NotificationDisplayServiceTester(Profile* profile);
Sundoo Kimdd91d952020-08-26 17:11:2035 NotificationDisplayServiceTester(const NotificationDisplayServiceTester&) =
36 delete;
37 NotificationDisplayServiceTester& operator=(
38 const NotificationDisplayServiceTester&) = delete;
Peter Beverloo14d352b2017-08-22 19:32:4139 ~NotificationDisplayServiceTester();
40
Evan Stade29036d52017-11-14 18:11:4241 // Returns the currently active tester, if any.
42 static NotificationDisplayServiceTester* Get();
43
Peter Beverloob3310e62017-08-23 18:52:5444 // Sets |closure| to be invoked when any notification has been added.
45 void SetNotificationAddedClosure(base::RepeatingClosure closure);
46
Richard Knollb8e6c982019-04-16 09:07:0247 // Sets |closure| to be invoked when any notification has been closed.
48 void SetNotificationClosedClosure(base::RepeatingClosure closure);
49
Peter Beverloo14d352b2017-08-22 19:32:4150 // Synchronously gets a vector of the displayed Notifications for the |type|.
Evan Stade4755cf22017-10-17 18:35:4351 std::vector<message_center::Notification> GetDisplayedNotificationsForType(
Peter Beverloo6dba2e102017-11-23 17:46:3352 NotificationHandler::Type type);
Peter Beverloo14d352b2017-08-22 19:32:4153
Evan Stade4ebefdf42017-09-21 23:50:1854 const NotificationCommon::Metadata* GetMetadataForNotification(
Evan Stade4755cf22017-10-17 18:35:4355 const message_center::Notification& notification);
Evan Stade4ebefdf42017-09-21 23:50:1856
Evan Stade29036d52017-11-14 18:11:4257 base::Optional<message_center::Notification> GetNotification(
Evan Stade5bd7d0642017-12-11 18:12:3058 const std::string& notification_id) const;
Evan Stade29036d52017-11-14 18:11:4259
Peter Beverloo1d480b2a2017-12-05 16:36:0060 // 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 Beverloo14d352b2017-08-22 19:32:4172 // 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 Beverloo6dba2e102017-11-23 17:46:3377 void RemoveNotification(NotificationHandler::Type type,
Peter Beverloo14d352b2017-08-22 19:32:4178 const std::string& notification_id,
79 bool by_user,
80 bool silent = false);
81
82 // Removes all notifications of the given |type|.
Peter Beverloo6dba2e102017-11-23 17:46:3383 void RemoveAllNotifications(NotificationHandler::Type type, bool by_user);
Peter Beverloo14d352b2017-08-22 19:32:4184
Finnur Thorarinssond0ce7c52018-01-06 14:39:1285 // Sets a |delegate| to notify when ProcessNotificationOperation is called.
86 void SetProcessNotificationOperationDelegate(
87 const StubNotificationDisplayService::
88 ProcessNotificationOperationCallback& delegate);
89
Peter Beverloo14d352b2017-08-22 19:32:4190 private:
Evan Stade7a379f72018-01-25 18:16:5291 void OnProfileShutdown();
92
Peter Beverloo14d352b2017-08-22 19:32:4193 Profile* profile_;
94 StubNotificationDisplayService* display_service_;
Peter Kasting7ba9440c2020-11-22 01:49:0295 base::CallbackListSubscription profile_shutdown_subscription_;
Peter Beverloo14d352b2017-08-22 19:32:4196};
97
98#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_TESTER_H_