Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / message_center_notifications_unittest_win.cc
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
27 namespace message_center {
28 class 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 bool IsContextMenuEnabled() const OVERRIDE { return true; }
47   virtual void HideMessageCenter() OVERRIDE {}
48   virtual MessageCenterTray* GetMessageCenterTray() OVERRIDE {
49     return &tray_;
50   }
51
52   bool displayed_first_run_balloon() const {
53     return displayed_first_run_balloon_;
54   }
55  private:
56   MessageCenterTray tray_;
57   base::Closure quit_closure_;
58   bool displayed_first_run_balloon_;
59 };
60
61 class MessageCenterNotificationManagerTest : public testing::Test {
62  protected:
63   MessageCenterNotificationManagerTest() {
64     NotificationPrefsManager::RegisterPrefs(local_state_.registry());
65   }
66
67   virtual void SetUp() {
68     // Clear the preference and initialize.
69     local_state_.ClearPref(prefs::kMessageCenterShowedFirstRunBalloon);
70     first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon,
71                          &local_state_);
72
73     // Get ourselves a run loop.
74     run_loop_.reset(new base::RunLoop());
75
76     // Initialize message center infrastructure with mock tray delegate.
77     MessageCenter::Initialize();
78     message_center_ = MessageCenter::Get();
79     scoped_ptr<NotifierSettingsProvider> settings_provider(
80         new FakeNotifierSettingsProvider(notifiers_));
81     notification_manager_.reset(new MessageCenterNotificationManager(
82         message_center_, &local_state_, settings_provider.Pass()));
83     delegate_ = new FakeMessageCenterTrayDelegate(message_center_,
84                                                   run_loop_->QuitClosure());
85     notification_manager_->SetMessageCenterTrayDelegateForTest(delegate_);
86     notification_manager_->SetFirstRunTimeoutForTest(
87         TestTimeouts::tiny_timeout());
88   }
89
90   virtual void TearDown() {
91     run_loop_.reset();
92     notification_manager_.reset();
93     MessageCenter::Shutdown();
94   }
95
96   MessageCenterNotificationManager* notification_manager() {
97     return notification_manager_.get();
98   }
99
100   FakeMessageCenterTrayDelegate* delegate() { return delegate_; }
101
102   MessageCenter* message_center() { return message_center_; }
103
104   const ::Notification GetANotification(const std::string& id) {
105     return ::Notification(GURL(),
106                           GURL(),
107                           base::string16(),
108                           base::string16(),
109                           new MockNotificationDelegate(id));
110   }
111
112   base::RunLoop* run_loop() { return run_loop_.get(); }
113   const TestingPrefServiceSimple& local_state() { return local_state_; }
114   bool DidFirstRunPref() { return first_run_pref_.GetValue(); }
115
116  private:
117   scoped_ptr<base::RunLoop> run_loop_;
118   TestingPrefServiceSimple local_state_;
119   MessageCenter* message_center_;
120   std::vector<Notifier*> notifiers_;
121   scoped_ptr<MessageCenterNotificationManager> notification_manager_;
122   FakeMessageCenterTrayDelegate* delegate_;
123   content::TestBrowserThreadBundle thread_bundle_;
124   BooleanPrefMember first_run_pref_;
125 };
126
127 TEST_F(MessageCenterNotificationManagerTest, SetupNotificationManager) {
128   TestingProfile profile;
129   notification_manager()->Add(GetANotification("test"), &profile);
130   EXPECT_FALSE(DidFirstRunPref());
131 }
132
133 // The following tests test the first run balloon, which is only implemented for
134 // Windows.
135 TEST_F(MessageCenterNotificationManagerTest, FirstRunShown) {
136   TestingProfile profile;
137   notification_manager()->Add(GetANotification("test"), &profile);
138   message_center()->DisplayedNotification("test");
139   message_center()->MarkSinglePopupAsShown("test", false);
140
141   run_loop()->Run();
142   base::RunLoop run_loop_2;
143   run_loop_2.RunUntilIdle();
144   EXPECT_TRUE(delegate()->displayed_first_run_balloon());
145   EXPECT_TRUE(DidFirstRunPref());
146 }
147
148 TEST_F(MessageCenterNotificationManagerTest,
149        FirstRunNotShownWithPopupsVisible) {
150   TestingProfile profile;
151   notification_manager()->Add(GetANotification("test"), &profile);
152   message_center()->DisplayedNotification("test");
153   run_loop()->RunUntilIdle();
154   EXPECT_FALSE(delegate()->displayed_first_run_balloon());
155   EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
156   EXPECT_FALSE(DidFirstRunPref());
157 }
158
159 TEST_F(MessageCenterNotificationManagerTest,
160        FirstRunNotShownWithMessageCenter) {
161   TestingProfile profile;
162   notification_manager()->Add(GetANotification("test"), &profile);
163   message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
164   run_loop()->RunUntilIdle();
165   EXPECT_FALSE(notification_manager()->FirstRunTimerIsActive());
166   EXPECT_FALSE(DidFirstRunPref());
167 }
168 }  // namespace message_center