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/strings/utf_string_conversions.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/values.h"
10 #include "chrome/browser/notifications/message_center_notification_manager.h"
11 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/notifications/notification_test_util.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "chrome/test/base/scoped_testing_local_state.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/message_center/fake_message_center_tray_delegate.h"
22 #include "ui/message_center/message_center_impl.h"
23 #include "ui/message_center/message_center_tray.h"
24 #include "ui/message_center/message_center_types.h"
25 #include "ui/message_center/notifier_settings.h"
27 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/ui/ash/multi_user/multi_user_notification_blocker_chromeos.h"
29 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
30 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
31 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
34 namespace message_center
{
36 class MessageCenterNotificationManagerTest
: public BrowserWithTestWindowTest
{
38 MessageCenterNotificationManagerTest() {}
41 void SetUp() override
{
42 BrowserWithTestWindowTest::SetUp();
43 #if !defined(OS_CHROMEOS)
44 // BrowserWithTestWindowTest owns an AshTestHelper on OS_CHROMEOS, which
45 // in turn initializes the message center. On other platforms, we need to
46 // initialize it here.
47 MessageCenter::Initialize();
51 TestingBrowserProcess
* browser_process
= TestingBrowserProcess::GetGlobal();
52 profile_manager_
.reset(new TestingProfileManager(browser_process
));
53 ASSERT_TRUE(profile_manager_
->SetUp());
55 message_center_
= MessageCenter::Get();
56 delegate_
= new FakeMessageCenterTrayDelegate(message_center_
);
57 notification_manager()->SetMessageCenterTrayDelegateForTest(delegate_
);
60 void TearDown() override
{
61 profile_manager_
.reset();
63 #if !defined(OS_CHROMEOS)
64 // Shutdown the message center if we initialized it manually.
65 MessageCenter::Shutdown();
68 BrowserWithTestWindowTest::TearDown();
71 MessageCenterNotificationManager
* notification_manager() {
72 return (MessageCenterNotificationManager
*)
73 g_browser_process
->notification_ui_manager();
76 MessageCenter
* message_center() { return message_center_
; }
78 const ::Notification
GetANotification(const std::string
& id
) {
79 return ::Notification(
80 GURL("chrome-extension://adflkjsdflkdsfdsflkjdsflkdjfs"),
86 new MockNotificationDelegate(id
));
90 scoped_ptr
<TestingProfileManager
> profile_manager_
;
91 MessageCenter
* message_center_
;
92 FakeMessageCenterTrayDelegate
* delegate_
;
95 TEST_F(MessageCenterNotificationManagerTest
, SetupNotificationManager
) {
96 TestingProfile profile
;
97 notification_manager()->Add(GetANotification("test"), &profile
);
100 TEST_F(MessageCenterNotificationManagerTest
, UpdateNotification
) {
101 TestingProfile profile
;
102 EXPECT_TRUE(message_center()->NotificationCount() == 0);
103 notification_manager()->Add(GetANotification("test"), &profile
);
104 EXPECT_TRUE(message_center()->NotificationCount() == 1);
106 notification_manager()->Update(GetANotification("test"), &profile
));
107 EXPECT_TRUE(message_center()->NotificationCount() == 1);
110 #if defined(OS_CHROMEOS)
111 TEST_F(MessageCenterNotificationManagerTest
, MultiUserUpdates
) {
112 TestingProfile profile
;
113 std::string active_user_id
= multi_user_util::GetUserIDFromProfile(&profile
);
114 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager
=
115 new chrome::MultiUserWindowManagerChromeOS(active_user_id
);
116 multi_user_window_manager
->Init();
117 chrome::MultiUserWindowManager::SetInstanceForTest(
118 multi_user_window_manager
,
119 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
120 scoped_ptr
<MultiUserNotificationBlockerChromeOS
> blocker(
121 new MultiUserNotificationBlockerChromeOS(
122 message_center::MessageCenter::Get(),
124 EXPECT_EQ(0u, message_center()->NotificationCount());
125 notification_manager()->Add(GetANotification("test"), &profile
);
126 EXPECT_EQ(1u, message_center()->NotificationCount());
127 notification_manager()->Update(GetANotification("test"), &profile
);
128 EXPECT_EQ(1u, message_center()->NotificationCount());
129 chrome::MultiUserWindowManager::DeleteInstance();
133 } // namespace message_center