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