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_prefs_manager.h"
13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/common/pref_names.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 "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.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_tray_delegate.h"
24 #include "ui/message_center/message_center_types.h"
25 #include "ui/message_center/notifier_settings.h"
27 namespace message_center {
28 class FakeMessageCenterTrayDelegate : public MessageCenterTrayDelegate {
30 FakeMessageCenterTrayDelegate(MessageCenter* message_center,
31 base::Closure quit_closure)
32 : tray_(this, message_center),
33 quit_closure_(quit_closure),
34 displayed_first_run_balloon_(false) {}
36 virtual void DisplayFirstRunBalloon() OVERRIDE {
37 displayed_first_run_balloon_ = true;
38 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
41 virtual void OnMessageCenterTrayChanged() OVERRIDE {}
42 virtual bool ShowPopups() OVERRIDE { return true; }
43 virtual void HidePopups() OVERRIDE {}
44 virtual bool ShowMessageCenter() OVERRIDE { return true; }
45 virtual bool ShowNotifierSettings() OVERRIDE { return true; }
46 virtual void HideMessageCenter() OVERRIDE {}
47 virtual MessageCenterTray* GetMessageCenterTray() OVERRIDE {
51 bool displayed_first_run_balloon() const {
52 return displayed_first_run_balloon_;
55 MessageCenterTray tray_;
56 base::Closure quit_closure_;
57 bool displayed_first_run_balloon_;
60 class MessageCenterNotificationManagerTest : public testing::Test {
62 MessageCenterNotificationManagerTest() {
63 NotificationPrefsManager::RegisterPrefs(local_state_.registry());
66 virtual void SetUp() {
67 // Clear the preference and initialize.
68 local_state_.ClearPref(prefs::kMessageCenterShowedFirstRunBalloon);
69 first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon,
72 // Get ourselves a run loop.
73 run_loop_.reset(new base::RunLoop());
75 // Initialize message center infrastructure with mock tray delegate.
76 MessageCenter::Initialize();
77 message_center_ = MessageCenter::Get();
78 scoped_ptr<NotifierSettingsProvider> settings_provider(
79 new FakeNotifierSettingsProvider(notifiers_));
80 notification_manager_.reset(new MessageCenterNotificationManager(
81 message_center_, &local_state_, settings_provider.Pass()));
82 delegate_ = new FakeMessageCenterTrayDelegate(message_center_,
83 run_loop_->QuitClosure());
84 notification_manager_->SetMessageCenterTrayDelegateForTest(delegate_);
85 notification_manager_->SetFirstRunTimeoutForTest(
86 TestTimeouts::tiny_timeout());
89 virtual void TearDown() {
91 notification_manager_.reset();
92 MessageCenter::Shutdown();
95 MessageCenterNotificationManager* notification_manager() {
96 return notification_manager_.get();
99 FakeMessageCenterTrayDelegate* delegate() { return delegate_; }
101 MessageCenter* message_center() { return message_center_; }
103 const ::Notification GetANotification(const std::string& id) {
104 return ::Notification(GURL(),
108 new MockNotificationDelegate(id));
111 base::RunLoop* run_loop() { return run_loop_.get(); }
112 const TestingPrefServiceSimple& local_state() { return local_state_; }
113 bool DidFirstRunPref() { return first_run_pref_.GetValue(); }
116 scoped_ptr<base::RunLoop> run_loop_;
117 TestingPrefServiceSimple local_state_;
118 MessageCenter* message_center_;
119 std::vector<Notifier*> notifiers_;
120 scoped_ptr<MessageCenterNotificationManager> notification_manager_;
121 FakeMessageCenterTrayDelegate* delegate_;
122 content::TestBrowserThreadBundle thread_bundle_;
123 BooleanPrefMember first_run_pref_;
126 TEST_F(MessageCenterNotificationManagerTest, SetupNotificationManager) {
127 TestingProfile profile;
128 notification_manager()->Add(GetANotification("test"), &profile);
129 EXPECT_FALSE(DidFirstRunPref());
132 // The following tests test the first run balloon, which is only implemented for
134 TEST_F(MessageCenterNotificationManagerTest, FirstRunShown) {
135 TestingProfile profile;
136 notification_manager()->Add(GetANotification("test"), &profile);
137 message_center()->DisplayedNotification("test");
138 message_center()->MarkSinglePopupAsShown("test", false);
141 base::RunLoop run_loop_2;
142 run_loop_2.RunUntilIdle();
143 EXPECT_TRUE(delegate()->displayed_first_run_balloon());
144 EXPECT_TRUE(DidFirstRunPref());
147 TEST_F(MessageCenterNotificationManagerTest,
148 FirstRunNotShownWithPopupsVisible) {
149 TestingProfile profile;
150 notification_manager()->Add(GetANotification("test"), &profile);
151 message_center()->DisplayedNotification("test");
152 run_loop()->RunUntilIdle();
153 EXPECT_FALSE(delegate()->displayed_first_run_balloon());
154 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
155 EXPECT_FALSE(DidFirstRunPref());
158 TEST_F(MessageCenterNotificationManagerTest,
159 FirstRunNotShownWithMessageCenter) {
160 TestingProfile profile;
161 notification_manager()->Add(GetANotification("test"), &profile);
162 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
163 run_loop()->RunUntilIdle();
164 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
165 EXPECT_FALSE(DidFirstRunPref());
167 } // namespace message_center