blob: fabd936d970e8caca87c83fc9959f62253e6dd5f [file] [log] [blame]
peter28d8b732017-04-11 17:36:171// 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_STUB_NOTIFICATION_DISPLAY_SERVICE_H_
6#define CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_DISPLAY_SERVICE_H_
7
8#include <memory>
9#include <utility>
10#include <vector>
11
Peter Beverloob3310e62017-08-23 18:52:5412#include "base/callback.h"
peter28d8b732017-04-11 17:36:1713#include "base/macros.h"
Evan Stade29036d52017-11-14 18:11:4214#include "base/optional.h"
peter28d8b732017-04-11 17:36:1715#include "chrome/browser/notifications/notification_common.h"
16#include "chrome/browser/notifications/notification_display_service.h"
Evan Stade4755cf22017-10-17 18:35:4317#include "ui/message_center/notification.h"
peter28d8b732017-04-11 17:36:1718
miguelgabf610a2017-06-02 17:31:4319class Profile;
20
peter28d8b732017-04-11 17:36:1721// Implementation of the NotificationDisplayService interface that can be used
22// for testing purposes. Supports additional methods enabling instrumenting the
23// faked underlying notification system.
24class StubNotificationDisplayService : public NotificationDisplayService {
25 public:
26 // Factory function to be used with NotificationDisplayServiceFactory's
27 // SetTestingFactory method, overriding the default display service.
28 static std::unique_ptr<KeyedService> FactoryForTests(
29 content::BrowserContext* browser_context);
30
miguelgabf610a2017-06-02 17:31:4331 explicit StubNotificationDisplayService(Profile* profile);
peter28d8b732017-04-11 17:36:1732 ~StubNotificationDisplayService() override;
33
Peter Beverloob3310e62017-08-23 18:52:5434 // Sets |closure| to be invoked when any notification has been added.
35 void SetNotificationAddedClosure(base::RepeatingClosure closure);
36
Peter Beverloof91afc842017-08-14 16:09:5037 // Returns a vector of the displayed Notification objects.
Evan Stade4755cf22017-10-17 18:35:4338 std::vector<message_center::Notification> GetDisplayedNotificationsForType(
Peter Beverloof91afc842017-08-14 16:09:5039 NotificationCommon::Type type) const;
40
Evan Stade29036d52017-11-14 18:11:4241 base::Optional<message_center::Notification> GetNotification(
42 const std::string& notification_id);
43
Evan Stade4ebefdf42017-09-21 23:50:1844 const NotificationCommon::Metadata* GetMetadataForNotification(
Evan Stade4755cf22017-10-17 18:35:4345 const message_center::Notification& notification);
Evan Stade4ebefdf42017-09-21 23:50:1846
Peter Beverloo14d352b2017-08-22 19:32:4147 // Simulates the notification identified by |notification_id| being closed due
48 // to external events, such as the user dismissing it when |by_user| is set.
49 // When |silent| is set, the notification handlers won't be informed of the
50 // change to immitate behaviour of operating systems that don't inform apps
51 // about removed notifications.
peter28d8b732017-04-11 17:36:1752 void RemoveNotification(NotificationCommon::Type notification_type,
53 const std::string& notification_id,
Peter Beverloof91afc842017-08-14 16:09:5054 bool by_user,
Peter Beverloo14d352b2017-08-22 19:32:4155 bool silent);
peter28d8b732017-04-11 17:36:1756
57 // Removes all notifications shown by this display service.
58 void RemoveAllNotifications(NotificationCommon::Type notification_type,
59 bool by_user);
60
61 // NotificationDisplayService implementation:
62 void Display(NotificationCommon::Type notification_type,
Evan Stade4755cf22017-10-17 18:35:4363 const message_center::Notification& notification,
Evan Stade4ebefdf42017-09-21 23:50:1864 std::unique_ptr<NotificationCommon::Metadata> metadata) override;
peter28d8b732017-04-11 17:36:1765 void Close(NotificationCommon::Type notification_type,
66 const std::string& notification_id) override;
peterb5298ae2017-04-11 21:52:5167 void GetDisplayed(const DisplayedNotificationsCallback& callback) override;
peter28d8b732017-04-11 17:36:1768
69 private:
70 // Data to store for a notification that's being shown through this service.
Evan Stade4ebefdf42017-09-21 23:50:1871 struct NotificationData {
72 NotificationData(NotificationCommon::Type type,
Evan Stade4755cf22017-10-17 18:35:4373 const message_center::Notification& notification,
Evan Stade4ebefdf42017-09-21 23:50:1874 std::unique_ptr<NotificationCommon::Metadata> metadata);
75 NotificationData(NotificationData&& other);
76 ~NotificationData();
77
78 NotificationData& operator=(NotificationData&& other);
79
80 NotificationCommon::Type type;
Evan Stade4755cf22017-10-17 18:35:4381 message_center::Notification notification;
Evan Stade4ebefdf42017-09-21 23:50:1882 std::unique_ptr<NotificationCommon::Metadata> metadata;
83 };
peter28d8b732017-04-11 17:36:1784
Peter Beverloob3310e62017-08-23 18:52:5485 base::RepeatingClosure notification_added_closure_;
peter28d8b732017-04-11 17:36:1786 std::vector<NotificationData> notifications_;
miguelg30dfc2e2017-06-14 18:18:0987 Profile* profile_;
peter28d8b732017-04-11 17:36:1788
89 DISALLOW_COPY_AND_ASSIGN(StubNotificationDisplayService);
90};
91
92#endif // CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_DISPLAY_SERVICE_H_