Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / apps / guest_view / web_view_interactive_browsertest.cc
blob64dbd622dbbd8bce32fb505563f6c211db8a276f
1 // Copyright 2013 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.
5 #include "base/location.h"
6 #include "base/single_thread_task_runner.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/apps/app_browsertest_util.h"
12 #include "chrome/browser/chrome_content_browser_client.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
15 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "chrome/test/base/test_launcher_utils.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "components/guest_view/browser/guest_view_base.h"
20 #include "components/guest_view/browser/guest_view_manager.h"
21 #include "components/guest_view/browser/guest_view_manager_delegate.h"
22 #include "components/guest_view/browser/guest_view_manager_factory.h"
23 #include "components/guest_view/browser/test_guest_view_manager.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/render_widget_host_iterator.h"
28 #include "content/public/browser/render_widget_host_view.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "extensions/browser/api/extensions_api_client.h"
33 #include "extensions/browser/app_window/app_window.h"
34 #include "extensions/browser/app_window/app_window_registry.h"
35 #include "extensions/test/extension_test_message_listener.h"
36 #include "net/test/embedded_test_server/embedded_test_server.h"
37 #include "ui/base/ime/composition_text.h"
38 #include "ui/base/ime/text_input_client.h"
39 #include "ui/base/test/ui_controls.h"
40 #include "ui/events/keycodes/keyboard_codes.h"
42 using extensions::AppWindow;
43 using extensions::ExtensionsAPIClient;
44 using guest_view::GuestViewBase;
45 using guest_view::GuestViewManager;
46 using guest_view::TestGuestViewManager;
47 using guest_view::TestGuestViewManagerFactory;
49 class WebViewInteractiveTest
50 : public extensions::PlatformAppBrowserTest {
51 public:
52 WebViewInteractiveTest()
53 : guest_web_contents_(NULL),
54 embedder_web_contents_(NULL),
55 corner_(gfx::Point()),
56 mouse_click_result_(false),
57 first_click_(true) {
58 GuestViewManager::set_factory_for_testing(&factory_);
61 TestGuestViewManager* GetGuestViewManager() {
62 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>(
63 TestGuestViewManager::FromBrowserContext(browser()->profile()));
64 // TestGuestViewManager::WaitForSingleGuestCreated may and will get called
65 // before a guest is created.
66 if (!manager) {
67 manager = static_cast<TestGuestViewManager*>(
68 GuestViewManager::CreateWithDelegate(
69 browser()->profile(),
70 ExtensionsAPIClient::Get()->CreateGuestViewManagerDelegate(
71 browser()->profile())));
73 return manager;
76 void MoveMouseInsideWindowWithListener(gfx::Point point,
77 const std::string& message) {
78 ExtensionTestMessageListener move_listener(message, false);
79 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
80 gfx::Point(corner_.x() + point.x(), corner_.y() + point.y())));
81 ASSERT_TRUE(move_listener.WaitUntilSatisfied());
84 void SendMouseClickWithListener(ui_controls::MouseButton button,
85 const std::string& message) {
86 ExtensionTestMessageListener listener(message, false);
87 SendMouseClick(button);
88 ASSERT_TRUE(listener.WaitUntilSatisfied());
91 void SendMouseClick(ui_controls::MouseButton button) {
92 SendMouseEvent(button, ui_controls::DOWN);
93 SendMouseEvent(button, ui_controls::UP);
96 void MoveMouseInsideWindow(const gfx::Point& point) {
97 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
98 gfx::Point(corner_.x() + point.x(), corner_.y() + point.y())));
101 gfx::NativeWindow GetPlatformAppWindow() {
102 const extensions::AppWindowRegistry::AppWindowList& app_windows =
103 extensions::AppWindowRegistry::Get(browser()->profile())->app_windows();
104 return (*app_windows.begin())->GetNativeWindow();
107 void SendKeyPressToPlatformApp(ui::KeyboardCode key) {
108 ASSERT_EQ(1U, GetAppWindowCount());
109 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
110 GetPlatformAppWindow(), key, false, false, false, false));
113 void SendCopyKeyPressToPlatformApp() {
114 ASSERT_EQ(1U, GetAppWindowCount());
115 #if defined(OS_MACOSX)
116 // Send Cmd+C on MacOSX.
117 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
118 GetPlatformAppWindow(), ui::VKEY_C, false, false, false, true));
119 #else
120 // Send Ctrl+C on Windows and Linux/ChromeOS.
121 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
122 GetPlatformAppWindow(), ui::VKEY_C, true, false, false, false));
123 #endif
126 void SendStartOfLineKeyPressToPlatformApp() {
127 #if defined(OS_MACOSX)
128 // Send Cmd+Left on MacOSX.
129 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
130 GetPlatformAppWindow(), ui::VKEY_LEFT, false, false, false, true));
131 #else
132 // Send Ctrl+Left on Windows and Linux/ChromeOS.
133 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
134 GetPlatformAppWindow(), ui::VKEY_LEFT, true, false, false, false));
135 #endif
138 void SendBackShortcutToPlatformApp() {
139 #if defined(OS_MACOSX)
140 // Send Cmd+[ on MacOSX.
141 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
142 GetPlatformAppWindow(), ui::VKEY_OEM_4, false, false, false, true));
143 #else
144 // Send browser back key on Linux/Windows.
145 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
146 GetPlatformAppWindow(), ui::VKEY_BROWSER_BACK,
147 false, false, false, false));
148 #endif
151 void SendForwardShortcutToPlatformApp() {
152 #if defined(OS_MACOSX)
153 // Send Cmd+] on MacOSX.
154 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
155 GetPlatformAppWindow(), ui::VKEY_OEM_6, false, false, false, true));
156 #else
157 // Send browser back key on Linux/Windows.
158 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
159 GetPlatformAppWindow(), ui::VKEY_BROWSER_FORWARD,
160 false, false, false, false));
161 #endif
164 void SendMouseEvent(ui_controls::MouseButton button,
165 ui_controls::MouseButtonState state) {
166 if (first_click_) {
167 mouse_click_result_ = ui_test_utils::SendMouseEventsSync(button,
168 state);
169 first_click_ = false;
170 } else {
171 ASSERT_EQ(mouse_click_result_, ui_test_utils::SendMouseEventsSync(
172 button, state));
176 enum TestServer {
177 NEEDS_TEST_SERVER,
178 NO_TEST_SERVER
181 scoped_ptr<ExtensionTestMessageListener> RunAppHelper(
182 const std::string& test_name,
183 const std::string& app_location,
184 TestServer test_server,
185 content::WebContents** embedder_web_contents) {
186 // For serving guest pages.
187 if ((test_server == NEEDS_TEST_SERVER) && !StartEmbeddedTestServer()) {
188 LOG(ERROR) << "FAILED TO START TEST SERVER.";
189 return scoped_ptr<ExtensionTestMessageListener>();
192 LoadAndLaunchPlatformApp(app_location.c_str(), "Launched");
193 if (!ui_test_utils::ShowAndFocusNativeWindow(GetPlatformAppWindow())) {
194 LOG(ERROR) << "UNABLE TO FOCUS TEST WINDOW.";
195 return scoped_ptr<ExtensionTestMessageListener>();
198 // Flush any pending events to make sure we start with a clean slate.
199 content::RunAllPendingInMessageLoop();
201 *embedder_web_contents = GetFirstAppWindowWebContents();
203 scoped_ptr<ExtensionTestMessageListener> done_listener(
204 new ExtensionTestMessageListener("TEST_PASSED", false));
205 done_listener->set_failure_message("TEST_FAILED");
206 if (!content::ExecuteScript(
207 *embedder_web_contents,
208 base::StringPrintf("runTest('%s')", test_name.c_str()))) {
209 LOG(ERROR) << "UNABLE TO START TEST";
210 return scoped_ptr<ExtensionTestMessageListener>();
213 return done_listener.Pass();
216 void TestHelper(const std::string& test_name,
217 const std::string& app_location,
218 TestServer test_server) {
219 content::WebContents* embedder_web_contents = NULL;
220 scoped_ptr<ExtensionTestMessageListener> done_listener(
221 RunAppHelper(
222 test_name, app_location, test_server, &embedder_web_contents));
224 ASSERT_TRUE(done_listener);
225 ASSERT_TRUE(done_listener->WaitUntilSatisfied());
227 guest_web_contents_ = GetGuestViewManager()->WaitForSingleGuestCreated();
230 void SetupTest(const std::string& app_name,
231 const std::string& guest_url_spec) {
232 ASSERT_TRUE(StartEmbeddedTestServer());
233 GURL::Replacements replace_host;
234 replace_host.SetHostStr("localhost");
236 GURL guest_url = embedded_test_server()->GetURL(guest_url_spec);
237 guest_url = guest_url.ReplaceComponents(replace_host);
239 ui_test_utils::UrlLoadObserver guest_observer(
240 guest_url, content::NotificationService::AllSources());
242 LoadAndLaunchPlatformApp(app_name.c_str(), "connected");
244 guest_observer.Wait();
245 content::Source<content::NavigationController> source =
246 guest_observer.source();
247 EXPECT_TRUE(source->GetWebContents()->GetRenderProcessHost()->
248 IsForGuestsOnly());
250 guest_web_contents_ = source->GetWebContents();
251 embedder_web_contents_ =
252 GuestViewBase::FromWebContents(guest_web_contents_)->
253 embedder_web_contents();
255 gfx::Rect offset = embedder_web_contents_->GetContainerBounds();
256 corner_ = gfx::Point(offset.x(), offset.y());
258 const testing::TestInfo* const test_info =
259 testing::UnitTest::GetInstance()->current_test_info();
260 const char* prefix = "DragDropWithinWebView";
261 if (!strncmp(test_info->name(), prefix, strlen(prefix))) {
262 // In the drag drop test we add 20px padding to the page body because on
263 // windows if we get too close to the edge of the window the resize cursor
264 // appears and we start dragging the window edge.
265 corner_.Offset(20, 20);
269 content::WebContents* guest_web_contents() {
270 return guest_web_contents_;
273 content::WebContents* embedder_web_contents() {
274 return embedder_web_contents_;
277 gfx::Point corner() {
278 return corner_;
281 void SimulateRWHMouseClick(content::RenderWidgetHost* rwh,
282 blink::WebMouseEvent::Button button,
283 int x,
284 int y) {
285 blink::WebMouseEvent mouse_event;
286 mouse_event.button = button;
287 mouse_event.x = mouse_event.windowX = x;
288 mouse_event.y = mouse_event.windowY = y;
289 mouse_event.modifiers = 0;
290 // Needed for the WebViewTest.ContextMenuPositionAfterCSSTransforms
291 gfx::Rect rect = rwh->GetView()->GetViewBounds();
292 mouse_event.globalX = x + rect.x();
293 mouse_event.globalY = y + rect.y();
294 mouse_event.type = blink::WebInputEvent::MouseDown;
295 rwh->ForwardMouseEvent(mouse_event);
296 mouse_event.type = blink::WebInputEvent::MouseUp;
297 rwh->ForwardMouseEvent(mouse_event);
300 class PopupCreatedObserver {
301 public:
302 PopupCreatedObserver()
303 : initial_widget_count_(0),
304 last_render_widget_host_(NULL) {}
306 ~PopupCreatedObserver() {}
308 void Wait() {
309 if (CountWidgets() == initial_widget_count_ + 1) {
310 gfx::Rect popup_bounds =
311 last_render_widget_host_->GetView()->GetViewBounds();
312 if (!popup_bounds.size().IsEmpty()) {
313 if (message_loop_.get())
314 message_loop_->Quit();
315 return;
319 // If we haven't seen any new widget or we get 0 size widget, we need to
320 // schedule waiting.
321 ScheduleWait();
323 if (!message_loop_.get()) {
324 message_loop_ = new content::MessageLoopRunner;
325 message_loop_->Run();
329 void Init() { initial_widget_count_ = CountWidgets(); }
331 // Returns the last widget created.
332 content::RenderWidgetHost* last_render_widget_host() {
333 return last_render_widget_host_;
336 private:
337 void ScheduleWait() {
338 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
339 FROM_HERE,
340 base::Bind(&PopupCreatedObserver::Wait, base::Unretained(this)),
341 base::TimeDelta::FromMilliseconds(200));
344 size_t CountWidgets() {
345 scoped_ptr<content::RenderWidgetHostIterator> widgets(
346 content::RenderWidgetHost::GetRenderWidgetHosts());
347 size_t num_widgets = 0;
348 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
349 if (widget->IsRenderView())
350 continue;
351 ++num_widgets;
352 last_render_widget_host_ = widget;
354 return num_widgets;
357 size_t initial_widget_count_;
358 content::RenderWidgetHost* last_render_widget_host_;
359 scoped_refptr<content::MessageLoopRunner> message_loop_;
360 bool seen_new_widget_;
362 DISALLOW_COPY_AND_ASSIGN(PopupCreatedObserver);
365 void PopupTestHelper(const gfx::Point& padding) {
366 PopupCreatedObserver popup_observer;
367 popup_observer.Init();
368 // Press alt+DOWN to open popup.
369 bool alt = true;
370 content::SimulateKeyPress(
371 guest_web_contents(), ui::VKEY_DOWN, false, false, alt, false);
372 popup_observer.Wait();
374 content::RenderWidgetHost* popup_rwh =
375 popup_observer.last_render_widget_host();
376 gfx::Rect popup_bounds = popup_rwh->GetView()->GetViewBounds();
378 content::RenderViewHost* embedder_rvh =
379 GetFirstAppWindowWebContents()->GetRenderViewHost();
380 gfx::Rect embedder_bounds = embedder_rvh->GetView()->GetViewBounds();
381 gfx::Vector2d diff = popup_bounds.origin() - embedder_bounds.origin();
382 LOG(INFO) << "DIFF: x = " << diff.x() << ", y = " << diff.y();
384 const int left_spacing = 40 + padding.x(); // div.style.paddingLeft = 40px.
385 // div.style.paddingTop = 50px + (input box height = 26px).
386 const int top_spacing = 50 + 26 + padding.y();
388 // If the popup is placed within |threshold_px| of the expected position,
389 // then we consider the test as a pass.
390 const int threshold_px = 10;
392 EXPECT_LE(std::abs(diff.x() - left_spacing), threshold_px);
393 EXPECT_LE(std::abs(diff.y() - top_spacing), threshold_px);
395 // Close the popup.
396 content::SimulateKeyPress(
397 guest_web_contents(), ui::VKEY_ESCAPE, false, false, false, false);
400 void DragTestStep1() {
401 // Move mouse to start of text.
402 MoveMouseInsideWindow(gfx::Point(45, 8));
403 MoveMouseInsideWindow(gfx::Point(45, 9));
404 SendMouseEvent(ui_controls::LEFT, ui_controls::DOWN);
406 MoveMouseInsideWindow(gfx::Point(74, 12));
407 MoveMouseInsideWindow(gfx::Point(78, 12));
409 // Now wait a bit before moving mouse to initiate drag/drop.
410 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
411 FROM_HERE, base::Bind(&WebViewInteractiveTest::DragTestStep2,
412 base::Unretained(this)),
413 base::TimeDelta::FromMilliseconds(200));
416 void DragTestStep2() {
417 // Drag source over target.
418 MoveMouseInsideWindow(gfx::Point(76, 76));
420 // Create a second mouse over the source to trigger the drag over event.
421 MoveMouseInsideWindow(gfx::Point(76, 77));
423 // Release mouse to drop.
424 SendMouseEvent(ui_controls::LEFT, ui_controls::UP);
425 SendMouseClick(ui_controls::LEFT);
427 quit_closure_.Run();
429 // Note that following ExtensionTestMessageListener and ExecuteScript*
430 // call must be after we quit |quit_closure_|. Otherwise the class
431 // here won't be able to receive messages sent by chrome.test.sendMessage.
432 // This is because of the nature of drag and drop code (esp. the
433 // MessageLoop) in it.
435 // Now check if we got a drop and read the drop data.
436 embedder_web_contents_ = GetFirstAppWindowWebContents();
437 ExtensionTestMessageListener drop_listener("guest-got-drop", false);
438 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_,
439 "window.checkIfGuestGotDrop()"));
440 EXPECT_TRUE(drop_listener.WaitUntilSatisfied());
442 std::string last_drop_data;
443 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
444 embedder_web_contents_,
445 "window.domAutomationController.send(getLastDropData())",
446 &last_drop_data));
448 last_drop_data_ = last_drop_data;
451 void FullscreenTestHelper(const std::string& test_name,
452 const std::string& test_dir) {
453 TestHelper(test_name, test_dir, NO_TEST_SERVER);
454 content::WebContents* embedder_web_contents =
455 GetFirstAppWindowWebContents();
456 ASSERT_TRUE(embedder_web_contents);
457 ASSERT_TRUE(guest_web_contents());
458 // Click the guest to request fullscreen.
459 ExtensionTestMessageListener passed_listener(
460 "FULLSCREEN_STEP_PASSED", false);
461 passed_listener.set_failure_message("TEST_FAILED");
462 content::SimulateMouseClickAt(guest_web_contents(),
464 blink::WebMouseEvent::ButtonLeft,
465 gfx::Point(20, 20));
466 ASSERT_TRUE(passed_listener.WaitUntilSatisfied());
469 protected:
470 TestGuestViewManagerFactory factory_;
471 content::WebContents* guest_web_contents_;
472 content::WebContents* embedder_web_contents_;
473 gfx::Point corner_;
474 bool mouse_click_result_;
475 bool first_click_;
476 // Only used in drag/drop test.
477 base::Closure quit_closure_;
478 std::string last_drop_data_;
481 // ui_test_utils::SendMouseMoveSync doesn't seem to work on OS_MACOSX, and
482 // likely won't work on many other platforms as well, so for now this test
483 // is for Windows and Linux only. As of Sept 17th, 2013 this test is disabled
484 // on Windows due to flakines, see http://crbug.com/293445.
486 // Disabled on Linux Aura because pointer lock does not work on Linux Aura.
487 // crbug.com/341876
489 #if defined(OS_LINUX)
490 // flaky http://crbug.com/412086
491 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, DISABLED_PointerLock) {
492 SetupTest("web_view/pointer_lock",
493 "/extensions/platform_apps/web_view/pointer_lock/guest.html");
495 // Move the mouse over the Lock Pointer button.
496 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
497 gfx::Point(corner().x() + 75, corner().y() + 25)));
499 // Click the Lock Pointer button. The first two times the button is clicked
500 // the permission API will deny the request (intentional).
501 ExtensionTestMessageListener exception_listener("request exception", false);
502 SendMouseClickWithListener(ui_controls::LEFT, "lock error");
503 ASSERT_TRUE(exception_listener.WaitUntilSatisfied());
504 SendMouseClickWithListener(ui_controls::LEFT, "lock error");
506 // Click the Lock Pointer button, locking the mouse to lockTarget1.
507 SendMouseClickWithListener(ui_controls::LEFT, "locked");
509 // Attempt to move the mouse off of the lock target, and onto lockTarget2,
510 // (which would trigger a test failure).
511 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
512 gfx::Point(corner().x() + 74, corner().y() + 74)));
513 MoveMouseInsideWindowWithListener(gfx::Point(75, 75), "mouse-move");
515 #if defined(OS_WIN)
516 // When the mouse is unlocked on win aura, sending a test mouse click clicks
517 // where the mouse moved to while locked. I was unable to figure out why, and
518 // since the issue only occurs with the test mouse events, just fix it with
519 // a simple workaround - moving the mouse back to where it should be.
520 // TODO(mthiesse): Fix Win Aura simulated mouse events while mouse locked.
521 MoveMouseInsideWindowWithListener(gfx::Point(75, 25), "mouse-move");
522 #endif
524 ExtensionTestMessageListener unlocked_listener("unlocked", false);
525 // Send a key press to unlock the mouse.
526 SendKeyPressToPlatformApp(ui::VKEY_ESCAPE);
528 // Wait for page to receive (successful) mouse unlock response.
529 ASSERT_TRUE(unlocked_listener.WaitUntilSatisfied());
531 // After the second lock, guest.js sends a message to main.js to remove the
532 // webview object. main.js then removes the div containing the webview, which
533 // should unlock, and leave the mouse over the mousemove-capture-container
534 // div. We then move the mouse over that div to ensure the mouse was properly
535 // unlocked and that the div receieves the message.
536 ExtensionTestMessageListener move_captured_listener("move-captured", false);
537 move_captured_listener.set_failure_message("timeout");
539 // Mouse should already be over lock button (since we just unlocked), so send
540 // click to re-lock the mouse.
541 SendMouseClickWithListener(ui_controls::LEFT, "deleted");
543 // A mousemove event is triggered on the mousemove-capture-container element
544 // when we delete the webview container (since the mouse moves onto the
545 // element), but just in case, send an explicit mouse movement to be safe.
546 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
547 gfx::Point(corner().x() + 50, corner().y() + 10)));
549 // Wait for page to receive second (successful) mouselock response.
550 bool success = move_captured_listener.WaitUntilSatisfied();
551 if (!success) {
552 fprintf(stderr, "TIMEOUT - retrying\n");
553 // About 1 in 40 tests fail to detect mouse moves at this point (why?).
554 // Sending a right click seems to fix this (why?).
555 ExtensionTestMessageListener move_listener2("move-captured", false);
556 SendMouseClick(ui_controls::RIGHT);
557 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
558 gfx::Point(corner().x() + 51, corner().y() + 11)));
559 ASSERT_TRUE(move_listener2.WaitUntilSatisfied());
563 // flaky http://crbug.com/412086
564 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, DISABLED_PointerLockFocus) {
565 SetupTest("web_view/pointer_lock_focus",
566 "/extensions/platform_apps/web_view/pointer_lock_focus/guest.html");
568 // Move the mouse over the Lock Pointer button.
569 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
570 gfx::Point(corner().x() + 75, corner().y() + 25)));
572 // Click the Lock Pointer button, locking the mouse to lockTarget.
573 // This will also change focus to another element
574 SendMouseClickWithListener(ui_controls::LEFT, "locked");
576 // Try to unlock the mouse now that the focus is outside of the BrowserPlugin
577 ExtensionTestMessageListener unlocked_listener("unlocked", false);
578 // Send a key press to unlock the mouse.
579 SendKeyPressToPlatformApp(ui::VKEY_ESCAPE);
581 // Wait for page to receive (successful) mouse unlock response.
582 ASSERT_TRUE(unlocked_listener.WaitUntilSatisfied());
585 #endif // defined(OS_LINUX)
587 // Tests that if a <webview> is focused before navigation then the guest starts
588 // off focused.
589 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Focus_FocusBeforeNavigation) {
590 TestHelper("testFocusBeforeNavigation", "web_view/focus", NO_TEST_SERVER);
593 // Tests that setting focus on the <webview> sets focus on the guest.
594 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Focus_FocusEvent) {
595 TestHelper("testFocusEvent", "web_view/focus", NO_TEST_SERVER);
598 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Focus_FocusTracksEmbedder) {
599 content::WebContents* embedder_web_contents = NULL;
601 scoped_ptr<ExtensionTestMessageListener> done_listener(
602 RunAppHelper("testFocusTracksEmbedder", "web_view/focus", NO_TEST_SERVER,
603 &embedder_web_contents));
604 done_listener->WaitUntilSatisfied();
606 ExtensionTestMessageListener next_step_listener("TEST_STEP_PASSED", false);
607 next_step_listener.set_failure_message("TEST_STEP_FAILED");
608 EXPECT_TRUE(content::ExecuteScript(
609 embedder_web_contents,
610 "window.runCommand('testFocusTracksEmbedderRunNextStep');"));
612 // Blur the embedder.
613 embedder_web_contents->GetRenderViewHost()->Blur();
614 // Ensure that the guest is also blurred.
615 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
618 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Focus_AdvanceFocus) {
619 content::WebContents* embedder_web_contents = NULL;
622 scoped_ptr<ExtensionTestMessageListener> done_listener(
623 RunAppHelper("testAdvanceFocus", "web_view/focus", NO_TEST_SERVER,
624 &embedder_web_contents));
625 done_listener->WaitUntilSatisfied();
629 ExtensionTestMessageListener listener("button1-focused", false);
630 listener.set_failure_message("TEST_FAILED");
631 SimulateRWHMouseClick(embedder_web_contents->GetRenderViewHost(),
632 blink::WebMouseEvent::ButtonLeft, 200, 20);
633 content::SimulateKeyPress(embedder_web_contents, ui::VKEY_TAB,
634 false, false, false, false);
635 ASSERT_TRUE(listener.WaitUntilSatisfied());
639 // Wait for button1 to be focused again, this means we were asked to
640 // move the focus to the next focusable element.
641 ExtensionTestMessageListener listener("button1-advance-focus", false);
642 listener.set_failure_message("TEST_FAILED");
643 // TODO(fsamuel): A third Tab key press should not be necessary.
644 // The <webview> will take keyboard focus but it will not focus an initial
645 // element. The initial element is dependent upon tab direction which blink
646 // does not propagate to the plugin.
647 // See http://crbug.com/147644.
648 content::SimulateKeyPress(embedder_web_contents, ui::VKEY_TAB,
649 false, false, false, false);
650 content::SimulateKeyPress(embedder_web_contents, ui::VKEY_TAB,
651 false, false, false, false);
652 content::SimulateKeyPress(embedder_web_contents, ui::VKEY_TAB,
653 false, false, false, false);
654 ASSERT_TRUE(listener.WaitUntilSatisfied());
658 // Tests that blurring <webview> also blurs the guest.
659 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Focus_BlurEvent) {
660 TestHelper("testBlurEvent", "web_view/focus", NO_TEST_SERVER);
663 // Tests that guests receive edit commands and respond appropriately.
664 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, EditCommands) {
665 LoadAndLaunchPlatformApp("web_view/edit_commands", "connected");
667 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
668 GetPlatformAppWindow()));
670 // Flush any pending events to make sure we start with a clean slate.
671 content::RunAllPendingInMessageLoop();
673 ExtensionTestMessageListener copy_listener("copy", false);
674 SendCopyKeyPressToPlatformApp();
676 // Wait for the guest to receive a 'copy' edit command.
677 ASSERT_TRUE(copy_listener.WaitUntilSatisfied());
680 // Tests that guests receive edit commands and respond appropriately.
681 // http://crbug.com/417892
682 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, DISABLED_EditCommandsNoMenu) {
683 SetupTest("web_view/edit_commands_no_menu",
684 "/extensions/platform_apps/web_view/edit_commands_no_menu/"
685 "guest.html");
687 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
688 GetPlatformAppWindow()));
690 // Flush any pending events to make sure we start with a clean slate.
691 content::RunAllPendingInMessageLoop();
693 ExtensionTestMessageListener start_of_line_listener("StartOfLine", false);
694 SendStartOfLineKeyPressToPlatformApp();
695 // Wait for the guest to receive a 'copy' edit command.
696 ASSERT_TRUE(start_of_line_listener.WaitUntilSatisfied());
699 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
700 NewWindow_AttachAfterOpenerDestroyed) {
701 TestHelper("testNewWindowAttachAfterOpenerDestroyed",
702 "web_view/newwindow",
703 NEEDS_TEST_SERVER);
706 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
707 NewWindow_NewWindowNameTakesPrecedence) {
708 TestHelper("testNewWindowNameTakesPrecedence",
709 "web_view/newwindow",
710 NEEDS_TEST_SERVER);
713 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
714 NewWindow_WebViewNameTakesPrecedence) {
715 TestHelper("testNewWindowWebViewNameTakesPrecedence",
716 "web_view/newwindow",
717 NEEDS_TEST_SERVER);
720 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, NewWindow_NoName) {
721 TestHelper("testNewWindowNoName",
722 "web_view/newwindow",
723 NEEDS_TEST_SERVER);
726 // Flaky on win_chromium_rel_ng. https://crbug.com/504054
727 #if defined(OS_WIN)
728 #define MAYBE_NewWindow_Redirect DISABLED_NewWindow_Redirect
729 #else
730 #define MAYBE_NewWindow_Redirect NewWindow_Redirect
731 #endif
732 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, MAYBE_NewWindow_Redirect) {
733 TestHelper("testNewWindowRedirect",
734 "web_view/newwindow",
735 NEEDS_TEST_SERVER);
738 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, NewWindow_Close) {
739 TestHelper("testNewWindowClose",
740 "web_view/newwindow",
741 NEEDS_TEST_SERVER);
744 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, NewWindow_DeferredAttachment) {
745 TestHelper("testNewWindowDeferredAttachment",
746 "web_view/newwindow",
747 NEEDS_TEST_SERVER);
750 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, NewWindow_ExecuteScript) {
751 TestHelper("testNewWindowExecuteScript",
752 "web_view/newwindow",
753 NEEDS_TEST_SERVER);
756 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
757 NewWindow_DeclarativeWebRequest) {
758 TestHelper("testNewWindowDeclarativeWebRequest",
759 "web_view/newwindow",
760 NEEDS_TEST_SERVER);
763 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
764 NewWindow_DiscardAfterOpenerDestroyed) {
765 TestHelper("testNewWindowDiscardAfterOpenerDestroyed",
766 "web_view/newwindow",
767 NEEDS_TEST_SERVER);
770 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, NewWindow_WebRequest) {
771 TestHelper("testNewWindowWebRequest",
772 "web_view/newwindow",
773 NEEDS_TEST_SERVER);
776 // A custom elements bug needs to be addressed to enable this test:
777 // See http://crbug.com/282477 for more information.
778 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
779 DISABLED_NewWindow_WebRequestCloseWindow) {
780 TestHelper("testNewWindowWebRequestCloseWindow",
781 "web_view/newwindow",
782 NEEDS_TEST_SERVER);
785 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
786 NewWindow_WebRequestRemoveElement) {
787 TestHelper("testNewWindowWebRequestRemoveElement",
788 "web_view/newwindow",
789 NEEDS_TEST_SERVER);
792 // There is a problem of missing keyup events with the command key after
793 // the NSEvent is sent to NSApplication in ui/base/test/ui_controls_mac.mm .
794 // This test is disabled on only the Mac until the problem is resolved.
795 // See http://crbug.com/425859 for more information.
796 #if !defined(OS_MACOSX)
797 // Tests that Ctrl+Click/Cmd+Click on a link fires up the newwindow API.
798 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, NewWindow_OpenInNewTab) {
799 content::WebContents* embedder_web_contents = NULL;
801 ExtensionTestMessageListener loaded_listener("Loaded", false);
802 scoped_ptr<ExtensionTestMessageListener> done_listener(
803 RunAppHelper("testNewWindowOpenInNewTab",
804 "web_view/newwindow",
805 NEEDS_TEST_SERVER,
806 &embedder_web_contents));
808 loaded_listener.WaitUntilSatisfied();
809 #if defined(OS_MACOSX)
810 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
811 GetPlatformAppWindow(), ui::VKEY_RETURN,
812 false, false, false, true /* cmd */));
813 #else
814 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
815 GetPlatformAppWindow(), ui::VKEY_RETURN,
816 true /* ctrl */, false, false, false));
817 #endif
819 // Wait for the embedder to receive a 'newwindow' event.
820 ASSERT_TRUE(done_listener->WaitUntilSatisfied());
822 #endif
824 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
825 NewWindow_OpenerDestroyedWhileUnattached) {
826 TestHelper("testNewWindowOpenerDestroyedWhileUnattached",
827 "web_view/newwindow",
828 NEEDS_TEST_SERVER);
829 ASSERT_EQ(2u, GetGuestViewManager()->num_guests_created());
831 // We have two guests in this test, one is the intial one, the other
832 // is the newwindow one.
833 // Before the embedder goes away, both the guests should go away.
834 // This ensures that unattached guests are gone if opener is gone.
835 GetGuestViewManager()->WaitForAllGuestsDeleted();
838 // Tests whether <webview> context menu sees <webview> local coordinates
839 // in its RenderViewContextMenu params.
840 // Local coordinates are required for plugin actions to work properly.
841 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, ContextMenuParamCoordinates) {
842 TestHelper("testCoordinates", "web_view/context_menus/coordinates",
843 NO_TEST_SERVER);
844 ASSERT_TRUE(guest_web_contents());
846 ContextMenuWaiter menu_observer(content::NotificationService::AllSources());
847 SimulateRWHMouseClick(guest_web_contents()->GetRenderViewHost(),
848 blink::WebMouseEvent::ButtonRight, 10, 20);
849 // Wait until the context menu is opened and closed.
850 menu_observer.WaitForMenuOpenAndClose();
851 ASSERT_EQ(10, menu_observer.params().x);
852 ASSERT_EQ(20, menu_observer.params().y);
855 // Tests whether <webview> context menu sees <webview> local coordinates in its
856 // RenderViewContextMenu params, when it is subject to CSS transforms.
857 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
858 ContextMenuParamsAfterCSSTransforms) {
859 LoadAndLaunchPlatformApp("web_view/context_menus/coordinates_with_transforms",
860 "Launched");
862 if (!embedder_web_contents_)
863 embedder_web_contents_ = GetFirstAppWindowWebContents();
864 EXPECT_TRUE(embedder_web_contents());
866 if (!guest_web_contents_)
867 guest_web_contents_ = GetGuestViewManager()->WaitForSingleGuestCreated();
868 EXPECT_TRUE(guest_web_contents());
870 // We will send the input event to the embedder rather than the guest; which
871 // is more realistic. We need to do this to make sure that the MouseDown event
872 // is received forwarded by the BrowserPlugin to the RWHVG and eventually back
873 // to the guest. The RWHVG will in turn notify the ChromeWVGDelegate of the
874 // newly observed mouse down (potentially a context menu).
875 const std::string transforms[] = {"rotate(20deg)", "scale(1.5, 2.0)",
876 "translate(20px, 30px)", "NONE"};
877 for (size_t index = 0; index < 4; ++index) {
878 std::string command =
879 base::StringPrintf("setTransform('%s')", transforms[index].c_str());
880 ExtensionTestMessageListener transform_set_listener("TRANSFORM_SET", false);
881 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents(), command));
882 ASSERT_TRUE(transform_set_listener.WaitUntilSatisfied());
884 gfx::Rect embedder_view_bounds =
885 embedder_web_contents()->GetRenderWidgetHostView()->GetViewBounds();
886 gfx::Rect guest_view_bounds =
887 guest_web_contents()->GetRenderWidgetHostView()->GetViewBounds();
888 ContextMenuWaiter menu_observer(content::NotificationService::AllSources());
889 gfx::Point guest_window_point(150, 150);
890 gfx::Point embedder_window_point = guest_window_point;
891 embedder_window_point += guest_view_bounds.OffsetFromOrigin();
892 embedder_window_point -= embedder_view_bounds.OffsetFromOrigin();
893 SimulateRWHMouseClick(embedder_web_contents()->GetRenderViewHost(),
894 blink::WebMouseEvent::ButtonRight,
895 /* Using window coordinates for the embedder */
896 embedder_window_point.x(), embedder_window_point.y());
898 menu_observer.WaitForMenuOpenAndClose();
899 EXPECT_EQ(menu_observer.params().x, guest_window_point.x());
900 EXPECT_EQ(menu_observer.params().y, guest_window_point.y());
904 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, ExecuteCode) {
905 ASSERT_TRUE(RunPlatformAppTestWithArg(
906 "platform_apps/web_view/common", "execute_code")) << message_;
909 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, PopupPositioningBasic) {
910 TestHelper("testBasic", "web_view/popup_positioning", NO_TEST_SERVER);
911 ASSERT_TRUE(guest_web_contents());
912 PopupTestHelper(gfx::Point());
914 // TODO(lazyboy): Move the embedder window to a random location and
915 // make sure we keep rendering popups correct in webview.
918 // Flaky on ChromeOS: http://crbug.com/526886
919 #if defined(OS_CHROMEOS)
920 #define MAYBE_PopupPositioningMoved DISABLED_PopupPositioningMoved
921 #else
922 #define MAYBE_PopupPositioningMoved PopupPositioningMoved
923 #endif
924 // Tests that moving browser plugin (without resize/UpdateRects) correctly
925 // repositions popup.
926 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, MAYBE_PopupPositioningMoved) {
927 TestHelper("testMoved", "web_view/popup_positioning", NO_TEST_SERVER);
928 ASSERT_TRUE(guest_web_contents());
929 PopupTestHelper(gfx::Point(20, 0));
932 // Drag and drop inside a webview is currently only enabled for linux and mac,
933 // but the tests don't work on anything except chromeos for now. This is because
934 // of simulating mouse drag code's dependency on platforms.
935 #if defined(OS_CHROMEOS) && !defined(USE_OZONE)
936 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, DragDropWithinWebView) {
937 LoadAndLaunchPlatformApp("web_view/dnd_within_webview", "connected");
938 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(GetPlatformAppWindow()));
940 embedder_web_contents_ = GetFirstAppWindowWebContents();
941 gfx::Rect offset = embedder_web_contents_->GetContainerBounds();
942 corner_ = gfx::Point(offset.x(), offset.y());
944 // In the drag drop test we add 20px padding to the page body because on
945 // windows if we get too close to the edge of the window the resize cursor
946 // appears and we start dragging the window edge.
947 corner_.Offset(20, 20);
949 // Flush any pending events to make sure we start with a clean slate.
950 content::RunAllPendingInMessageLoop();
951 for (;;) {
952 base::RunLoop run_loop;
953 quit_closure_ = run_loop.QuitClosure();
954 base::MessageLoop::current()->PostTask(
955 FROM_HERE,
956 base::Bind(&WebViewInteractiveTest::DragTestStep1,
957 base::Unretained(this)));
958 run_loop.Run();
960 if (last_drop_data_ == "Drop me")
961 break;
963 LOG(INFO) << "Drag was cancelled in interactive_test, restarting drag";
965 // Reset state for next try.
966 ExtensionTestMessageListener reset_listener("resetStateReply", false);
967 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_,
968 "window.resetState()"));
969 ASSERT_TRUE(reset_listener.WaitUntilSatisfied());
971 ASSERT_EQ("Drop me", last_drop_data_);
973 #endif // (defined(OS_CHROMEOS))
975 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Navigation) {
976 TestHelper("testNavigation", "web_view/navigation", NO_TEST_SERVER);
979 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Navigation_BackForwardKeys) {
980 LoadAndLaunchPlatformApp("web_view/navigation", "Launched");
981 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
982 GetPlatformAppWindow()));
983 // Flush any pending events to make sure we start with a clean slate.
984 content::RunAllPendingInMessageLoop();
986 content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
987 ASSERT_TRUE(embedder_web_contents);
989 ExtensionTestMessageListener done_listener(
990 "TEST_PASSED", false);
991 done_listener.set_failure_message("TEST_FAILED");
992 ExtensionTestMessageListener ready_back_key_listener(
993 "ReadyForBackKey", false);
994 ExtensionTestMessageListener ready_forward_key_listener(
995 "ReadyForForwardKey", false);
997 EXPECT_TRUE(content::ExecuteScript(
998 embedder_web_contents,
999 "runTest('testBackForwardKeys')"));
1001 ASSERT_TRUE(ready_back_key_listener.WaitUntilSatisfied());
1002 SendBackShortcutToPlatformApp();
1004 ASSERT_TRUE(ready_forward_key_listener.WaitUntilSatisfied());
1005 SendForwardShortcutToPlatformApp();
1007 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
1010 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
1011 PointerLock_PointerLockLostWithFocus) {
1012 TestHelper("testPointerLockLostWithFocus",
1013 "web_view/pointerlock",
1014 NO_TEST_SERVER);
1017 // Disable this on mac, throws an assertion failure on teardown which
1018 // will result in flakiness:
1020 // "not is fullscreen state"
1021 // "*** Assertion failure in -[_NSWindowFullScreenTransition
1022 // transitionedWindowFrame],"
1023 // See similar bug: http://crbug.com/169820.
1025 // In addition to the above, these tests are flaky on many platforms:
1026 // http://crbug.com/468660
1027 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
1028 DISABLED_FullscreenAllow_EmbedderHasPermission) {
1029 FullscreenTestHelper("testFullscreenAllow",
1030 "web_view/fullscreen/embedder_has_permission");
1033 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
1034 DISABLED_FullscreenDeny_EmbedderHasPermission) {
1035 FullscreenTestHelper("testFullscreenDeny",
1036 "web_view/fullscreen/embedder_has_permission");
1039 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
1040 DISABLED_FullscreenAllow_EmbedderHasNoPermission) {
1041 FullscreenTestHelper("testFullscreenAllow",
1042 "web_view/fullscreen/embedder_has_no_permission");
1045 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest,
1046 DISABLED_FullscreenDeny_EmbedderHasNoPermission) {
1047 FullscreenTestHelper("testFullscreenDeny",
1048 "web_view/fullscreen/embedder_has_no_permission");
1051 // This test exercies the following scenario:
1052 // 1. An <input> in guest has focus.
1053 // 2. User takes focus to embedder by clicking e.g. an <input> in embedder.
1054 // 3. User brings back the focus directly to the <input> in #1.
1056 // Now we need to make sure TextInputTypeChanged fires properly for the guest's
1057 // view upon step #3. We simply read the input type's state after #3 to
1058 // make sure it's not TEXT_INPUT_TYPE_NONE.
1059 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, Focus_FocusRestored) {
1060 TestHelper("testFocusRestored", "web_view/focus", NO_TEST_SERVER);
1061 content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
1062 ASSERT_TRUE(embedder_web_contents);
1063 ASSERT_TRUE(guest_web_contents());
1065 // 1) We click on the guest so that we get a focus event.
1066 ExtensionTestMessageListener next_step_listener("TEST_STEP_PASSED", false);
1067 next_step_listener.set_failure_message("TEST_STEP_FAILED");
1069 content::SimulateMouseClickAt(guest_web_contents(),
1071 blink::WebMouseEvent::ButtonLeft,
1072 gfx::Point(10, 10));
1073 EXPECT_TRUE(content::ExecuteScript(
1074 embedder_web_contents,
1075 "window.runCommand('testFocusRestoredRunNextStep', 1);"));
1077 // Wait for the next step to complete.
1078 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
1080 // 2) We click on the embedder so the guest's focus goes away and it observes
1081 // a blur event.
1082 next_step_listener.Reset();
1084 content::SimulateMouseClickAt(embedder_web_contents,
1086 blink::WebMouseEvent::ButtonLeft,
1087 gfx::Point(200, 20));
1088 EXPECT_TRUE(content::ExecuteScript(
1089 embedder_web_contents,
1090 "window.runCommand('testFocusRestoredRunNextStep', 2);"));
1092 // Wait for the next step to complete.
1093 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
1095 // 3) We click on the guest again to bring back focus directly to the previous
1096 // input element, then we ensure text_input_type is properly set.
1097 next_step_listener.Reset();
1099 content::SimulateMouseClickAt(guest_web_contents(),
1101 blink::WebMouseEvent::ButtonLeft,
1102 gfx::Point(10, 10));
1103 EXPECT_TRUE(content::ExecuteScript(
1104 embedder_web_contents,
1105 "window.runCommand('testFocusRestoredRunNextStep', 3)"));
1107 // Wait for the next step to complete.
1108 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
1110 // |text_input_client| is not available for mac and android.
1111 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
1112 ui::TextInputClient* text_input_client =
1113 embedder_web_contents->GetRenderViewHost()->GetView()
1114 ->GetTextInputClient();
1115 ASSERT_TRUE(text_input_client);
1116 ASSERT_TRUE(text_input_client->GetTextInputType() !=
1117 ui::TEXT_INPUT_TYPE_NONE);
1118 #endif
1121 // ui::TextInputClient is NULL for mac and android.
1122 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
1123 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, DISABLED_Focus_InputMethod) {
1124 content::WebContents* embedder_web_contents = NULL;
1125 scoped_ptr<ExtensionTestMessageListener> done_listener(
1126 RunAppHelper("testInputMethod", "web_view/focus", NO_TEST_SERVER,
1127 &embedder_web_contents));
1128 ASSERT_TRUE(done_listener->WaitUntilSatisfied());
1130 ui::TextInputClient* text_input_client =
1131 embedder_web_contents->GetRenderViewHost()->GetView()
1132 ->GetTextInputClient();
1133 ASSERT_TRUE(text_input_client);
1135 ExtensionTestMessageListener next_step_listener("TEST_STEP_PASSED", false);
1136 next_step_listener.set_failure_message("TEST_STEP_FAILED");
1138 // An input element inside the <webview> gets focus and is given some
1139 // user input via IME.
1141 ui::CompositionText composition;
1142 composition.text = base::UTF8ToUTF16("InputTest123");
1143 text_input_client->SetCompositionText(composition);
1144 EXPECT_TRUE(content::ExecuteScript(
1145 embedder_web_contents,
1146 "window.runCommand('testInputMethodRunNextStep', 1);"));
1148 // Wait for the next step to complete.
1149 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
1152 // A composition is committed via IME.
1154 next_step_listener.Reset();
1156 ui::CompositionText composition;
1157 composition.text = base::UTF8ToUTF16("InputTest456");
1158 text_input_client->SetCompositionText(composition);
1159 text_input_client->ConfirmCompositionText();
1160 EXPECT_TRUE(content::ExecuteScript(
1161 embedder_web_contents,
1162 "window.runCommand('testInputMethodRunNextStep', 2);"));
1164 // Wait for the next step to complete.
1165 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
1168 // TODO(lazyboy): http://crbug.com/457007, Add a step or a separate test to
1169 // check the following, currently it turns this test to be flaky:
1170 // If we move the focus from the first <input> to the second one after we
1171 // have some composition text set but *not* committed (by calling
1172 // text_input_client->SetCompositionText()), then it would cause IME cancel
1173 // and the onging composition is committed in the first <input> in the
1174 // <webview>, not the second one.
1176 // Tests ExtendSelectionAndDelete message works in <webview>.
1178 next_step_listener.Reset();
1180 // At this point we have set focus on first <input> in the <webview>,
1181 // and the value it contains is 'InputTest456' with caret set after 'T'.
1182 // Now we delete 'Test' in 'InputTest456', as the caret is after 'T':
1183 // delete before 1 character ('T') and after 3 characters ('est').
1184 text_input_client->ExtendSelectionAndDelete(1, 3);
1185 EXPECT_TRUE(content::ExecuteScript(
1186 embedder_web_contents,
1187 "window.runCommand('testInputMethodRunNextStep', 3);"));
1189 // Wait for the next step to complete.
1190 ASSERT_TRUE(next_step_listener.WaitUntilSatisfied());
1193 #endif
1195 #if defined(OS_MACOSX)
1196 IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, TextSelection) {
1197 SetupTest("web_view/text_selection",
1198 "/extensions/platform_apps/web_view/text_selection/guest.html");
1199 ASSERT_TRUE(guest_web_contents());
1200 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
1201 GetPlatformAppWindow()));
1203 // Wait until guest sees a context menu, select an arbitrary item (copy).
1204 ExtensionTestMessageListener ctx_listener("MSG_CONTEXTMENU", false);
1205 ContextMenuNotificationObserver menu_observer(IDC_CONTENT_CONTEXT_COPY);
1206 SimulateRWHMouseClick(guest_web_contents()->GetRenderViewHost(),
1207 blink::WebMouseEvent::ButtonRight, 20, 20);
1208 ASSERT_TRUE(ctx_listener.WaitUntilSatisfied());
1210 // Now verify that the selection text propagates properly to RWHV.
1211 content::RenderWidgetHostView* guest_rwhv =
1212 guest_web_contents()->GetRenderWidgetHostView();
1213 ASSERT_TRUE(guest_rwhv);
1214 std::string selected_text = base::UTF16ToUTF8(guest_rwhv->GetSelectedText());
1215 ASSERT_TRUE(selected_text.size() >= 10u);
1216 ASSERT_EQ("AAAAAAAAAA", selected_text.substr(0, 10));
1218 #endif