OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/browser_process.h" | |
6 #include "chrome/browser/extensions/api/notification/notification_api.h" | |
7 #include "chrome/browser/extensions/extension_apitest.h" | |
8 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
9 #include "chrome/common/chrome_notification_types.h" | |
10 #include "chrome/common/chrome_switches.h" | |
11 #include "content/public/browser/notification_service.h" | |
12 #include "content/public/test/test_utils.h" | |
13 #include "ui/message_center/message_center.h" | |
14 #include "ui/message_center/message_center_util.h" | |
15 | |
16 using extensions::Extension; | |
17 | |
18 namespace utils = extension_function_test_utils; | |
19 | |
20 namespace { | |
21 | |
22 class NotificationApiTest : public ExtensionApiTest { | |
23 public: | |
24 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
25 ExtensionApiTest::SetUpCommandLine(command_line); | |
26 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | |
27 } | |
28 | |
29 const extensions::Extension* LoadExtensionAndWait( | |
30 const std::string& test_name) { | |
31 base::FilePath extdir = test_data_dir_.AppendASCII(test_name); | |
32 content::WindowedNotificationObserver page_created( | |
33 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, | |
34 content::NotificationService::AllSources()); | |
35 const extensions::Extension* extension = LoadExtension(extdir); | |
36 if (extension) { | |
37 page_created.Wait(); | |
38 } | |
39 return extension; | |
40 } | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestIdUsage) { | |
46 // Create a new notification. A lingering output of this block is the | |
47 // notification ID, which we'll use in later parts of this test. | |
48 std::string notification_id; | |
49 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
50 { | |
51 scoped_refptr<extensions::NotificationCreateFunction> | |
52 notification_function( | |
53 new extensions::NotificationCreateFunction()); | |
54 | |
55 notification_function->set_extension(empty_extension.get()); | |
56 notification_function->set_has_callback(true); | |
57 | |
58 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
59 notification_function, | |
60 "[\"\", " // Empty string: ask API to generate ID | |
61 "{" | |
62 "\"templateType\": \"simple\"," | |
63 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
64 "\"title\": \"Attention!\"," | |
65 "\"message\": \"Check out Cirque du Soleil\"" | |
66 "}]", | |
67 browser(), utils::NONE)); | |
68 | |
69 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
70 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
71 ASSERT_TRUE(notification_id.length() > 0); | |
72 } | |
73 | |
74 // Update the existing notification. | |
75 { | |
76 scoped_refptr<extensions::NotificationUpdateFunction> | |
77 notification_function( | |
78 new extensions::NotificationUpdateFunction()); | |
79 | |
80 notification_function->set_extension(empty_extension.get()); | |
81 notification_function->set_has_callback(true); | |
82 | |
83 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
84 notification_function, | |
85 "[\"" + notification_id + "\", " | |
86 "{" | |
87 "\"templateType\": \"simple\"," | |
88 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
89 "\"title\": \"Attention!\"," | |
90 "\"message\": \"Too late! The show ended yesterday\"" | |
91 "}]", | |
92 browser(), utils::NONE)); | |
93 | |
94 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
95 bool copy_bool_value = false; | |
96 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
97 ASSERT_TRUE(copy_bool_value); | |
98 | |
99 // TODO(miket): add a testing method to query the message from the | |
100 // displayed notification, and assert it matches the updated message. | |
101 // | |
102 // TODO(miket): add a method to count the number of outstanding | |
103 // notifications, and confirm it remains at one at this point. | |
104 } | |
105 | |
106 // Update a nonexistent notification. | |
107 { | |
108 scoped_refptr<extensions::NotificationUpdateFunction> | |
109 notification_function( | |
110 new extensions::NotificationUpdateFunction()); | |
111 | |
112 notification_function->set_extension(empty_extension.get()); | |
113 notification_function->set_has_callback(true); | |
114 | |
115 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
116 notification_function, | |
117 "[\"xxxxxxxxxxxx\", " | |
118 "{" | |
119 "\"templateType\": \"simple\"," | |
120 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
121 "\"title\": \"!\"," | |
122 "\"message\": \"!\"" | |
123 "}]", | |
124 browser(), utils::NONE)); | |
125 | |
126 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
127 bool copy_bool_value = false; | |
128 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
129 ASSERT_FALSE(copy_bool_value); | |
130 } | |
131 | |
132 // Clear a nonexistent notification. | |
133 { | |
134 scoped_refptr<extensions::NotificationClearFunction> | |
135 notification_function( | |
136 new extensions::NotificationClearFunction()); | |
137 | |
138 notification_function->set_extension(empty_extension.get()); | |
139 notification_function->set_has_callback(true); | |
140 | |
141 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
142 notification_function, | |
143 "[\"xxxxxxxxxxx\"]", browser(), utils::NONE)); | |
144 | |
145 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
146 bool copy_bool_value = false; | |
147 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
148 ASSERT_FALSE(copy_bool_value); | |
149 } | |
150 | |
151 // Clear the notification we created. | |
152 { | |
153 scoped_refptr<extensions::NotificationClearFunction> | |
154 notification_function( | |
155 new extensions::NotificationClearFunction()); | |
156 | |
157 notification_function->set_extension(empty_extension.get()); | |
158 notification_function->set_has_callback(true); | |
159 | |
160 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
161 notification_function, | |
162 "[\"" + notification_id + "\"]", browser(), utils::NONE)); | |
163 | |
164 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
165 bool copy_bool_value = false; | |
166 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
167 ASSERT_TRUE(copy_bool_value); | |
168 } | |
169 } | |
170 | |
171 IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestBaseFormatNotification) { | |
172 scoped_refptr<extensions::NotificationCreateFunction> | |
173 notification_create_function( | |
174 new extensions::NotificationCreateFunction()); | |
175 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
176 | |
177 notification_create_function->set_extension(empty_extension.get()); | |
178 notification_create_function->set_has_callback(true); | |
179 | |
180 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
181 notification_create_function, | |
182 "[\"\", " | |
183 "{" | |
184 "\"templateType\": \"basic\"," | |
185 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
186 "\"title\": \"Attention!\"," | |
187 "\"message\": \"Check out Cirque du Soleil\"," | |
188 "\"priority\": 1," | |
189 "\"eventTime\": 1234567890.12345678," | |
190 "\"buttons\": [" | |
191 " {" | |
192 " \"title\": \"Up\"," | |
193 " \"iconUrl\":\"http://www.google.com/logos/2012/\"" | |
194 " }," | |
195 " {" | |
196 " \"title\": \"Down\"" // note: no iconUrl | |
197 " }" | |
198 "]," | |
199 "\"expandedMessage\": \"This is a longer expanded message.\"," | |
200 "\"imageUrl\": \"http://www.google.com/logos/2012/election12-hp.jpg\"" | |
201 "}]", | |
202 browser(), utils::NONE)); | |
203 | |
204 std::string notification_id; | |
205 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
206 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
207 ASSERT_TRUE(notification_id.length() > 0); | |
208 } | |
209 | |
210 IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestMultipleItemNotification) { | |
211 scoped_refptr<extensions::NotificationCreateFunction> | |
212 notification_create_function( | |
213 new extensions::NotificationCreateFunction()); | |
214 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
215 | |
216 notification_create_function->set_extension(empty_extension.get()); | |
217 notification_create_function->set_has_callback(true); | |
218 | |
219 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
220 notification_create_function, | |
221 "[\"\", " | |
222 "{" | |
223 "\"templateType\": \"list\"," | |
224 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
225 "\"title\": \"Multiple Item Notification Title\"," | |
226 "\"message\": \"Multiple item notification message.\"," | |
227 "\"items\": [" | |
228 " {\"title\": \"Brett Boe\"," | |
229 " \"message\": \"This is an important message!\"}," | |
230 " {\"title\": \"Carla Coe\"," | |
231 " \"message\": \"Just took a look at the proposal\"}," | |
232 " {\"title\": \"Donna Doe\"," | |
233 " \"message\": \"I see that you went to the conference\"}," | |
234 " {\"title\": \"Frank Foe\"," | |
235 " \"message\": \"I ate Harry's sandwich!\"}," | |
236 " {\"title\": \"Grace Goe\"," | |
237 " \"message\": \"I saw Frank steal a sandwich :-)\"}" | |
238 "]," | |
239 "\"priority\": 1," | |
240 "\"eventTime\": 1361488019.9999999" | |
241 "}]", | |
242 browser(), utils::NONE)); | |
243 // TODO(dharcourt): [...], items = [{title: foo, message: bar}, ...], [...] | |
244 | |
245 std::string notification_id; | |
246 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
247 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
248 ASSERT_TRUE(notification_id.length() > 0); | |
249 } | |
250 | |
251 IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestEvents) { | |
252 ASSERT_TRUE(RunExtensionTest("notification/api/events")) << message_; | |
253 } | |
254 | |
255 IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestCSP) { | |
256 ASSERT_TRUE(RunExtensionTest("notification/api/csp")) << message_; | |
257 } | |
258 | |
259 #ifdef ENABLE_MESSAGE_CENTER | |
260 #if !defined(OS_WIN) || !defined(USE_ASH) | |
261 | |
262 IN_PROC_BROWSER_TEST_F(NotificationApiTest, TestByUser) { | |
263 if (!message_center::IsRichNotificationEnabled()) | |
264 return; | |
265 | |
266 const extensions::Extension* extension = | |
267 LoadExtensionAndWait("notification/api/by_user"); | |
268 ASSERT_TRUE(extension) << message_; | |
269 | |
270 { | |
271 ResultCatcher catcher; | |
272 g_browser_process->message_center()->SendRemoveNotification( | |
273 extension->id() + "-FOO", | |
274 false); | |
275 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
276 } | |
277 | |
278 { | |
279 ResultCatcher catcher; | |
280 g_browser_process->message_center()->SendRemoveNotification( | |
281 extension->id() + "-BAR", | |
282 true); | |
283 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
284 } | |
285 | |
286 { | |
287 ResultCatcher catcher; | |
288 g_browser_process->message_center()->SendRemoveAllNotifications(false); | |
289 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
290 } | |
291 | |
292 { | |
293 ResultCatcher catcher; | |
294 g_browser_process->message_center()->SendRemoveAllNotifications(true); | |
295 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
296 } | |
297 } | |
298 | |
299 #endif | |
300 #endif | |
OLD | NEW |