Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: ash/system/web_notification/web_notification_tray_unittest.cc

Issue 11819048: Implement message center on Windows (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address sky/msw comments + rebase. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/system/web_notification/web_notification_tray.h" 5 #include "ash/system/web_notification/web_notification_tray.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "ash/root_window_controller.h" 9 #include "ash/root_window_controller.h"
10 #include "ash/system/status_area_widget.h" 10 #include "ash/system/status_area_widget.h"
11 #include "ash/system/tray/system_tray_item.h" 11 #include "ash/system/tray/system_tray_item.h"
12 #include "ash/test/ash_test_base.h"
13 #include "base/stringprintf.h"
14 #include "base/utf_string_conversions.h"
12 #include "ui/message_center/message_center_bubble.h" 15 #include "ui/message_center/message_center_bubble.h"
16 #include "ui/message_center/message_center_tray.h"
13 #include "ui/message_center/message_popup_bubble.h" 17 #include "ui/message_center/message_popup_bubble.h"
14 #include "ui/message_center/notification_list.h" 18 #include "ui/message_center/notification_list.h"
15 #include "ui/notifications/notification_types.h" 19 #include "ui/notifications/notification_types.h"
16 #include "ash/test/ash_test_base.h"
17 #include "base/stringprintf.h"
18 #include "base/utf_string_conversions.h"
19 #include "ui/views/controls/label.h" 20 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/fill_layout.h" 21 #include "ui/views/layout/fill_layout.h"
21 #include "ui/views/view.h" 22 #include "ui/views/view.h"
22 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
23 24
24 #if defined(OS_WIN) 25 #if defined(OS_WIN)
25 #include "base/win/windows_version.h" 26 #include "base/win/windows_version.h"
26 #endif 27 #endif
27 28
28 namespace ash { 29 namespace ash {
29 30
30 namespace { 31 namespace {
31 32
32 WebNotificationTray* GetWebNotificationTray() { 33 WebNotificationTray* GetWebNotificationTray() {
33 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> 34 return Shell::GetPrimaryRootWindowController()->status_area_widget()->
34 web_notification_tray(); 35 web_notification_tray();
35 } 36 }
36 37
38 message_center::MessageCenter* get_message_center() {
39 return GetWebNotificationTray()->message_center();
40 }
41
37 class TestDelegate : public message_center::MessageCenter::Delegate { 42 class TestDelegate : public message_center::MessageCenter::Delegate {
38 public: 43 public:
39 TestDelegate(message_center::MessageCenter* message_center) 44 TestDelegate(message_center::MessageCenter* message_center)
40 : message_center_(message_center) { 45 : message_center_(message_center) {
41 message_center_->SetDelegate(this); 46 message_center_->SetDelegate(this);
42 } 47 }
43 virtual ~TestDelegate() { 48 virtual ~TestDelegate() {
44 message_center_->SetDelegate(NULL); 49 message_center_->SetDelegate(NULL);
45 message_center_->notification_list()->RemoveAllNotifications(); 50 message_center_->notification_list()->RemoveAllNotifications();
46 } 51 }
(...skipping 14 matching lines...) Expand all
61 } 66 }
62 67
63 virtual void ShowSettingsDialog(gfx::NativeView context) { 68 virtual void ShowSettingsDialog(gfx::NativeView context) {
64 } 69 }
65 70
66 virtual void OnClicked(const std::string& notifcation_id) { 71 virtual void OnClicked(const std::string& notifcation_id) {
67 } 72 }
68 73
69 void AddNotification(WebNotificationTray* tray, const std::string& id) { 74 void AddNotification(WebNotificationTray* tray, const std::string& id) {
70 notification_ids_.insert(id); 75 notification_ids_.insert(id);
71 tray->message_center()->AddNotification( 76 get_message_center()->AddNotification(
72 ui::notifications::NOTIFICATION_TYPE_SIMPLE, 77 ui::notifications::NOTIFICATION_TYPE_SIMPLE,
73 id, 78 id,
74 ASCIIToUTF16("Test Web Notification"), 79 ASCIIToUTF16("Test Web Notification"),
75 ASCIIToUTF16("Notification message body."), 80 ASCIIToUTF16("Notification message body."),
76 ASCIIToUTF16("www.test.org"), 81 ASCIIToUTF16("www.test.org"),
77 "" /* extension id */, 82 "" /* extension id */,
78 NULL /* optional_fields */); 83 NULL /* optional_fields */);
79 } 84 }
80 85
81 void UpdateNotification(WebNotificationTray* tray, 86 void UpdateNotification(WebNotificationTray* tray,
82 const std::string& old_id, 87 const std::string& old_id,
83 const std::string& new_id) { 88 const std::string& new_id) {
84 notification_ids_.erase(old_id); 89 notification_ids_.erase(old_id);
85 notification_ids_.insert(new_id); 90 notification_ids_.insert(new_id);
86 tray->message_center()->UpdateNotification( 91 get_message_center()->UpdateNotification(
87 old_id, new_id, 92 old_id, new_id,
88 ASCIIToUTF16("Updated Web Notification"), 93 ASCIIToUTF16("Updated Web Notification"),
89 ASCIIToUTF16("Updated message body."), 94 ASCIIToUTF16("Updated message body."),
90 NULL); 95 NULL);
91 } 96 }
92 97
93 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { 98 void RemoveNotification(WebNotificationTray* tray, const std::string& id) {
94 tray->message_center()->RemoveNotification(id); 99 get_message_center()->RemoveNotification(id);
95 notification_ids_.erase(id); 100 notification_ids_.erase(id);
96 } 101 }
97 102
98 bool HasNotificationId(const std::string& id) { 103 bool HasNotificationId(const std::string& id) {
99 return notification_ids_.find(id) != notification_ids_.end(); 104 return notification_ids_.find(id) != notification_ids_.end();
100 } 105 }
101 106
102 private: 107 private:
103 std::set<std::string> notification_ids_; 108 std::set<std::string> notification_ids_;
104 message_center::MessageCenter* message_center_; 109 message_center::MessageCenter* message_center_;
105 110
106 DISALLOW_COPY_AND_ASSIGN(TestDelegate); 111 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
107 }; 112 };
108 113
109 } // namespace 114 } // namespace
110 115
111 typedef test::AshTestBase WebNotificationTrayTest; 116 typedef test::AshTestBase WebNotificationTrayTest;
112 117
113 TEST_F(WebNotificationTrayTest, WebNotifications) { 118 TEST_F(WebNotificationTrayTest, WebNotifications) {
114 WebNotificationTray* tray = GetWebNotificationTray(); 119 WebNotificationTray* tray = GetWebNotificationTray();
115 message_center::MessageCenter* message_center = tray->message_center(); 120 message_center::MessageCenter* message_center = tray->message_center();
116 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); 121 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center));
117 ASSERT_TRUE(tray->GetWidget()); 122 ASSERT_TRUE(tray->GetWidget());
118 123
119 // Add a notification. 124 // Add a notification.
120 delegate->AddNotification(tray, "test_id1"); 125 delegate->AddNotification(tray, "test_id1");
121 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); 126 EXPECT_EQ(1u, get_message_center()->NotificationCount());
122 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); 127 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1"));
123 delegate->AddNotification(tray, "test_id2"); 128 delegate->AddNotification(tray, "test_id2");
124 delegate->AddNotification(tray, "test_id2"); 129 delegate->AddNotification(tray, "test_id2");
125 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); 130 EXPECT_EQ(2u, get_message_center()->NotificationCount());
126 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); 131 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2"));
127 132
128 // Ensure that updating a notification does not affect the count. 133 // Ensure that updating a notification does not affect the count.
129 delegate->UpdateNotification(tray, "test_id2", "test_id3"); 134 delegate->UpdateNotification(tray, "test_id2", "test_id3");
130 delegate->UpdateNotification(tray, "test_id3", "test_id3"); 135 delegate->UpdateNotification(tray, "test_id3", "test_id3");
131 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); 136 EXPECT_EQ(2u, get_message_center()->NotificationCount());
132 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); 137 EXPECT_FALSE(delegate->HasNotificationId("test_id2"));
133 EXPECT_FALSE(message_center->notification_list()->HasNotification( 138 EXPECT_FALSE(message_center->notification_list()->HasNotification(
134 "test_id2")); 139 "test_id2"));
135 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); 140 EXPECT_TRUE(delegate->HasNotificationId("test_id3"));
136 141
137 // Ensure that Removing the first notification removes it from the tray. 142 // Ensure that Removing the first notification removes it from the tray.
138 delegate->RemoveNotification(tray, "test_id1"); 143 delegate->RemoveNotification(tray, "test_id1");
139 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); 144 EXPECT_FALSE(delegate->HasNotificationId("test_id1"));
140 EXPECT_FALSE(message_center->notification_list()->HasNotification( 145 EXPECT_FALSE(message_center->notification_list()->HasNotification(
141 "test_id1")); 146 "test_id1"));
142 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); 147 EXPECT_EQ(1u, get_message_center()->NotificationCount());
143 148
144 // Remove the remianing notification. 149 // Remove the remianing notification.
145 delegate->RemoveNotification(tray, "test_id3"); 150 delegate->RemoveNotification(tray, "test_id3");
146 EXPECT_EQ(0u, tray->message_center()->NotificationCount()); 151 EXPECT_EQ(0u, get_message_center()->NotificationCount());
147 EXPECT_FALSE(message_center->notification_list()->HasNotification( 152 EXPECT_FALSE(message_center->notification_list()->HasNotification(
148 "test_id3")); 153 "test_id3"));
149 } 154 }
150 155
151 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { 156 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) {
152 WebNotificationTray* tray = GetWebNotificationTray(); 157 WebNotificationTray* tray = GetWebNotificationTray();
153 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); 158 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center()));
154 159
155 ASSERT_TRUE(tray->GetWidget()); 160 ASSERT_TRUE(tray->GetWidget());
156 161
157 // Adding a notification should show the popup bubble. 162 // Adding a notification should show the popup bubble.
158 delegate->AddNotification(tray, "test_id1"); 163 delegate->AddNotification(tray, "test_id1");
159 EXPECT_TRUE(tray->popup_bubble() != NULL); 164 EXPECT_TRUE(tray->IsPopupVisible());
160 165
161 // Updating a notification should not hide the popup bubble. 166 // Updating a notification should not hide the popup bubble.
162 delegate->AddNotification(tray, "test_id2"); 167 delegate->AddNotification(tray, "test_id2");
163 delegate->UpdateNotification(tray, "test_id2", "test_id3"); 168 delegate->UpdateNotification(tray, "test_id2", "test_id3");
164 EXPECT_TRUE(tray->popup_bubble() != NULL); 169 EXPECT_TRUE(tray->IsPopupVisible());
165 170
166 // Removing the first notification should not hide the popup bubble. 171 // Removing the first notification should not hide the popup bubble.
167 delegate->RemoveNotification(tray, "test_id1"); 172 delegate->RemoveNotification(tray, "test_id1");
168 EXPECT_TRUE(tray->popup_bubble() != NULL); 173 EXPECT_TRUE(tray->IsPopupVisible());
169 174
170 // Removing the visible notification should hide the popup bubble. 175 // Removing the visible notification should hide the popup bubble.
171 delegate->RemoveNotification(tray, "test_id3"); 176 delegate->RemoveNotification(tray, "test_id3");
172 EXPECT_TRUE(tray->popup_bubble() == NULL); 177 EXPECT_FALSE(tray->IsPopupVisible());
173 } 178 }
174 179
175 using message_center::NotificationList; 180 using message_center::NotificationList;
176 181
177 182
178 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { 183 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) {
179 WebNotificationTray* tray = GetWebNotificationTray(); 184 WebNotificationTray* tray = GetWebNotificationTray();
180 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); 185 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center()));
181 186
182 // Add the max visible notifications +1, ensure the correct visible number. 187 // Add the max visible notifications +1, ensure the correct visible number.
183 size_t notifications_to_add = 188 size_t notifications_to_add =
184 NotificationList::kMaxVisibleMessageCenterNotifications + 1; 189 NotificationList::kMaxVisibleMessageCenterNotifications + 1;
185 for (size_t i = 0; i < notifications_to_add; ++i) { 190 for (size_t i = 0; i < notifications_to_add; ++i) {
186 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); 191 std::string id = StringPrintf("test_id%d", static_cast<int>(i));
187 delegate->AddNotification(tray, id); 192 delegate->AddNotification(tray, id);
188 } 193 }
189 tray->ShowMessageCenterBubble(); 194 bool shown = tray->message_center_tray_->ShowMessageCenterBubble();
195 EXPECT_TRUE(shown);
190 RunAllPendingInMessageLoop(); 196 RunAllPendingInMessageLoop();
191 EXPECT_TRUE(tray->message_center_bubble() != NULL); 197 EXPECT_TRUE(tray->message_center_bubble() != NULL);
192 EXPECT_EQ(notifications_to_add, 198 EXPECT_EQ(notifications_to_add,
193 tray->message_center()->NotificationCount()); 199 get_message_center()->NotificationCount());
194 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, 200 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications,
195 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); 201 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest());
196 } 202 }
197 203
198 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { 204 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) {
199 WebNotificationTray* tray = GetWebNotificationTray(); 205 WebNotificationTray* tray = GetWebNotificationTray();
200 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); 206 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center()));
201 207
202 // Add the max visible popup notifications +1, ensure the correct num visible. 208 // Add the max visible popup notifications +1, ensure the correct num visible.
203 size_t notifications_to_add = 209 size_t notifications_to_add =
204 NotificationList::kMaxVisiblePopupNotifications + 1; 210 NotificationList::kMaxVisiblePopupNotifications + 1;
205 for (size_t i = 0; i < notifications_to_add; ++i) { 211 for (size_t i = 0; i < notifications_to_add; ++i) {
206 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); 212 std::string id = StringPrintf("test_id%d", static_cast<int>(i));
207 delegate->AddNotification(tray, id); 213 delegate->AddNotification(tray, id);
208 } 214 }
209 // Hide and reshow the bubble so that it is updated immediately, not delayed. 215 // Hide and reshow the bubble so that it is updated immediately, not delayed.
210 tray->HidePopupBubble(); 216 tray->SetHidePopupBubble(true);
211 tray->ShowPopupBubble(); 217 tray->SetHidePopupBubble(false);
212 EXPECT_TRUE(tray->popup_bubble() != NULL); 218 EXPECT_TRUE(tray->IsPopupVisible());
213 EXPECT_EQ(notifications_to_add, 219 EXPECT_EQ(notifications_to_add,
214 tray->message_center()->NotificationCount()); 220 get_message_center()->NotificationCount());
215 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, 221 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications,
216 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); 222 tray->GetPopupBubbleForTest()->NumMessageViewsForTest());
223 get_message_center()->SetDelegate(NULL);
224 get_message_center()->notification_list()->RemoveAllNotifications();
217 } 225 }
218 226
219 } // namespace ash 227 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698