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