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, "1",
50 base::ASCIIToUTF16("First notification"),
51 base::ASCIIToUTF16("This is a simple test."), gfx::Image(),
52 base::string16(), GURL(), message_center::NotifierId(), data, NULL));
55 TestingPrefServiceSimple* local_state() { return local_state_->Get(); }
57 scoped_ptr<TestingBrowserProcessInitializer> initializer_;
58 scoped_ptr<ScopedTestingLocalState> local_state_;
60 base::MessageLoop message_loop_;
61 message_center::MessageCenter* center_; // Weak, global.
62 scoped_ptr<MessageCenterTrayBridge> bridge_;
65 class MessageCenterTrayBridgeTestPrefNever
66 : public MessageCenterTrayBridgeTest {
68 void SetUp() override {
69 MessageCenterTrayBridgeTest::SetUp();
70 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
74 TEST_F(MessageCenterTrayBridgeTest, StatusItemOnlyAfterFirstNotification) {
75 EXPECT_FALSE(status_item());
77 bridge_->OnMessageCenterTrayChanged();
78 base::RunLoop().RunUntilIdle();
79 EXPECT_FALSE(status_item());
81 center_->AddNotification(GetNotification());
83 base::RunLoop().RunUntilIdle();
85 EXPECT_TRUE(status_item());
86 EXPECT_EQ(1u, [status_item() unreadCount]);
88 center_->RemoveNotification("1", /*by_user=*/true);
90 base::RunLoop().RunUntilIdle();
92 EXPECT_TRUE(status_item());
95 TEST_F(MessageCenterTrayBridgeTest, StatusItemAppearsWithPrefChange) {
96 EXPECT_FALSE(status_item());
97 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
98 EXPECT_FALSE(status_item());
99 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
100 EXPECT_TRUE(status_item());
103 TEST_F(MessageCenterTrayBridgeTest, StatusItemDisappearsWithPrefChange) {
104 EXPECT_FALSE(status_item());
105 center_->AddNotification(GetNotification());
107 base::RunLoop().RunUntilIdle();
109 EXPECT_TRUE(status_item());
110 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
111 EXPECT_FALSE(status_item());
112 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
113 EXPECT_TRUE(status_item());
116 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemNever) {
117 EXPECT_FALSE(status_item());
119 center_->AddNotification(GetNotification());
121 base::RunLoop().RunUntilIdle();
123 EXPECT_FALSE(status_item());
125 center_->RemoveNotification("1", /*by_user=*/true);
127 base::RunLoop().RunUntilIdle();
129 EXPECT_FALSE(status_item());
132 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemBackWithPref) {
133 EXPECT_FALSE(status_item());
135 center_->AddNotification(GetNotification());
137 base::RunLoop().RunUntilIdle();
139 EXPECT_FALSE(status_item());
141 center_->RemoveNotification("1", /*by_user=*/true);
143 base::RunLoop().RunUntilIdle();
145 EXPECT_FALSE(status_item());
147 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
149 EXPECT_TRUE(status_item());