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 #import "ui/gfx/test/ui_cocoa_test_helper.h"
13 #import "ui/message_center/cocoa/status_item_view.h"
14 #include "ui/message_center/message_center.h"
15 #include "ui/message_center/message_center_switches.h"
16 #include "ui/message_center/notification.h"
17 #include "ui/message_center/notifier_settings.h"
19 class MessageCenterTrayBridgeTest : public ui::CocoaTest {
21 virtual void SetUp() OVERRIDE {
22 ui::CocoaTest::SetUp();
24 message_center::MessageCenter::Initialize();
25 center_ = message_center::MessageCenter::Get();
27 bridge_.reset(new MessageCenterTrayBridge(center_));
30 virtual void TearDown() OVERRIDE {
32 message_center::MessageCenter::Shutdown();
33 ui::CocoaTest::TearDown();
36 MCStatusItemView* status_item() { return bridge_->status_item_view_.get(); }
39 scoped_ptr<message_center::Notification> GetNotification() {
40 message_center::RichNotificationData data;
42 return make_scoped_ptr(new message_center::Notification(
43 message_center::NOTIFICATION_TYPE_SIMPLE,
45 base::ASCIIToUTF16("First notification"),
46 base::ASCIIToUTF16("This is a simple test."),
49 message_center::NotifierId(),
54 base::MessageLoop message_loop_;
55 message_center::MessageCenter* center_; // Weak, global.
56 scoped_ptr<MessageCenterTrayBridge> bridge_;
59 class MessageCenterTrayBridgeTestPrefNever
60 : public MessageCenterTrayBridgeTest {
62 virtual void SetUp() OVERRIDE {
63 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
64 message_center::switches::kNotificationCenterTrayBehavior, "never");
65 MessageCenterTrayBridgeTest::SetUp();
69 class MessageCenterTrayBridgeTestPrefAlways
70 : public MessageCenterTrayBridgeTest {
72 virtual void SetUp() OVERRIDE {
73 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
74 message_center::switches::kNotificationCenterTrayBehavior, "always");
75 MessageCenterTrayBridgeTest::SetUp();
79 class MessageCenterTrayBridgeTestPrefUnread
80 : public MessageCenterTrayBridgeTest {
82 virtual void SetUp() OVERRIDE {
83 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
84 message_center::switches::kNotificationCenterTrayBehavior, "unread");
85 MessageCenterTrayBridgeTest::SetUp();
89 TEST_F(MessageCenterTrayBridgeTest, StatusItemOnlyAfterFirstNotification) {
90 EXPECT_FALSE(status_item());
92 center_->AddNotification(GetNotification());
94 base::RunLoop().RunUntilIdle();
96 EXPECT_TRUE(status_item());
97 EXPECT_EQ(1u, [status_item() unreadCount]);
99 center_->RemoveNotification("1", /*by_user=*/true);
101 base::RunLoop().RunUntilIdle();
103 EXPECT_TRUE(status_item());
106 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemNeverWithPref) {
107 EXPECT_FALSE(status_item());
109 center_->AddNotification(GetNotification());
111 base::RunLoop().RunUntilIdle();
113 EXPECT_FALSE(status_item());
115 center_->RemoveNotification("1", /*by_user=*/true);
117 base::RunLoop().RunUntilIdle();
119 EXPECT_FALSE(status_item());
122 TEST_F(MessageCenterTrayBridgeTestPrefAlways, StatusItemAlwaysWithPref) {
123 EXPECT_TRUE(status_item());
125 center_->AddNotification(GetNotification());
127 base::RunLoop().RunUntilIdle();
129 EXPECT_TRUE(status_item());
130 EXPECT_EQ(1u, [status_item() unreadCount]);
132 center_->RemoveNotification("1", /*by_user=*/true);
134 base::RunLoop().RunUntilIdle();
136 EXPECT_TRUE(status_item());
139 TEST_F(MessageCenterTrayBridgeTestPrefUnread, StatusItemUnreadWithPref) {
140 EXPECT_FALSE(status_item());
142 center_->AddNotification(GetNotification());
144 base::RunLoop().RunUntilIdle();
146 EXPECT_TRUE(status_item());
147 EXPECT_EQ(1u, [status_item() unreadCount]);
149 center_->RemoveNotification("1", /*by_user=*/true);
151 base::RunLoop().RunUntilIdle();
153 EXPECT_FALSE(status_item());