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/testing_browser_process.h"
14 #include "chrome/test/base/scoped_testing_local_state.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 virtual 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 virtual 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 virtual void SetUp() OVERRIDE {
74 MessageCenterTrayBridgeTest::SetUp();
75 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
79 TEST_F(MessageCenterTrayBridgeTest, StatusItemOnlyAfterFirstNotification) {
80 EXPECT_FALSE(status_item());
82 center_->AddNotification(GetNotification());
84 base::RunLoop().RunUntilIdle();
86 EXPECT_TRUE(status_item());
87 EXPECT_EQ(1u, [status_item() unreadCount]);
89 center_->RemoveNotification("1", /*by_user=*/true);
91 base::RunLoop().RunUntilIdle();
93 EXPECT_TRUE(status_item());
96 TEST_F(MessageCenterTrayBridgeTest, StatusItemAppearsWithPrefChange) {
97 EXPECT_FALSE(status_item());
98 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
99 EXPECT_FALSE(status_item());
100 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
101 EXPECT_TRUE(status_item());
104 TEST_F(MessageCenterTrayBridgeTest, StatusItemDisappearsWithPrefChange) {
105 EXPECT_FALSE(status_item());
106 center_->AddNotification(GetNotification());
108 base::RunLoop().RunUntilIdle();
110 EXPECT_TRUE(status_item());
111 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, false);
112 EXPECT_FALSE(status_item());
113 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
114 EXPECT_TRUE(status_item());
117 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemNever) {
118 EXPECT_FALSE(status_item());
120 center_->AddNotification(GetNotification());
122 base::RunLoop().RunUntilIdle();
124 EXPECT_FALSE(status_item());
126 center_->RemoveNotification("1", /*by_user=*/true);
128 base::RunLoop().RunUntilIdle();
130 EXPECT_FALSE(status_item());
133 TEST_F(MessageCenterTrayBridgeTestPrefNever, StatusItemBackWithPref) {
134 EXPECT_FALSE(status_item());
136 center_->AddNotification(GetNotification());
138 base::RunLoop().RunUntilIdle();
140 EXPECT_FALSE(status_item());
142 center_->RemoveNotification("1", /*by_user=*/true);
144 base::RunLoop().RunUntilIdle();
146 EXPECT_FALSE(status_item());
148 local_state()->SetBoolean(prefs::kMessageCenterShowIcon, true);
150 EXPECT_TRUE(status_item());