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

Side by Side Diff: chrome/browser/extensions/extension_uitest.cc

Issue 173034: Validation of extension api callback and event parameters in DEBUG (Closed)
Patch Set: build docs Created 11 years, 4 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
« no previous file with comments | « no previous file | chrome/common/common_resources.grd » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/gfx/rect.h" 6 #include "base/gfx/rect.h"
7 #include "base/json_reader.h" 7 #include "base/json_reader.h"
8 #include "base/json_writer.h" 8 #include "base/json_writer.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/automation/extension_automation_constants.h" 10 #include "chrome/browser/automation/extension_automation_constants.h"
11 #include "chrome/browser/extensions/extension_tabs_module_constants.h"
11 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
13 #include "chrome/test/automation/automation_messages.h" 14 #include "chrome/test/automation/automation_messages.h"
14 #include "chrome/test/automation/automation_proxy_uitest.h" 15 #include "chrome/test/automation/automation_proxy_uitest.h"
15 #include "chrome/test/automation/tab_proxy.h" 16 #include "chrome/test/automation/tab_proxy.h"
16 #include "chrome/test/ui/ui_test.h" 17 #include "chrome/test/ui/ui_test.h"
17 #include "googleurl/src/gurl.h" 18 #include "googleurl/src/gurl.h"
18 19
19 namespace { 20 namespace {
20 21
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 ASSERT_TRUE(request_dict->GetString(keys::kAutomationNameKey, 219 ASSERT_TRUE(request_dict->GetString(keys::kAutomationNameKey,
219 &function_name)); 220 &function_name));
220 int request_id = -2; 221 int request_id = -2;
221 EXPECT_TRUE(request_dict->GetInteger(keys::kAutomationRequestIdKey, 222 EXPECT_TRUE(request_dict->GetInteger(keys::kAutomationRequestIdKey,
222 &request_id)); 223 &request_id));
223 bool has_callback = false; 224 bool has_callback = false;
224 EXPECT_TRUE(request_dict->GetBoolean(keys::kAutomationHasCallbackKey, 225 EXPECT_TRUE(request_dict->GetBoolean(keys::kAutomationHasCallbackKey,
225 &has_callback)); 226 &has_callback));
226 227
227 if (messages_received_ == 1) { 228 if (messages_received_ == 1) {
228 EXPECT_EQ(function_name, "windows.getLastFocused"); 229 EXPECT_EQ(function_name, "tabs.getSelected");
229 EXPECT_GE(request_id, 0); 230 EXPECT_GE(request_id, 0);
230 EXPECT_TRUE(has_callback); 231 EXPECT_TRUE(has_callback);
231 232
232 DictionaryValue response_dict; 233 DictionaryValue response_dict;
233 EXPECT_TRUE(response_dict.SetInteger(keys::kAutomationRequestIdKey, 234 EXPECT_TRUE(response_dict.SetInteger(keys::kAutomationRequestIdKey,
234 request_id)); 235 request_id));
235 EXPECT_TRUE(response_dict.SetString(keys::kAutomationResponseKey, "42")); 236 DictionaryValue tab_dict;
237 EXPECT_TRUE(tab_dict.SetInteger(extension_tabs_module_constants::kIdKey,
238 42));
239 EXPECT_TRUE(tab_dict.SetInteger(
240 extension_tabs_module_constants::kIndexKey, 1));
241 EXPECT_TRUE(tab_dict.SetInteger(
242 extension_tabs_module_constants::kWindowIdKey, 1));
243 EXPECT_TRUE(tab_dict.SetBoolean(
244 extension_tabs_module_constants::kSelectedKey, true));
245 EXPECT_TRUE(tab_dict.SetString(
246 extension_tabs_module_constants::kUrlKey, "http://www.google.com"));
247
248 std::string tab_json;
249 JSONWriter::Write(&tab_dict, false, &tab_json);
250
251 EXPECT_TRUE(response_dict.SetString(keys::kAutomationResponseKey, tab_json ));
236 252
237 std::string response_json; 253 std::string response_json;
238 JSONWriter::Write(&response_dict, false, &response_json); 254 JSONWriter::Write(&response_dict, false, &response_json);
239 255
240 tab_->HandleMessageFromExternalHost( 256 tab_->HandleMessageFromExternalHost(
241 response_json, 257 response_json,
242 keys::kAutomationOrigin, 258 keys::kAutomationOrigin,
243 keys::kAutomationResponseTarget); 259 keys::kAutomationResponseTarget);
244 } else if (messages_received_ == 2) { 260 } else if (messages_received_ == 2) {
245 EXPECT_EQ(function_name, "tabs.remove"); 261 EXPECT_EQ(function_name, "tabs.remove");
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 tab_(NULL) { 316 tab_(NULL) {
301 } 317 }
302 318
303 // Must set before initiating test. 319 // Must set before initiating test.
304 TabProxy* tab_; 320 TabProxy* tab_;
305 321
306 // Counts the number of times we got a given event. 322 // Counts the number of times we got a given event.
307 std::map<std::string, int> event_count_; 323 std::map<std::string, int> event_count_;
308 324
309 // Array containing the names of the events to fire to the extension. 325 // Array containing the names of the events to fire to the extension.
310 static const char* event_names_[]; 326 static const char* events_[];
311 327
312 protected: 328 protected:
313 // Process a message received from the test extension. 329 // Process a message received from the test extension.
314 virtual void HandleMessageFromChrome(); 330 virtual void HandleMessageFromChrome();
315 331
316 // Fire an event of the given name to the test extension. 332 // Fire an event of the given name to the test extension.
317 void FireEvent(const char* event_name); 333 void FireEvent(const char* event_name);
318 }; 334 };
319 335
320 const char* BrowserEventAutomationProxy::event_names_[] = { 336 const char* BrowserEventAutomationProxy::events_[] = {
321 // Window events. 337 // Window events.
322 "windows.onCreated", 338 "[\"windows.onCreated\", \"[42]\"]",
323 "windows.onRemoved", 339
324 "windows.onFocusChanged", 340 "[\"windows.onRemoved\", \"[42]\"]",
325 341
342 "[\"windows.onFocusChanged\", \"[42]\"]",
343
326 // Tab events. 344 // Tab events.
327 "tabs.onCreated", 345 "[\"tabs.onCreated\", \"[{'id\':42,'index':1,'windowId':1,"
328 "tabs.onUpdated", 346 "'selected':true,'url':'http://www.google.com'}]\"]",
329 "tabs.onMoved", 347
330 "tabs.onSelectionChanged", 348 "[\"tabs.onUpdated\", \"[42, {'status': 'complete',"
331 "tabs.onAttached", 349 "'url':'http://www.google.com'}]\"]",
332 "tabs.onDetached", 350
333 "tabs.onRemoved", 351 "[\"tabs.onMoved\", \"[42, {'windowId':1,'fromIndex':1,'toIndex':2}]\"]",
352
353 "[\"tabs.onSelectionChanged\", \"[42, {'windowId':1}]\"]",
354
355 "[\"tabs.onAttached\", \"[42, {'newWindowId':1,'newPosition':1}]\"]",
356
357 "[\"tabs.onDetached\", \"[43, {'oldWindowId':1,'oldPosition':1}]\"]",
358
359 "[\"tabs.onRemoved\", \"[43]\"]",
334 360
335 // Bookmark events. 361 // Bookmark events.
336 "bookmarks.onAdded", 362 "[\"bookmarks.onAdded\", \"['42', {'id':'42','title':'foo',}]\"]",
337 "bookmarks.onRemoved", 363
338 "bookmarks.onChanged", 364 "[\"bookmarks.onRemoved\", \"['42', {'parentId':'2','index':1}]\"]",
339 "bookmarks.onMoved", 365
340 "bookmarks.onChildrenReordered", 366 "[\"bookmarks.onChanged\", \"['42', {'title':'foo'}]\"]",
367
368 "[\"bookmarks.onMoved\", \"['42', {'parentId':'2','index':1,"
369 "'oldParentId':'3','oldIndex':2}]\"]",
370
371 "[\"bookmarks.onChildrenReordered\", \"['32', ['1', '2', '3']]\"]"
341 }; 372 };
342 373
343 void BrowserEventAutomationProxy::HandleMessageFromChrome() { 374 void BrowserEventAutomationProxy::HandleMessageFromChrome() {
344 namespace keys = extension_automation_constants; 375 namespace keys = extension_automation_constants;
345 ASSERT_TRUE(tab_ != NULL); 376 ASSERT_TRUE(tab_ != NULL);
346 377
347 std::string message(message()); 378 std::string message(message());
348 std::string origin(origin()); 379 std::string origin(origin());
349 std::string target(target()); 380 std::string target(target());
350 381
(...skipping 18 matching lines...) Expand all
369 // TEST_F(BrowserEventExtensionTest, RunTest) to understand where the 400 // TEST_F(BrowserEventExtensionTest, RunTest) to understand where the
370 // extension Id comes from. 401 // extension Id comes from.
371 tab_->HandleMessageFromExternalHost( 402 tab_->HandleMessageFromExternalHost(
372 "{\"rqid\":0, \"extid\": \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"," 403 "{\"rqid\":0, \"extid\": \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\","
373 " \"connid\": 1}", 404 " \"connid\": 1}",
374 keys::kAutomationOrigin, 405 keys::kAutomationOrigin,
375 keys::kAutomationPortRequestTarget); 406 keys::kAutomationPortRequestTarget);
376 } else if (target == keys::kAutomationPortResponseTarget) { 407 } else if (target == keys::kAutomationPortResponseTarget) {
377 // This is a response to the open channel request. This means we know 408 // This is a response to the open channel request. This means we know
378 // that the port is ready to send us messages. Fire all the events now. 409 // that the port is ready to send us messages. Fire all the events now.
379 for (int i = 0; i < arraysize(event_names_); ++i) { 410 for (int i = 0; i < arraysize(events_); ++i) {
380 FireEvent(event_names_[i]); 411 FireEvent(events_[i]);
381 } 412 }
382 } else if (target == keys::kAutomationPortRequestTarget) { 413 } else if (target == keys::kAutomationPortRequestTarget) {
383 // This is the test extension calling us back. Make sure its telling 414 // This is the test extension calling us back. Make sure its telling
384 // us that it received an event. We do this by checking to see if the 415 // us that it received an event. We do this by checking to see if the
385 // message is a simple string of one of the event names that is fired. 416 // message is a simple string of one of the event names that is fired.
386 // 417 //
387 // There is a special message "ACK" which means that the extension 418 // There is a special message "ACK" which means that the extension
388 // received the port connection. This is not an event response and 419 // received the port connection. This is not an event response and
389 // should happen before all events. 420 // should happen before all events.
390 scoped_ptr<Value> message_value(JSONReader::Read(message, false)); 421 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
391 ASSERT_TRUE(message_value->IsType(Value::TYPE_DICTIONARY)); 422 ASSERT_TRUE(message_value->IsType(Value::TYPE_DICTIONARY));
392 DictionaryValue* message_dict = 423 DictionaryValue* message_dict =
393 reinterpret_cast<DictionaryValue*>(message_value.get()); 424 reinterpret_cast<DictionaryValue*>(message_value.get());
394 425
395 std::string event_name; 426 std::string event_name;
396 ASSERT_TRUE(message_dict->GetString(L"data", &event_name)); 427 ASSERT_TRUE(message_dict->GetString(L"data", &event_name));
397 if (event_name == "\"ACK\"") { 428 if (event_name == "\"ACK\"") {
398 ASSERT_EQ(0, event_count_.size()); 429 ASSERT_EQ(0, event_count_.size());
399 } else { 430 } else {
400 ++event_count_[event_name]; 431 ++event_count_[event_name];
401 } 432 }
402 } 433 }
403 } 434 }
404 435
405 void BrowserEventAutomationProxy::FireEvent(const char* event_name) { 436 void BrowserEventAutomationProxy::FireEvent(const char* event) {
406 namespace keys = extension_automation_constants; 437 namespace keys = extension_automation_constants;
407 438
408 // Build the event message to send to the extension. The only important 439 // Build the event message to send to the extension. The only important
409 // part is the name, as the payload is not used by the test extension. 440 // part is the name, as the payload is not used by the test extension.
410 std::string message; 441 std::string message;
411 message += "[\""; 442 message += event;
412 message += event_name;
413 message += "\", \"[]\"]";
414 443
415 tab_->HandleMessageFromExternalHost( 444 tab_->HandleMessageFromExternalHost(
416 message, 445 message,
417 keys::kAutomationOrigin, 446 keys::kAutomationOrigin,
418 keys::kAutomationBrowserEventRequestTarget); 447 keys::kAutomationBrowserEventRequestTarget);
419 } 448 }
420 449
421 class BrowserEventExtensionTest 450 class BrowserEventExtensionTest
422 : public ExtensionUITest< 451 : public ExtensionUITest<
423 CustomAutomationProxyTest<BrowserEventAutomationProxy>> { 452 CustomAutomationProxyTest<BrowserEventAutomationProxy>> {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 Extension::ResetGeneratedIdCounter(); 484 Extension::ResetGeneratedIdCounter();
456 TestWithURL(GURL( 485 TestWithURL(GURL(
457 "chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test.html")); 486 "chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test.html"));
458 BrowserEventAutomationProxy* proxy = 487 BrowserEventAutomationProxy* proxy =
459 static_cast<BrowserEventAutomationProxy*>(automation()); 488 static_cast<BrowserEventAutomationProxy*>(automation());
460 489
461 // If this assert hits and the actual size is 0 then you need to look at: 490 // If this assert hits and the actual size is 0 then you need to look at:
462 // src\chrome\test\data\extensions\uitest\event_sink\test.html and see if 491 // src\chrome\test\data\extensions\uitest\event_sink\test.html and see if
463 // all the events we are attaching to are valid. Also compare the list against 492 // all the events we are attaching to are valid. Also compare the list against
464 // the event_names_ string array above. 493 // the event_names_ string array above.
465 EXPECT_EQ(arraysize(BrowserEventAutomationProxy::event_names_), 494 EXPECT_EQ(arraysize(BrowserEventAutomationProxy::events_),
466 proxy->event_count_.size()); 495 proxy->event_count_.size());
467 for (std::map<std::string, int>::iterator i = proxy->event_count_.begin(); 496 for (std::map<std::string, int>::iterator i = proxy->event_count_.begin();
468 i != proxy->event_count_.end(); ++i) { 497 i != proxy->event_count_.end(); ++i) {
469 const std::pair<std::string, int>& value = *i; 498 const std::pair<std::string, int>& value = *i;
470 ASSERT_EQ(1, value.second); 499 ASSERT_EQ(1, value.second);
471 } 500 }
472 } 501 }
473 #endif // defined(OS_WIN) 502 #endif // defined(OS_WIN)
474 503
475 } // namespace 504 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/common/common_resources.grd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698