Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/macros.h" | |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h" | 14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h" |
| 14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" | 15 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" |
| 16 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "chrome/common/render_messages.h" | |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 22 #include "components/search_engines/template_url_data.h" | |
| 23 #include "components/search_engines/template_url_service.h" | |
| 24 #include "content/public/browser/browser_message_filter.h" | |
| 25 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/navigation_controller.h" | 26 #include "content/public/browser/navigation_controller.h" |
| 20 #include "content/public/browser/navigation_entry.h" | 27 #include "content/public/browser/navigation_entry.h" |
| 21 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 29 #include "content/public/browser/render_process_host.h" | |
| 22 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/test/browser_test_utils.h" | 32 #include "content/public/test/browser_test_utils.h" |
| 33 #include "content/public/test/test_utils.h" | |
| 25 #include "third_party/WebKit/public/web/WebContextMenuData.h" | 34 #include "third_party/WebKit/public/web/WebContextMenuData.h" |
| 26 #include "third_party/WebKit/public/web/WebInputEvent.h" | 35 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 27 | 36 |
| 28 using content::WebContents; | 37 using content::WebContents; |
| 29 | 38 |
| 30 namespace { | 39 namespace { |
| 31 | 40 |
| 32 class ContextMenuBrowserTest : public InProcessBrowserTest { | 41 class ContextMenuBrowserTest : public InProcessBrowserTest { |
| 33 public: | 42 public: |
| 34 ContextMenuBrowserTest() { } | 43 ContextMenuBrowserTest() { } |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 TestRenderViewContextMenu menu(tab->GetMainFrame(), context_menu_params); | 290 TestRenderViewContextMenu menu(tab->GetMainFrame(), context_menu_params); |
| 282 menu.Init(); | 291 menu.Init(); |
| 283 | 292 |
| 284 // The item shouldn't be enabled in the menu. | 293 // The item shouldn't be enabled in the menu. |
| 285 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); | 294 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); |
| 286 | 295 |
| 287 // Ensure that viewing page info doesn't crash even if you can get to it. | 296 // Ensure that viewing page info doesn't crash even if you can get to it. |
| 288 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); | 297 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); |
| 289 } | 298 } |
| 290 | 299 |
| 300 class ThumbnailResponseWatcher : public content::NotificationObserver { | |
| 301 public: | |
| 302 enum QuitReason { | |
| 303 STILL_RUNNING = 0, | |
| 304 THUMBNAIL_RECEIVED, | |
| 305 RENDER_PROCESS_GONE, | |
| 306 }; | |
| 307 | |
| 308 class MessageFilter : public content::BrowserMessageFilter { | |
| 309 public: | |
| 310 explicit MessageFilter(ThumbnailResponseWatcher* owner) | |
| 311 : content::BrowserMessageFilter(ChromeMsgStart), owner_(owner) {} | |
| 312 | |
| 313 bool OnMessageReceived(const IPC::Message& message) override { | |
| 314 if (message.type() == | |
| 315 ChromeViewHostMsg_RequestThumbnailForContextNode_ACK::ID) { | |
| 316 content::BrowserThread::PostTask( | |
| 317 content::BrowserThread::UI, FROM_HERE, | |
| 318 base::Bind(&MessageFilter::OnRequestThumbnailForContextNodeACK, | |
| 319 this)); | |
| 320 } | |
| 321 return false; | |
| 322 } | |
| 323 | |
| 324 void OnRequestThumbnailForContextNodeACK() { | |
| 325 if (owner_) | |
| 326 owner_->OnRequestThumbnailForContextNodeACK(); | |
| 327 } | |
| 328 | |
| 329 void Disown() { owner_ = nullptr; } | |
| 330 | |
| 331 private: | |
| 332 ~MessageFilter() override {} | |
| 333 | |
| 334 ThumbnailResponseWatcher* owner_; | |
| 335 | |
| 336 DISALLOW_COPY_AND_ASSIGN(MessageFilter); | |
| 337 }; | |
| 338 | |
| 339 explicit ThumbnailResponseWatcher( | |
| 340 content::RenderProcessHost* render_process_host) | |
| 341 : message_loop_runner_(new content::MessageLoopRunner), | |
| 342 filter_(new MessageFilter(this)), | |
| 343 quit_reason_(STILL_RUNNING) { | |
| 344 notification_registrar_.Add( | |
| 345 this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 346 content::Source<content::RenderProcessHost>(render_process_host)); | |
| 347 notification_registrar_.Add( | |
| 348 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 349 content::Source<content::RenderProcessHost>(render_process_host)); | |
| 350 render_process_host->AddFilter(filter_.get()); | |
| 351 } | |
| 352 | |
| 353 ~ThumbnailResponseWatcher() override { filter_->Disown(); } | |
| 354 | |
| 355 QuitReason Wait() WARN_UNUSED_RESULT { | |
| 356 message_loop_runner_->Run(); | |
| 357 DCHECK_NE(STILL_RUNNING, quit_reason_); | |
| 358 return quit_reason_; | |
| 359 } | |
| 360 | |
| 361 void Observe(int type, | |
| 362 const content::NotificationSource& source, | |
| 363 const content::NotificationDetails& details) override { | |
| 364 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || | |
| 365 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | |
| 366 quit_reason_ = RENDER_PROCESS_GONE; | |
| 367 message_loop_runner_->Quit(); | |
| 368 } | |
| 369 | |
| 370 void OnRequestThumbnailForContextNodeACK() { | |
| 371 quit_reason_ = THUMBNAIL_RECEIVED; | |
| 372 message_loop_runner_->Quit(); | |
| 373 } | |
| 374 | |
| 375 private: | |
| 376 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
|
Johnny(Jianning) Ding
2014/12/12 02:48:02
Please include header file base/memory/ref_counted
jbroman
2014/12/12 03:34:39
Done.
| |
| 377 scoped_refptr<MessageFilter> filter_; | |
| 378 content::NotificationRegistrar notification_registrar_; | |
| 379 QuitReason quit_reason_; | |
| 380 | |
| 381 DISALLOW_COPY_AND_ASSIGN(ThumbnailResponseWatcher); | |
| 382 }; | |
| 383 | |
| 384 // Maintains image search test state. In particular, note that |menu_observer_| | |
| 385 // must live until the right-click completes asynchronously. | |
| 386 class SearchByImageBrowserTest : public InProcessBrowserTest { | |
| 387 protected: | |
| 388 void SetupAndLoadImagePage(const std::string& image_path) { | |
| 389 // The test server must start first, so that we know the port that the test | |
| 390 // server is using. | |
| 391 ASSERT_TRUE(test_server()->Start()); | |
| 392 SetupImageSearchEngine(); | |
| 393 | |
| 394 // Go to a page with an image in it. The test server doesn't serve the image | |
| 395 // with the right MIME type, so use a data URL to make a page containing it. | |
| 396 GURL image_url(test_server()->GetURL(image_path)); | |
| 397 GURL page("data:text/html,<img src='" + image_url.spec() + "'>"); | |
| 398 ui_test_utils::NavigateToURL(browser(), page); | |
| 399 } | |
| 400 | |
| 401 void AttemptImageSearch() { | |
| 402 // Right-click where the image should be. | |
| 403 // |menu_observer_| will cause the search-by-image menu item to be clicked. | |
| 404 menu_observer_.reset(new ContextMenuNotificationObserver( | |
| 405 IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE)); | |
| 406 content::WebContents* tab = | |
| 407 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 408 content::SimulateMouseClickAt(tab, 0, blink::WebMouseEvent::ButtonRight, | |
| 409 gfx::Point(15, 15)); | |
| 410 } | |
| 411 | |
| 412 GURL GetImageSearchURL() { | |
| 413 static const char kImageSearchURL[] = "imagesearch"; | |
| 414 return test_server()->GetURL(kImageSearchURL); | |
| 415 } | |
| 416 | |
| 417 private: | |
| 418 void SetupImageSearchEngine() { | |
| 419 static const char kShortName[] = "test"; | |
| 420 static const char kSearchURL[] = "search?q={searchTerms}"; | |
| 421 static const char kImageSearchPostParams[] = | |
| 422 "thumb={google:imageThumbnail}"; | |
| 423 | |
| 424 TemplateURLService* model = | |
| 425 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | |
| 426 ASSERT_TRUE(model); | |
| 427 ui_test_utils::WaitForTemplateURLServiceToLoad(model); | |
| 428 ASSERT_TRUE(model->loaded()); | |
| 429 | |
| 430 TemplateURLData data; | |
| 431 data.short_name = base::ASCIIToUTF16(kShortName); | |
| 432 data.SetKeyword(data.short_name); | |
| 433 data.SetURL(test_server()->GetURL(kSearchURL).spec()); | |
| 434 data.image_url = GetImageSearchURL().spec(); | |
| 435 data.image_url_post_params = kImageSearchPostParams; | |
| 436 | |
| 437 // The model takes ownership of |template_url|. | |
| 438 TemplateURL* template_url = new TemplateURL(data); | |
| 439 ASSERT_TRUE(model->Add(template_url)); | |
| 440 model->SetUserSelectedDefaultSearchProvider(template_url); | |
| 441 } | |
| 442 | |
| 443 void TearDownInProcessBrowserTestFixture() override { | |
| 444 menu_observer_.reset(); | |
| 445 } | |
| 446 | |
| 447 scoped_ptr<ContextMenuNotificationObserver> menu_observer_; | |
| 448 }; | |
| 449 | |
| 450 IN_PROC_BROWSER_TEST_F(SearchByImageBrowserTest, ImageSearchWithValidImage) { | |
| 451 static const char kValidImage[] = "files/image_search/valid.png"; | |
| 452 SetupAndLoadImagePage(kValidImage); | |
| 453 | |
| 454 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( | |
| 455 content::NotificationService::AllSources()); | |
| 456 AttemptImageSearch(); | |
| 457 | |
| 458 // The browser should open a new tab for an image search. | |
| 459 tab_observer.Wait(); | |
| 460 content::WebContents* new_tab = tab_observer.GetTab(); | |
| 461 content::WaitForLoadStop(new_tab); | |
| 462 EXPECT_EQ(GetImageSearchURL(), new_tab->GetURL()); | |
| 463 } | |
| 464 | |
| 465 IN_PROC_BROWSER_TEST_F(SearchByImageBrowserTest, ImageSearchWithCorruptImage) { | |
| 466 static const char kCorruptImage[] = "files/image_search/corrupt.png"; | |
| 467 SetupAndLoadImagePage(kCorruptImage); | |
| 468 | |
| 469 content::WebContents* tab = | |
| 470 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 471 ThumbnailResponseWatcher watcher(tab->GetRenderProcessHost()); | |
| 472 AttemptImageSearch(); | |
| 473 | |
| 474 // The browser should receive a response from the renderer, because the | |
| 475 // renderer should not crash. | |
| 476 EXPECT_EQ(ThumbnailResponseWatcher::THUMBNAIL_RECEIVED, watcher.Wait()); | |
| 477 } | |
| 478 | |
| 291 } // namespace | 479 } // namespace |
| OLD | NEW |