OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_window.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/interactive_test_utils.h" |
| 14 #include "content/public/browser/native_web_keyboard_event.h" |
| 15 #include "content/public/browser/render_widget_host.h" |
| 16 #include "content/public/browser/render_widget_host_view.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #import "testing/gtest_mac.h" |
| 19 |
| 20 using WebContentsViewMacInteractiveTest = InProcessBrowserTest; |
| 21 |
| 22 // Integration test for a <select> popup run by the Mac-specific |
| 23 // content::PopupMenuHelper, owned by a WebContentsViewMac. |
| 24 IN_PROC_BROWSER_TEST_F(WebContentsViewMacInteractiveTest, SelectMenuLifetime) { |
| 25 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 26 EXPECT_TRUE(embedded_test_server()->Start()); |
| 27 |
| 28 // Open a tab with a <select> element, which starts focused. |
| 29 AddTabAtIndex(1, GURL(embedded_test_server()->GetURL("/select.html")), |
| 30 ui::PAGE_TRANSITION_LINK); |
| 31 |
| 32 base::RunLoop outer_run_loop; |
| 33 base::RunLoop* outer_run_loop_for_block = &outer_run_loop; |
| 34 __block base::scoped_nsobject<NSString> first_item; |
| 35 |
| 36 // Wait for a native menu to open. |
| 37 id token = [[NSNotificationCenter defaultCenter] |
| 38 addObserverForName:NSMenuDidBeginTrackingNotification |
| 39 object:nil |
| 40 queue:nil |
| 41 usingBlock:^(NSNotification* notification) { |
| 42 first_item.reset( |
| 43 [[[[notification object] itemAtIndex:0] title] copy]); |
| 44 // The nested run loop runs next. Ensure the outer run loop |
| 45 // exits once the inner run loop quits. |
| 46 outer_run_loop_for_block->Quit(); |
| 47 |
| 48 // We can't cancel tracking until after |
| 49 // NSMenuDidBeginTrackingNotification is processed (i.e. after |
| 50 // this block returns). So post a task to run on the inner run |
| 51 // loop which will close the tab (and cancel tracking in |
| 52 // ~PopupMenuHelper()). |
| 53 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 54 FROM_HERE, |
| 55 base::Bind( |
| 56 base::IgnoreResult(&TabStripModel::CloseWebContentsAt), |
| 57 base::Unretained(browser()->tab_strip_model()), 1, 0)); |
| 58 }]; |
| 59 |
| 60 // Send a space key to open the <select>. |
| 61 content::NativeWebKeyboardEvent event( |
| 62 blink::WebKeyboardEvent::kChar, blink::WebInputEvent::kNoModifiers, |
| 63 blink::WebInputEvent::kTimeStampForTesting); |
| 64 event.text[0] = ' '; |
| 65 browser() |
| 66 ->tab_strip_model() |
| 67 ->GetActiveWebContents() |
| 68 ->GetRenderWidgetHostView() |
| 69 ->GetRenderWidgetHost() |
| 70 ->ForwardKeyboardEvent(event); |
| 71 |
| 72 EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 73 outer_run_loop.Run(); |
| 74 |
| 75 [[NSNotificationCenter defaultCenter] removeObserver:token]; |
| 76 |
| 77 EXPECT_NSEQ(@"Apple", first_item); // Was it the menu we expected? |
| 78 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 79 } |
OLD | NEW |