1 // Copyright 2013 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/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/scoped_testing_local_state.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/message_center/fake_message_center_tray_delegate.h"
20 #include "ui/message_center/fake_notifier_settings_provider.h"
21 #include "ui/message_center/message_center_impl.h"
22 #include "ui/message_center/message_center_tray.h"
23 #include "ui/message_center/message_center_types.h"
24 #include "ui/message_center/notifier_settings.h"
26 namespace message_center
{
28 class MessageCenterNotificationManagerTest
: public testing::Test
{
30 MessageCenterNotificationManagerTest() {
31 MessageCenterNotificationManager::RegisterPrefs(local_state_
.registry());
34 virtual void SetUp() {
35 // Clear the preference and initialize.
36 local_state_
.ClearPref(prefs::kMessageCenterShowedFirstRunBalloon
);
37 first_run_pref_
.Init(prefs::kMessageCenterShowedFirstRunBalloon
,
40 // Get ourselves a run loop.
41 run_loop_
.reset(new base::RunLoop());
43 // Initialize message center infrastructure with mock tray delegate.
44 MessageCenter::Initialize();
45 message_center_
= MessageCenter::Get();
46 scoped_ptr
<NotifierSettingsProvider
> settings_provider(
47 new FakeNotifierSettingsProvider(notifiers_
));
48 notification_manager_
.reset(new MessageCenterNotificationManager(
49 message_center_
, &local_state_
, settings_provider
.Pass()));
50 delegate_
= new FakeMessageCenterTrayDelegate(message_center_
,
51 run_loop_
->QuitClosure());
52 notification_manager_
->SetMessageCenterTrayDelegateForTest(delegate_
);
53 notification_manager_
->SetFirstRunTimeoutForTest(
54 TestTimeouts::tiny_timeout());
57 virtual void TearDown() {
59 notification_manager_
.reset();
60 MessageCenter::Shutdown();
63 MessageCenterNotificationManager
* notification_manager() {
64 return notification_manager_
.get();
67 FakeMessageCenterTrayDelegate
* delegate() { return delegate_
; }
69 MessageCenter
* message_center() { return message_center_
; }
71 const ::Notification
GetANotification(const std::string
& id
) {
72 return ::Notification(GURL(),
76 blink::WebTextDirectionDefault
,
79 new MockNotificationDelegate(id
));
82 base::RunLoop
* run_loop() { return run_loop_
.get(); }
83 const TestingPrefServiceSimple
& local_state() { return local_state_
; }
84 bool DidFirstRunPref() { return first_run_pref_
.GetValue(); }
87 scoped_ptr
<base::RunLoop
> run_loop_
;
88 TestingPrefServiceSimple local_state_
;
89 MessageCenter
* message_center_
;
90 std::vector
<Notifier
*> notifiers_
;
91 scoped_ptr
<MessageCenterNotificationManager
> notification_manager_
;
92 FakeMessageCenterTrayDelegate
* delegate_
;
93 content::TestBrowserThreadBundle thread_bundle_
;
94 BooleanPrefMember first_run_pref_
;
97 TEST_F(MessageCenterNotificationManagerTest
, SetupNotificationManager
) {
98 TestingProfile profile
;
99 notification_manager()->Add(GetANotification("test"), &profile
);
100 EXPECT_FALSE(DidFirstRunPref());
103 // The following tests test the first run balloon, which is only implemented for
105 TEST_F(MessageCenterNotificationManagerTest
, FirstRunShown
) {
106 TestingProfile profile
;
107 notification_manager()->Add(GetANotification("test"), &profile
);
108 message_center()->DisplayedNotification("test");
109 message_center()->MarkSinglePopupAsShown("test", false);
112 base::RunLoop run_loop_2
;
113 run_loop_2
.RunUntilIdle();
114 EXPECT_TRUE(delegate()->displayed_first_run_balloon());
115 EXPECT_TRUE(DidFirstRunPref());
118 TEST_F(MessageCenterNotificationManagerTest
,
119 FirstRunNotShownWithPopupsVisible
) {
120 TestingProfile profile
;
121 notification_manager()->Add(GetANotification("test"), &profile
);
122 message_center()->DisplayedNotification("test");
123 run_loop()->RunUntilIdle();
124 EXPECT_FALSE(delegate()->displayed_first_run_balloon());
125 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
126 EXPECT_FALSE(DidFirstRunPref());
129 TEST_F(MessageCenterNotificationManagerTest
,
130 FirstRunNotShownWithMessageCenter
) {
131 TestingProfile profile
;
132 notification_manager()->Add(GetANotification("test"), &profile
);
133 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
134 run_loop()->RunUntilIdle();
135 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
136 EXPECT_FALSE(DidFirstRunPref());
138 } // namespace message_center