blob: 01c8534012b2391e7e620e0e018832a3b4eba069 [file] [log] [blame]
Ben Murdocheb525c52013-07-10 11:40:50 +01001// 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.
4
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"
Ben Murdochbb1529c2013-08-08 10:24:53 +010020#include "ui/message_center/fake_notifier_settings_provider.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010021#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"
Ben Murdochbb1529c2013-08-08 10:24:53 +010024#include "ui/message_center/notifier_settings.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010025
26namespace message_center {
27class FakeMessageCenterTrayDelegate : public MessageCenterTrayDelegate {
28 public:
29 FakeMessageCenterTrayDelegate(MessageCenter* message_center,
30 base::Closure quit_closure)
31 : tray_(this, message_center),
32 quit_closure_(quit_closure),
33 displayed_first_run_balloon_(false) {}
34
35 virtual void DisplayFirstRunBalloon() OVERRIDE {
36 displayed_first_run_balloon_ = true;
37 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
38 }
39
40 virtual void OnMessageCenterTrayChanged() OVERRIDE {}
41 virtual bool ShowPopups() OVERRIDE { return true; }
42 virtual void HidePopups() OVERRIDE {}
Ben Murdocheb525c52013-07-10 11:40:50 +010043 virtual bool ShowMessageCenter() OVERRIDE { return true; }
44 virtual bool ShowNotifierSettings() OVERRIDE { return true; }
45 virtual void HideMessageCenter() OVERRIDE {}
Ben Murdoch2385ea32013-08-06 11:01:04 +010046 virtual MessageCenterTray* GetMessageCenterTray() OVERRIDE {
47 return &tray_;
48 }
Ben Murdocheb525c52013-07-10 11:40:50 +010049
50 bool displayed_first_run_balloon() const {
51 return displayed_first_run_balloon_;
52 }
53 private:
54 MessageCenterTray tray_;
55 base::Closure quit_closure_;
56 bool displayed_first_run_balloon_;
57};
58
59class MessageCenterNotificationManagerTest : public testing::Test {
60 protected:
61 MessageCenterNotificationManagerTest() {
62 NotificationPrefsManager::RegisterPrefs(local_state_.registry());
63 }
64
65 virtual void SetUp() {
66 // Clear the preference and initialize.
67 local_state_.ClearPref(prefs::kMessageCenterShowedFirstRunBalloon);
68 first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon,
69 &local_state_);
70
71 // Get ourselves a run loop.
72 run_loop_.reset(new base::RunLoop());
73
74 // Initialize message center infrastructure with mock tray delegate.
75 MessageCenter::Initialize();
76 message_center_ = MessageCenter::Get();
Ben Murdochbb1529c2013-08-08 10:24:53 +010077 scoped_ptr<NotifierSettingsProvider> settings_provider(
78 new FakeNotifierSettingsProvider(notifiers_));
79 notification_manager_.reset(new MessageCenterNotificationManager(
80 message_center_, &local_state_, settings_provider.Pass()));
Ben Murdocheb525c52013-07-10 11:40:50 +010081 delegate_ = new FakeMessageCenterTrayDelegate(message_center_,
82 run_loop_->QuitClosure());
83 notification_manager_->SetMessageCenterTrayDelegateForTest(delegate_);
84 notification_manager_->SetFirstRunTimeoutForTest(
85 TestTimeouts::tiny_timeout());
86 }
87
88 virtual void TearDown() {
89 run_loop_.reset();
90 notification_manager_.reset();
91 MessageCenter::Shutdown();
92 }
93
94 MessageCenterNotificationManager* notification_manager() {
95 return notification_manager_.get();
96 }
97
98 FakeMessageCenterTrayDelegate* delegate() { return delegate_; }
99
100 MessageCenter* message_center() { return message_center_; }
101
102 const ::Notification GetANotification(const std::string& id) {
103 return ::Notification(GURL(),
104 GURL(),
105 string16(),
106 string16(),
107 new MockNotificationDelegate(id));
108 }
109
110 base::RunLoop* run_loop() { return run_loop_.get(); }
111 const TestingPrefServiceSimple& local_state() { return local_state_; }
112 bool DidFirstRunPref() { return first_run_pref_.GetValue(); }
113
114 private:
115 scoped_ptr<base::RunLoop> run_loop_;
116 TestingPrefServiceSimple local_state_;
117 MessageCenter* message_center_;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100118 std::vector<Notifier*> notifiers_;
Ben Murdocheb525c52013-07-10 11:40:50 +0100119 scoped_ptr<MessageCenterNotificationManager> notification_manager_;
120 FakeMessageCenterTrayDelegate* delegate_;
121 content::TestBrowserThreadBundle thread_bundle_;
122 BooleanPrefMember first_run_pref_;
123};
124
125TEST_F(MessageCenterNotificationManagerTest, SetupNotificationManager) {
126 TestingProfile profile;
127 notification_manager()->Add(GetANotification("test"), &profile);
128 EXPECT_FALSE(DidFirstRunPref());
129}
130
131// The following tests test the first run balloon, which is only implemented for
132// Windows.
133TEST_F(MessageCenterNotificationManagerTest, FirstRunShown) {
134 TestingProfile profile;
135 notification_manager()->Add(GetANotification("test"), &profile);
136 message_center()->DisplayedNotification("test");
137 message_center()->MarkSinglePopupAsShown("test", false);
138
139 run_loop()->Run();
140 base::RunLoop run_loop_2;
141 run_loop_2.RunUntilIdle();
142 EXPECT_TRUE(delegate()->displayed_first_run_balloon());
143 EXPECT_TRUE(DidFirstRunPref());
144}
145
146TEST_F(MessageCenterNotificationManagerTest,
147 FirstRunNotShownWithPopupsVisible) {
148 TestingProfile profile;
149 notification_manager()->Add(GetANotification("test"), &profile);
150 message_center()->DisplayedNotification("test");
151 run_loop()->RunUntilIdle();
152 EXPECT_FALSE(delegate()->displayed_first_run_balloon());
153 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
154 EXPECT_FALSE(DidFirstRunPref());
155}
156
157TEST_F(MessageCenterNotificationManagerTest,
158 FirstRunNotShownWithMessageCenter) {
159 TestingProfile profile;
160 notification_manager()->Add(GetANotification("test"), &profile);
161 message_center()->SetMessageCenterVisible(true);
162 run_loop()->RunUntilIdle();
163 EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
164 EXPECT_FALSE(DidFirstRunPref());
165}
166} // namespace message_center