1 // Copyright 2014 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 #include "base/memory/scoped_ptr.h"
6 #include "base/prefs/testing_pref_service.h"
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/test_timeouts.h"
10 #include "base/values.h"
11 #include "chrome/browser/notifications/message_center_notification_manager.h"
12 #include "chrome/browser/notifications/notification.h"
13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/scoped_testing_local_state.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chrome/test/base/testing_profile_manager.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/message_center/fake_message_center_tray_delegate.h"
23 #include "ui/message_center/fake_notifier_settings_provider.h"
24 #include "ui/message_center/message_center_impl.h"
25 #include "ui/message_center/message_center_tray.h"
26 #include "ui/message_center/message_center_types.h"
27 #include "ui/message_center/notifier_settings.h"
29 #if defined(OS_CHROMEOS)
30 #include "chrome/browser/ui/ash/multi_user/multi_user_notification_blocker_chromeos.h"
31 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
32 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
33 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
36 namespace message_center
{
38 class MessageCenterNotificationManagerTest
: public BrowserWithTestWindowTest
{
40 MessageCenterNotificationManagerTest() {}
43 void SetUp() override
{
44 BrowserWithTestWindowTest::SetUp();
45 #if !defined(OS_CHROMEOS)
46 // BrowserWithTestWindowTest owns an AshTestHelper on OS_CHROMEOS, which
47 // in turn initializes the message center. On other platforms, we need to
48 // initialize it here.
49 MessageCenter::Initialize();
53 // Clear the preference and initialize.
54 TestingBrowserProcess
* browser_process
= TestingBrowserProcess::GetGlobal();
55 profile_manager_
.reset(new TestingProfileManager(browser_process
));
56 ASSERT_TRUE(profile_manager_
->SetUp());
57 local_state()->ClearPref(prefs::kMessageCenterShowedFirstRunBalloon
);
58 first_run_pref_
.reset(new BooleanPrefMember
);
59 first_run_pref_
->Init(prefs::kMessageCenterShowedFirstRunBalloon
,
62 // Get ourselves a run loop.
63 run_loop_
.reset(new base::RunLoop());
65 message_center_
= MessageCenter::Get();
66 scoped_ptr
<NotifierSettingsProvider
> settings_provider(
67 new FakeNotifierSettingsProvider(notifiers_
));
68 delegate_
= new FakeMessageCenterTrayDelegate(message_center_
,
69 run_loop_
->QuitClosure());
70 notification_manager()->SetMessageCenterTrayDelegateForTest(delegate_
);
72 // First run features are only implemented on Windows, where the
73 // notification center is hard to find.
74 notification_manager()->SetFirstRunTimeoutForTest(
75 TestTimeouts::tiny_timeout());
79 void TearDown() override
{
81 first_run_pref_
.reset();
82 profile_manager_
.reset();
84 #if !defined(OS_CHROMEOS)
85 // Shutdown the message center if we initialized it manually.
86 MessageCenter::Shutdown();
89 BrowserWithTestWindowTest::TearDown();
92 MessageCenterNotificationManager
* notification_manager() {
93 return (MessageCenterNotificationManager
*)
94 g_browser_process
->notification_ui_manager();
97 FakeMessageCenterTrayDelegate
* delegate() { return delegate_
; }
99 MessageCenter
* message_center() { return message_center_
; }
101 const ::Notification
GetANotification(const std::string
& id
) {
102 return ::Notification(
103 GURL("chrome-extension://adflkjsdflkdsfdsflkjdsflkdjfs"),
109 new MockNotificationDelegate(id
));
112 base::RunLoop
* run_loop() { return run_loop_
.get(); }
113 PrefService
* local_state() {
114 return TestingBrowserProcess::GetGlobal()->local_state();
116 bool DidFirstRunPref() { return first_run_pref_
->GetValue(); }
119 scoped_ptr
<TestingProfileManager
> profile_manager_
;
120 scoped_ptr
<base::RunLoop
> run_loop_
;
121 MessageCenter
* message_center_
;
122 std::vector
<Notifier
*> notifiers_
;
123 FakeMessageCenterTrayDelegate
* delegate_
;
124 scoped_ptr
<BooleanPrefMember
> first_run_pref_
;
127 TEST_F(MessageCenterNotificationManagerTest
, SetupNotificationManager
) {
128 TestingProfile profile
;
129 notification_manager()->Add(GetANotification("test"), &profile
);
130 EXPECT_FALSE(DidFirstRunPref());
133 TEST_F(MessageCenterNotificationManagerTest
, UpdateNotification
) {
134 TestingProfile profile
;
135 EXPECT_TRUE(message_center()->NotificationCount() == 0);
136 notification_manager()->Add(GetANotification("test"), &profile
);
137 EXPECT_TRUE(message_center()->NotificationCount() == 1);
139 notification_manager()->Update(GetANotification("test"), &profile
));
140 EXPECT_TRUE(message_center()->NotificationCount() == 1);
143 #if defined(OS_CHROMEOS)
144 TEST_F(MessageCenterNotificationManagerTest
, MultiUserUpdates
) {
145 TestingProfile profile
;
146 std::string active_user_id
= multi_user_util::GetUserIDFromProfile(&profile
);
147 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager
=
148 new chrome::MultiUserWindowManagerChromeOS(active_user_id
);
149 multi_user_window_manager
->Init();
150 chrome::MultiUserWindowManager::SetInstanceForTest(
151 multi_user_window_manager
,
152 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
153 scoped_ptr
<MultiUserNotificationBlockerChromeOS
> blocker(
154 new MultiUserNotificationBlockerChromeOS(
155 message_center::MessageCenter::Get(),
157 EXPECT_EQ(0u, message_center()->NotificationCount());
158 notification_manager()->Add(GetANotification("test"), &profile
);
159 EXPECT_EQ(1u, message_center()->NotificationCount());
160 notification_manager()->Update(GetANotification("test"), &profile
);
161 EXPECT_EQ(1u, message_center()->NotificationCount());
162 chrome::MultiUserWindowManager::DeleteInstance();
167 // The following tests test the first run balloon, which is only implemented for
169 TEST_F(MessageCenterNotificationManagerTest
, FirstRunShown
) {
170 TestingProfile profile
;
171 notification_manager()->Add(GetANotification("test"), &profile
);
172 std::string notification_id
=
173 notification_manager()
174 ->FindById("test", NotificationUIManager::GetProfileID(&profile
))
176 message_center()->DisplayedNotification(
177 notification_id
, message_center::DISPLAY_SOURCE_MESSAGE_CENTER
);
178 message_center()->MarkSinglePopupAsShown(notification_id
, false);
181 base::RunLoop run_loop_2
;
182 run_loop_2
.RunUntilIdle();
183 EXPECT_TRUE(delegate()->displayed_first_run_balloon());
184 EXPECT_TRUE(DidFirstRunPref());
187 TEST_F(MessageCenterNotificationManagerTest
,
188 FirstRunNotShownWithPopupsVisible
) {
189 TestingProfile profile
;
190 notification_manager()->Add(GetANotification("test"), &profile
);
191 std::string notification_id
=
192 notification_manager()
193 ->FindById("test", NotificationUIManager::GetProfileID(&profile
))
195 message_center()->DisplayedNotification(
196 notification_id
, message_center::DISPLAY_SOURCE_MESSAGE_CENTER
);
197 run_loop()->RunUntilIdle();
198 EXPECT_FALSE(delegate()->displayed_first_run_balloon());
199 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
200 EXPECT_FALSE(DidFirstRunPref());
203 TEST_F(MessageCenterNotificationManagerTest
,
204 FirstRunNotShownWithMessageCenter
) {
205 TestingProfile profile
;
206 notification_manager()->Add(GetANotification("test"), &profile
);
207 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
208 run_loop()->RunUntilIdle();
209 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
210 EXPECT_FALSE(DidFirstRunPref());
214 } // namespace message_center