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() { |
| 34 displayed_first_run_balloon_ = true; |
| 35 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); |
| 36 } |
| 37 |
| 38 virtual void OnMessageCenterTrayChanged() {} |
| 39 virtual bool ShowPopups() { return true; } |
| 40 virtual void HidePopups() {} |
| 41 virtual void UpdatePopups() {} |
| 42 virtual bool ShowMessageCenter() { return true; } |
| 43 virtual void HideMessageCenter() {} |
| 44 |
| 45 bool displayed_first_run_balloon() const { |
| 46 return displayed_first_run_balloon_; |
| 47 } |
| 48 private: |
| 49 MessageCenterTray tray_; |
| 50 base::Closure quit_closure_; |
| 51 bool displayed_first_run_balloon_; |
| 52 }; |
| 53 |
| 54 class MessageCenterNotificationManagerTest : public testing::Test { |
| 55 protected: |
| 56 MessageCenterNotificationManagerTest() { |
| 57 NotificationPrefsManager::RegisterPrefs(local_state_.registry()); |
| 58 } |
| 59 |
| 60 virtual void SetUp() { |
| 61 // Clear the preference and initialize. |
| 62 local_state_.ClearPref(prefs::kMessageCenterShowedFirstRunBalloon); |
| 63 first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, |
| 64 &local_state_); |
| 65 |
| 66 // Get ourselves a run loop. |
| 67 run_loop_.reset(new base::RunLoop()); |
| 68 |
| 69 // Initialize message center infrastructure with mock tray delegate. |
| 70 MessageCenter::Initialize(); |
| 71 message_center_ = MessageCenter::Get(); |
| 72 notification_manager_.reset( |
| 73 new MessageCenterNotificationManager(message_center_, &local_state_)); |
| 74 delegate_ = new FakeMessageCenterTrayDelegate(message_center_, |
| 75 run_loop_->QuitClosure()); |
| 76 notification_manager_->SetMessageCenterTrayDelegateForTest(delegate_); |
| 77 notification_manager_->SetFirstRunTimeoutForTest( |
| 78 TestTimeouts::tiny_timeout()); |
| 79 } |
| 80 |
| 81 virtual void TearDown() { |
| 82 run_loop_.reset(); |
| 83 notification_manager_.reset(); |
| 84 MessageCenter::Shutdown(); |
| 85 } |
| 86 |
| 87 MessageCenterNotificationManager* notification_manager() { |
| 88 return notification_manager_.get(); |
| 89 } |
| 90 |
| 91 FakeMessageCenterTrayDelegate* delegate() { return delegate_; } |
| 92 |
| 93 MessageCenter* message_center() { return message_center_; } |
| 94 |
| 95 const ::Notification GetANotification(const std::string& id) { |
| 96 return ::Notification(GURL(), |
| 97 GURL(), |
| 98 string16(), |
| 99 string16(), |
| 100 new MockNotificationDelegate(id)); |
| 101 } |
| 102 |
| 103 base::RunLoop* run_loop() { return run_loop_.get(); } |
| 104 const TestingPrefServiceSimple& local_state() { return local_state_; } |
| 105 bool DidFirstRunPref() { return first_run_pref_.GetValue(); } |
| 106 |
| 107 private: |
| 108 scoped_ptr<base::RunLoop> run_loop_; |
| 109 TestingPrefServiceSimple local_state_; |
| 110 MessageCenter* message_center_; |
| 111 scoped_ptr<MessageCenterNotificationManager> notification_manager_; |
| 112 FakeMessageCenterTrayDelegate* delegate_; |
| 113 content::TestBrowserThreadBundle thread_bundle_; |
| 114 BooleanPrefMember first_run_pref_; |
| 115 }; |
| 116 |
| 117 TEST_F(MessageCenterNotificationManagerTest, SetupNotificationManager) { |
| 118 TestingProfile profile; |
| 119 notification_manager()->Add(GetANotification("test"), &profile); |
| 120 EXPECT_FALSE(DidFirstRunPref()); |
| 121 } |
| 122 |
| 123 #if defined(OS_WIN) |
| 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 |
| 145 base::MessageLoop::current()->PostDelayedTask( |
| 146 FROM_HERE, |
| 147 run_loop()->QuitClosure(), |
| 148 TestTimeouts::tiny_timeout() + base::TimeDelta::FromMilliseconds(10)); |
| 149 run_loop()->Run(); |
| 150 EXPECT_FALSE(delegate()->displayed_first_run_balloon()); |
| 151 EXPECT_FALSE(DidFirstRunPref()); |
| 152 } |
| 153 |
| 154 TEST_F(MessageCenterNotificationManagerTest, |
| 155 FirstRunNotShownWithMessageCenter) { |
| 156 TestingProfile profile; |
| 157 notification_manager()->Add(GetANotification("test"), &profile); |
| 158 message_center()->SetMessageCenterVisible(true); |
| 159 |
| 160 base::MessageLoop::current()->PostDelayedTask( |
| 161 FROM_HERE, |
| 162 run_loop()->QuitClosure(), |
| 163 TestTimeouts::tiny_timeout() + base::TimeDelta::FromMilliseconds(10)); |
| 164 run_loop()->Run(); |
| 165 EXPECT_FALSE(delegate()->displayed_first_run_balloon()); |
| 166 EXPECT_FALSE(DidFirstRunPref()); |
| 167 } |
| 168 #endif // defined(OS_WIN) |
| 169 } // namespace message_center |
OLD | NEW |