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.
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"
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"
26
27namespace message_center {
28class FakeMessageCenterTrayDelegate : public MessageCenterTrayDelegate {
29 public:
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) {}
35
36  virtual void DisplayFirstRunBalloon() OVERRIDE {
37    displayed_first_run_balloon_ = true;
38    base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
39  }
40
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 {
48    return &tray_;
49  }
50
51  bool displayed_first_run_balloon() const {
52    return displayed_first_run_balloon_;
53  }
54 private:
55  MessageCenterTray tray_;
56  base::Closure quit_closure_;
57  bool displayed_first_run_balloon_;
58};
59
60class MessageCenterNotificationManagerTest : public testing::Test {
61 protected:
62  MessageCenterNotificationManagerTest() {
63    NotificationPrefsManager::RegisterPrefs(local_state_.registry());
64  }
65
66  virtual void SetUp() {
67    // Clear the preference and initialize.
68    local_state_.ClearPref(prefs::kMessageCenterShowedFirstRunBalloon);
69    first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon,
70                         &local_state_);
71
72    // Get ourselves a run loop.
73    run_loop_.reset(new base::RunLoop());
74
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());
87  }
88
89  virtual void TearDown() {
90    run_loop_.reset();
91    notification_manager_.reset();
92    MessageCenter::Shutdown();
93  }
94
95  MessageCenterNotificationManager* notification_manager() {
96    return notification_manager_.get();
97  }
98
99  FakeMessageCenterTrayDelegate* delegate() { return delegate_; }
100
101  MessageCenter* message_center() { return message_center_; }
102
103  const ::Notification GetANotification(const std::string& id) {
104    return ::Notification(GURL(),
105                          GURL(),
106                          base::string16(),
107                          base::string16(),
108                          new MockNotificationDelegate(id));
109  }
110
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(); }
114
115 private:
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_;
124};
125
126TEST_F(MessageCenterNotificationManagerTest, SetupNotificationManager) {
127  TestingProfile profile;
128  notification_manager()->Add(GetANotification("test"), &profile);
129  EXPECT_FALSE(DidFirstRunPref());
130}
131
132// The following tests test the first run balloon, which is only implemented for
133// Windows.
134TEST_F(MessageCenterNotificationManagerTest, FirstRunShown) {
135  TestingProfile profile;
136  notification_manager()->Add(GetANotification("test"), &profile);
137  message_center()->DisplayedNotification("test");
138  message_center()->MarkSinglePopupAsShown("test", false);
139
140  run_loop()->Run();
141  base::RunLoop run_loop_2;
142  run_loop_2.RunUntilIdle();
143  EXPECT_TRUE(delegate()->displayed_first_run_balloon());
144  EXPECT_TRUE(DidFirstRunPref());
145}
146
147TEST_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());
156}
157
158TEST_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());
166}
167}  // namespace message_center
168