1 // Copyright (c) 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.
5 #import "chrome/browser/ui/cocoa/notifications/message_center_tray_bridge.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/scoped_testing_local_state.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #import "ui/gfx/test/ui_cocoa_test_helper.h"
16 #import "ui/message_center/cocoa/status_item_view.h"
17 #include "ui/message_center/message_center.h"
18 #include "ui/message_center/notification.h"
19 #include "ui/message_center/notifier_settings.h"
21 class MessageCenterTrayBridgeTest : public ui::CocoaTest {
23 void SetUp() override {
24 ui::CocoaTest::SetUp();
27 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
28 message_center::MessageCenter::Initialize();
29 center_ = message_center::MessageCenter::Get();
31 bridge_.reset(new MessageCenterTrayBridge(center_));
34 void TearDown() override {
36 message_center::MessageCenter::Shutdown();
39 ui::CocoaTest::TearDown();
42 MCStatusItemView* status_item() { return bridge_->status_item_view_.get(); }
45 scoped_ptr<message_center::Notification> GetNotification() {
46 message_center::RichNotificationData data;
48 return make_scoped_ptr(new message_center::Notification(
49 message_center::NOTIFICATION_TYPE_SIMPLE,
51 base::ASCIIToUTF16("First notification"),
52 base::ASCIIToUTF16("This is a simple test."),
55 message_center::NotifierId(),
60 TestingPrefServiceSimple* local_state() { return local_state_->Get(); }
62 scoped_ptr<TestingBrowserProcessInitializer> initializer_;
63 scoped_ptr<ScopedTestingLocalState> local_state_;
65 base::MessageLoop message_loop_;
66 message_center::MessageCenter* center_; // Weak, global.
67 scoped_ptr<MessageCenterTrayBridge> bridge_;
70 class MessageCenterTrayBridgeTestPrefNever
71 : public MessageCenterTrayBridgeTest {
73 void SetUp() override {
74 MessageCenterTrayBridgeTest::SetUp();
75 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
79 TEST_F(MessageCenterTrayBridgeTest, StatusItemOnlyAfterFirstNotification) {
80 EXPECT_FALSE(status_item());
82 bridge_->OnMessageCenterTrayChanged();
83 base::RunLoop().RunUntilIdle();
84 EXPECT_FALSE(status_item());
86 center_->AddNotification(GetNotification());
88 base::RunLoop().RunUntilIdle();
90 EXPECT_TRUE(status_item());
91 EXPECT_EQ(1u, [status_item() unreadCount]);
93 center_->RemoveNotification("1", /*by_user=*/true);
95 base::RunLoop().RunUntilIdle();
97 EXPECT_TRUE(status_item());
100 TEST_F(MessageCenterTrayBridgeTest, StatusItemAppearsWithPrefChange) {
101 EXPECT_FALSE(status_item());
102 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
103 EXPECT_FALSE(status_item());
104 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
105 EXPECT_TRUE(status_item());
108 TEST_F(MessageCenterTrayBridgeTest, StatusItemDisappearsWithPrefChange) {
109 EXPECT_FALSE(status_item());
110 center_->AddNotification(GetNotification());
112 base::RunLoop().RunUntilIdle();
114 EXPECT_TRUE(status_item());
115 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
116 EXPECT_FALSE(status_item());
117 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
118 EXPECT_TRUE(status_item());
121 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemNever) {
122 EXPECT_FALSE(status_item());
124 center_->AddNotification(GetNotification());
126 base::RunLoop().RunUntilIdle();
128 EXPECT_FALSE(status_item());
130 center_->RemoveNotification("1", /*by_user=*/true);
132 base::RunLoop().RunUntilIdle();
134 EXPECT_FALSE(status_item());
137 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemBackWithPref) {
138 EXPECT_FALSE(status_item());
140 center_->AddNotification(GetNotification());
142 base::RunLoop().RunUntilIdle();
144 EXPECT_FALSE(status_item());
146 center_->RemoveNotification("1", /*by_user=*/true);
148 base::RunLoop().RunUntilIdle();
150 EXPECT_FALSE(status_item());
152 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
154 EXPECT_TRUE(status_item());