OLD | NEW |
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/system/status_area_widget.h" | 9 #include "ash/system/status_area_widget.h" |
10 #include "ash/system/tray/system_tray_item.h" | 10 #include "ash/system/tray/system_tray_item.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 void AddNotification(WebNotificationTray* tray, const std::string& id) { | 49 void AddNotification(WebNotificationTray* tray, const std::string& id) { |
50 notification_ids_.insert(id); | 50 notification_ids_.insert(id); |
51 tray->AddNotification(id, | 51 tray->AddNotification(id, |
52 ASCIIToUTF16("Test Web Notification"), | 52 ASCIIToUTF16("Test Web Notification"), |
53 ASCIIToUTF16("Notification message body."), | 53 ASCIIToUTF16("Notification message body."), |
54 ASCIIToUTF16("www.test.org"), | 54 ASCIIToUTF16("www.test.org"), |
55 "" /* extension id */); | 55 "" /* extension id */); |
56 } | 56 } |
57 | 57 |
| 58 void UpdateNotification(WebNotificationTray* tray, |
| 59 const std::string& old_id, |
| 60 const std::string& new_id) { |
| 61 notification_ids_.erase(old_id); |
| 62 notification_ids_.insert(new_id); |
| 63 tray->UpdateNotification(old_id, new_id, |
| 64 ASCIIToUTF16("Updated Web Notification"), |
| 65 ASCIIToUTF16("Updated message body.")); |
| 66 } |
| 67 |
58 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { | 68 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { |
59 tray->RemoveNotification(id); | 69 tray->RemoveNotification(id); |
| 70 notification_ids_.erase(id); |
60 } | 71 } |
61 | 72 |
62 bool HasNotificationId(const std::string& id) { | 73 bool HasNotificationId(const std::string& id) { |
63 return notification_ids_.find(id) != notification_ids_.end(); | 74 return notification_ids_.find(id) != notification_ids_.end(); |
64 } | 75 } |
65 | 76 |
66 private: | 77 private: |
67 std::set<std::string> notification_ids_; | 78 std::set<std::string> notification_ids_; |
68 | 79 |
69 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 80 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
70 }; | 81 }; |
71 | 82 |
72 } // namespace | 83 } // namespace |
73 | 84 |
74 typedef test::AshTestBase WebNotificationTrayTest; | 85 typedef test::AshTestBase WebNotificationTrayTest; |
75 | 86 |
76 TEST_F(WebNotificationTrayTest, WebNotifications) { | 87 TEST_F(WebNotificationTrayTest, WebNotifications) { |
77 WebNotificationTray* tray = GetWebNotificationTray(); | 88 WebNotificationTray* tray = GetWebNotificationTray(); |
78 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 89 scoped_ptr<TestDelegate> delegate(new TestDelegate); |
79 tray->SetDelegate(delegate.get()); | 90 tray->SetDelegate(delegate.get()); |
80 | 91 |
81 ASSERT_TRUE(tray->GetWidget()); | 92 ASSERT_TRUE(tray->GetWidget()); |
82 | 93 |
| 94 // Add a notification. |
| 95 delegate->AddNotification(tray, "test_id1"); |
| 96 EXPECT_EQ(1, tray->GetNotificationCount()); |
| 97 EXPECT_TRUE(tray->HasNotificationForTest("test_id1")); |
| 98 delegate->AddNotification(tray, "test_id2"); |
| 99 delegate->AddNotification(tray, "test_id2"); |
| 100 EXPECT_EQ(2, tray->GetNotificationCount()); |
| 101 EXPECT_TRUE(tray->HasNotificationForTest("test_id2")); |
| 102 |
| 103 // Ensure that updating a notification does not affect the count. |
| 104 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 105 delegate->UpdateNotification(tray, "test_id3", "test_id3"); |
| 106 EXPECT_EQ(2, tray->GetNotificationCount()); |
| 107 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); |
| 108 EXPECT_FALSE(tray->HasNotificationForTest("test_id2")); |
| 109 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); |
| 110 |
| 111 // Ensure that Removing the first notification removes it from the tray. |
| 112 delegate->RemoveNotification(tray, "test_id1"); |
| 113 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); |
| 114 EXPECT_FALSE(tray->HasNotificationForTest("test_id1")); |
| 115 EXPECT_EQ(1, tray->GetNotificationCount()); |
| 116 |
| 117 // Remove the remianing notification. |
| 118 delegate->RemoveNotification(tray, "test_id3"); |
| 119 EXPECT_EQ(0, tray->GetNotificationCount()); |
| 120 EXPECT_FALSE(tray->HasNotificationForTest("test_id3")); |
| 121 } |
| 122 |
| 123 TEST_F(WebNotificationTrayTest, WebNotificationBubble) { |
| 124 WebNotificationTray* tray = GetWebNotificationTray(); |
| 125 scoped_ptr<TestDelegate> delegate(new TestDelegate); |
| 126 tray->SetDelegate(delegate.get()); |
| 127 |
| 128 ASSERT_TRUE(tray->GetWidget()); |
| 129 |
83 // Adding a notification should show the bubble. | 130 // Adding a notification should show the bubble. |
84 delegate->AddNotification(tray, "test_id1"); | 131 delegate->AddNotification(tray, "test_id1"); |
85 EXPECT_TRUE(tray->notification_bubble() != NULL); | 132 EXPECT_TRUE(tray->notification_bubble() != NULL); |
86 EXPECT_EQ(1, tray->GetNotificationCount()); | 133 |
| 134 // Updating a notification should not hide the bubble. |
87 delegate->AddNotification(tray, "test_id2"); | 135 delegate->AddNotification(tray, "test_id2"); |
88 delegate->AddNotification(tray, "test_id2"); | 136 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
89 EXPECT_EQ(2, tray->GetNotificationCount()); | 137 EXPECT_TRUE(tray->notification_bubble() != NULL); |
90 // Ensure that removing a notification removes it from the tray, and signals | |
91 // the delegate. | |
92 EXPECT_TRUE(delegate->HasNotificationId("test_id2")); | |
93 delegate->RemoveNotification(tray, "test_id2"); | |
94 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); | |
95 EXPECT_EQ(1, tray->GetNotificationCount()); | |
96 | 138 |
97 // Removing the last notification should hide the bubble. | 139 // Removing the first notification should not hide the bubble. |
98 delegate->RemoveNotification(tray, "test_id1"); | 140 delegate->RemoveNotification(tray, "test_id1"); |
99 EXPECT_EQ(0, tray->GetNotificationCount()); | 141 EXPECT_TRUE(tray->notification_bubble() != NULL); |
| 142 |
| 143 // Removing the visible notification should hide the bubble. |
| 144 delegate->RemoveNotification(tray, "test_id3"); |
100 EXPECT_TRUE(tray->notification_bubble() == NULL); | 145 EXPECT_TRUE(tray->notification_bubble() == NULL); |
101 } | 146 } |
102 | 147 |
103 } // namespace ash | 148 } // namespace ash |
OLD | NEW |