Chromium Code Reviews| 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) { | |
|
Dmitry Titov
2013/06/21 20:53:40
Get->Create?
can also return a const ref, for the
dewittj
2013/06/21 21:56:51
Actually it can't since that would return a refere
| |
| 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 #if !defined(OS_CHROMEOS) | |
| 119 TEST_F(MessageCenterNotificationManagerTest, SetupNotificationManager) { | |
| 120 TestingProfile profile; | |
| 121 notification_manager()->Add(GetANotification("test"), &profile); | |
| 122 EXPECT_FALSE(DidFirstRunPref()); | |
| 123 } | |
| 124 #endif | |
| 125 | |
| 126 #if defined(OS_WIN) | |
| 127 // The following tests test the first run balloon, which is only implemented for | |
| 128 // Windows. | |
| 129 TEST_F(MessageCenterNotificationManagerTest, FirstRunShown) { | |
| 130 TestingProfile profile; | |
| 131 notification_manager()->Add(GetANotification("test"), &profile); | |
| 132 message_center()->DisplayedNotification("test"); | |
| 133 message_center()->MarkSinglePopupAsShown("test", false); | |
| 134 | |
| 135 run_loop()->Run(); | |
| 136 base::RunLoop run_loop_2; | |
| 137 run_loop_2.RunUntilIdle(); | |
| 138 EXPECT_TRUE(delegate()->displayed_first_run_balloon()); | |
| 139 EXPECT_TRUE(DidFirstRunPref()); | |
| 140 } | |
| 141 | |
| 142 TEST_F(MessageCenterNotificationManagerTest, | |
| 143 FirstRunNotShownWithPopupsVisible) { | |
| 144 TestingProfile profile; | |
| 145 notification_manager()->Add(GetANotification("test"), &profile); | |
| 146 message_center()->DisplayedNotification("test"); | |
| 147 | |
| 148 base::MessageLoop::current()->PostDelayedTask( | |
| 149 FROM_HERE, | |
| 150 run_loop()->QuitClosure(), | |
| 151 TestTimeouts::tiny_timeout() + base::TimeDelta::FromMilliseconds(10)); | |
|
Dmitry Titov
2013/06/21 20:53:40
tests based on timeouts are not welcome in Chrome.
dewittj
2013/06/21 21:56:51
Done.
| |
| 152 run_loop()->Run(); | |
| 153 EXPECT_FALSE(delegate()->displayed_first_run_balloon()); | |
| 154 EXPECT_FALSE(DidFirstRunPref()); | |
| 155 } | |
| 156 | |
| 157 TEST_F(MessageCenterNotificationManagerTest, | |
| 158 FirstRunNotShownWithMessageCenter) { | |
| 159 TestingProfile profile; | |
| 160 notification_manager()->Add(GetANotification("test"), &profile); | |
| 161 message_center()->SetMessageCenterVisible(true); | |
| 162 | |
| 163 base::MessageLoop::current()->PostDelayedTask( | |
| 164 FROM_HERE, | |
| 165 run_loop()->QuitClosure(), | |
| 166 TestTimeouts::tiny_timeout() + base::TimeDelta::FromMilliseconds(10)); | |
| 167 run_loop()->Run(); | |
| 168 EXPECT_FALSE(delegate()->displayed_first_run_balloon()); | |
| 169 EXPECT_FALSE(DidFirstRunPref()); | |
| 170 } | |
| 171 #endif // defined(OS_WIN) | |
| 172 } // namespace message_center | |
| OLD | NEW |