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.
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
11 #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/sync_notifier/chrome_notifier_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "ui/gfx/size.h"
18 // NotificationDelegate which does nothing, useful for testing when
19 // the notification events are not important.
20 class MockNotificationDelegate
: public NotificationDelegate
{
22 explicit MockNotificationDelegate(const std::string
& id
);
24 // NotificationDelegate interface.
25 virtual void Display() override
{}
26 virtual void Error() override
{}
27 virtual void Close(bool by_user
) override
{}
28 virtual void Click() override
{}
29 virtual std::string
id() const override
;
32 virtual ~MockNotificationDelegate();
36 DISALLOW_COPY_AND_ASSIGN(MockNotificationDelegate
);
39 // Mock implementation of Javascript object proxy which logs events that
40 // would have been fired on it. Useful for tests where the sequence of
41 // notification events needs to be verified.
43 // |Logger| class provided in template must implement method
44 // static void log(string);
45 template<class Logger
>
46 class LoggingNotificationDelegate
: public NotificationDelegate
{
48 explicit LoggingNotificationDelegate(std::string id
) : notification_id_(id
) {}
50 // NotificationObjectProxy override
51 virtual void Display() override
{
52 Logger::log("notification displayed\n");
54 virtual void Error() override
{
55 Logger::log("notification error\n");
57 virtual void Click() override
{
58 Logger::log("notification clicked\n");
60 virtual void ButtonClick(int index
) override
{
61 Logger::log("notification button clicked\n");
63 virtual void Close(bool by_user
) override
{
65 Logger::log("notification closed by user\n");
67 Logger::log("notification closed by script\n");
69 virtual std::string
id() const override
{
70 return notification_id_
;
74 std::string notification_id_
;
76 DISALLOW_COPY_AND_ASSIGN(LoggingNotificationDelegate
);
79 class StubNotificationUIManager
: public NotificationUIManager
{
81 explicit StubNotificationUIManager(const GURL
& welcome_origin
);
82 virtual ~StubNotificationUIManager();
84 // Adds a notification to be displayed. Virtual for unit test override.
85 virtual void Add(const Notification
& notification
, Profile
* profile
) override
;
86 virtual bool Update(const Notification
& notification
,
87 Profile
* profile
) override
;
89 // Returns NULL if no notifications match the supplied ID, either currently
90 // displayed or in the queue.
91 virtual const Notification
* FindById(const std::string
& delegate_id
,
92 ProfileID profile_id
) const override
;
94 // Removes any notifications matching the supplied ID, either currently
95 // displayed or in the queue. Returns true if anything was removed.
96 virtual bool CancelById(const std::string
& delegate_id
,
97 ProfileID profile_id
) override
;
99 // Adds the delegate_id for each outstanding notification to the set
100 // |delegate_ids| (must not be NULL).
101 virtual std::set
<std::string
> GetAllIdsByProfileAndSourceOrigin(
103 const GURL
& source
) override
;
105 // Removes notifications matching the |source_origin| (which could be an
106 // extension ID). Returns true if anything was removed.
107 virtual bool CancelAllBySourceOrigin(const GURL
& source_origin
) override
;
109 // Removes notifications matching |profile|. Returns true if any were removed.
110 virtual bool CancelAllByProfile(ProfileID profile_id
) override
;
112 // Cancels all pending notifications and closes anything currently showing.
113 // Used when the app is terminating.
114 virtual void CancelAll() override
;
116 // Test hook to get the notification so we can check it
117 const Notification
& notification() const { return notification_
; }
119 // Test hook to check the ID of the last notification cancelled.
120 std::string
& dismissed_id() { return dismissed_id_
; }
122 size_t added_notifications() const { return added_notifications_
; }
123 bool welcomed() const { return welcomed_
; }
126 DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager
);
127 Notification notification_
;
129 std::string dismissed_id_
;
130 GURL welcome_origin_
;
132 size_t added_notifications_
;
135 #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_