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

Side by Side Diff: chrome/browser/apps/guest_view/web_view_browsertest.cc

Issue 1180503002: Force a GuestView's embedder to be focused on TouchStart. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor utility code to browser_test_utils. Created 5 years, 6 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 <queue>
6
5 #include "base/location.h" 7 #include "base/location.h"
6 #include "base/path_service.h" 8 #include "base/path_service.h"
7 #include "base/process/process.h" 9 #include "base/process/process.h"
8 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
9 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
12 #include "chrome/app/chrome_command_ids.h" 14 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/apps/app_browsertest_util.h" 15 #include "chrome/browser/apps/app_browsertest_util.h"
14 #include "chrome/browser/chrome_content_browser_client.h" 16 #include "chrome/browser/chrome_content_browser_client.h"
(...skipping 30 matching lines...) Expand all
45 #include "extensions/browser/app_window/native_app_window.h" 47 #include "extensions/browser/app_window/native_app_window.h"
46 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h " 48 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h "
47 #include "extensions/browser/guest_view/web_view/web_view_guest.h" 49 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
48 #include "extensions/common/extension.h" 50 #include "extensions/common/extension.h"
49 #include "extensions/common/extensions_client.h" 51 #include "extensions/common/extensions_client.h"
50 #include "extensions/test/extension_test_message_listener.h" 52 #include "extensions/test/extension_test_message_listener.h"
51 #include "media/base/media_switches.h" 53 #include "media/base/media_switches.h"
52 #include "net/test/embedded_test_server/embedded_test_server.h" 54 #include "net/test/embedded_test_server/embedded_test_server.h"
53 #include "net/test/embedded_test_server/http_request.h" 55 #include "net/test/embedded_test_server/http_request.h"
54 #include "net/test/embedded_test_server/http_response.h" 56 #include "net/test/embedded_test_server/http_response.h"
57 #include "ui/aura/window.h"
58 #include "ui/compositor/compositor.h"
59 #include "ui/compositor/compositor_observer.h"
60 #include "ui/events/event_switches.h"
55 #include "ui/gfx/switches.h" 61 #include "ui/gfx/switches.h"
56 #include "ui/gl/gl_switches.h" 62 #include "ui/gl/gl_switches.h"
63 #include "ui/views/view.h"
64 #include "ui/views/widget/widget.h"
57 65
58 #if defined(ENABLE_PLUGINS) 66 #if defined(ENABLE_PLUGINS)
59 #include "content/public/browser/plugin_service.h" 67 #include "content/public/browser/plugin_service.h"
60 #include "content/public/common/webplugininfo.h" 68 #include "content/public/common/webplugininfo.h"
61 #include "content/public/test/ppapi_test_utils.h" 69 #include "content/public/test/ppapi_test_utils.h"
62 #endif 70 #endif
63 71
64 #if defined(OS_CHROMEOS) 72 #if defined(OS_CHROMEOS)
65 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" 73 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
66 #include "chrome/browser/chromeos/accessibility/speech_monitor.h" 74 #include "chrome/browser/chromeos/accessibility/speech_monitor.h"
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 const char* title) { 207 const char* title) {
200 base::string16 expected_title(base::ASCIIToUTF16(title)); 208 base::string16 expected_title(base::ASCIIToUTF16(title));
201 base::string16 error_title(base::ASCIIToUTF16("error")); 209 base::string16 error_title(base::ASCIIToUTF16("error"));
202 210
203 content::TitleWatcher title_watcher(web_contents, expected_title); 211 content::TitleWatcher title_watcher(web_contents, expected_title);
204 title_watcher.AlsoWaitForTitle(error_title); 212 title_watcher.AlsoWaitForTitle(error_title);
205 EXPECT_TRUE(content::ExecuteScript(web_contents, script)); 213 EXPECT_TRUE(content::ExecuteScript(web_contents, script));
206 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); 214 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
207 } 215 }
208 216
217 #if defined (USE_AURA)
218 void GiveItSomeTime(int ms) {
219 base::RunLoop run_loop;
220 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
221 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromMilliseconds(ms));
222 run_loop.Run();
223 }
224
225 views::View* FindWebView(views::View* v) {
nasko 2015/06/22 08:40:52 nit: Using just one letter for variable names (oth
wjmaclean 2015/06/22 13:39:43 Done.
226 std::queue<views::View*> queue;
227 queue.push(v);
228 while (!queue.empty()) {
229 views::View* current = queue.front();
230 queue.pop();
231 if (std::string(current->GetClassName()).find("WebView") !=
232 std::string::npos) {
233 return current;
234 }
235
236 for (int i = 0; i < current->child_count(); ++i)
237 queue.push(current->child_at(i));
238 }
239 return nullptr;
240 }
241 #endif
242
209 } // namespace 243 } // namespace
210 244
211 // This class intercepts media access request from the embedder. The request 245 // This class intercepts media access request from the embedder. The request
212 // should be triggered only if the embedder API (from tests) allows the request 246 // should be triggered only if the embedder API (from tests) allows the request
213 // in Javascript. 247 // in Javascript.
214 // We do not issue the actual media request; the fact that the request reached 248 // We do not issue the actual media request; the fact that the request reached
215 // embedder's WebContents is good enough for our tests. This is also to make 249 // embedder's WebContents is good enough for our tests. This is also to make
216 // the test run successfully on trybots. 250 // the test run successfully on trybots.
217 class MockWebContentsDelegate : public content::WebContentsDelegate { 251 class MockWebContentsDelegate : public content::WebContentsDelegate {
218 public: 252 public:
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 browser()->profile())))); 686 browser()->profile()))));
653 } 687 }
654 return manager; 688 return manager;
655 } 689 }
656 690
657 WebViewTest() : guest_web_contents_(NULL), 691 WebViewTest() : guest_web_contents_(NULL),
658 embedder_web_contents_(NULL) { 692 embedder_web_contents_(NULL) {
659 GuestViewManager::set_factory_for_testing(&factory_); 693 GuestViewManager::set_factory_for_testing(&factory_);
660 } 694 }
661 695
696 protected:
697 scoped_refptr<content::FrameWatcher> frame_watcher_;
698
662 private: 699 private:
663 bool UsesFakeSpeech() { 700 bool UsesFakeSpeech() {
664 const testing::TestInfo* const test_info = 701 const testing::TestInfo* const test_info =
665 testing::UnitTest::GetInstance()->current_test_info(); 702 testing::UnitTest::GetInstance()->current_test_info();
666 703
667 // SpeechRecognition test specific SetUp. 704 // SpeechRecognition test specific SetUp.
668 return !strcmp(test_info->name(), 705 return !strcmp(test_info->name(),
669 "SpeechRecognitionAPI_HasPermissionAllow"); 706 "SpeechRecognitionAPI_HasPermissionAllow");
670 } 707 }
671 708
(...skipping 1693 matching lines...) Expand 10 before | Expand all | Expand 10 after
2365 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. 2402 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
2366 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/post_message/basic")) 2403 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/post_message/basic"))
2367 << message_; 2404 << message_;
2368 } 2405 }
2369 2406
2370 // Tests that webviews do get garbage collected. 2407 // Tests that webviews do get garbage collected.
2371 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestGarbageCollect) { 2408 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestGarbageCollect) {
2372 TestHelper("testGarbageCollect", "web_view/shim", NO_TEST_SERVER); 2409 TestHelper("testGarbageCollect", "web_view/shim", NO_TEST_SERVER);
2373 GetGuestViewManager()->WaitForSingleViewGarbageCollected(); 2410 GetGuestViewManager()->WaitForSingleViewGarbageCollected();
2374 } 2411 }
2412
2413 #if defined(USE_AURA)
2414 class WebViewFocusTest : public WebViewTest {
2415 public:
2416 ~WebViewFocusTest() override {}
2417
2418 void SetUpCommandLine(base::CommandLine* command_line) override {
2419 WebViewTest::SetUpCommandLine(command_line);
2420
2421 command_line->AppendSwitchASCII(switches::kTouchEvents,
2422 switches::kTouchEventsEnabled);
2423 }
2424
2425 void ForceCompositorFrame() {
2426 if (!frame_watcher_) {
2427 frame_watcher_ = new content::FrameWatcher();
2428 frame_watcher_->AttachTo(GetEmbedderWebContents());
2429 }
2430
2431 while (!RequestFrame(GetEmbedderWebContents()))
2432 GiveItSomeTime(10 /* milliseconds */);
2433 frame_watcher_->WaitFrames(1);
2434 }
2435
2436 private:
2437 scoped_refptr<content::FrameWatcher> frame_watcher_;
2438 };
2439
2440 // The following test verifies that a views::WebView hosting an embedder
2441 // gains focus on touchstart.
2442 IN_PROC_BROWSER_TEST_F(WebViewFocusTest, TouchFocusesEmbedder) {
2443 LoadAppWithGuest("web_view/accept_touch_events");
2444
2445 content::WebContents* web_contents = GetEmbedderWebContents();
2446 content::RenderViewHost* embedder_rvh = web_contents->GetRenderViewHost();
2447
2448 bool embedder_has_touch_handler =
2449 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh);
2450 EXPECT_FALSE(embedder_has_touch_handler);
2451
2452 SendMessageToGuestAndWait("install-touch-handler", "installed-touch-handler");
2453
2454 // Note that we need to wait for the installed/registered touch handler to
2455 // appear in browser process before querying |embedder_rvh|.
2456 // In practice, since we do a roundrtip from browser process to guest and
2457 // back, this is sufficient.
2458 embedder_has_touch_handler =
2459 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh);
2460 EXPECT_TRUE(embedder_has_touch_handler);
2461
2462 extensions::AppWindow* app_window = GetFirstAppWindowForBrowser(browser());
2463 aura::Window* window = app_window->GetNativeWindow();
2464 EXPECT_TRUE(app_window);
2465 EXPECT_TRUE(window);
2466 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
2467 EXPECT_TRUE(widget->GetRootView());
2468 // We only expect a single views::webview in the view hierarchy.
2469 views::View* aura_webview = FindWebView(widget->GetRootView());
2470 ASSERT_TRUE(aura_webview);
2471 gfx::Rect bounds(aura_webview->bounds());
2472 EXPECT_TRUE(aura_webview->IsFocusable());
2473
2474 views::View* other_focusable_view = new views::View();
2475 other_focusable_view->SetBounds(bounds.x() + bounds.width(), 0, 100, 100);
2476 other_focusable_view->SetFocusable(true);
2477 aura_webview->parent()->AddChildView(other_focusable_view);
2478 other_focusable_view->SetPosition(gfx::Point(bounds.x() + bounds.width(), 0));
2479
2480 // Sync changes to compositor.
2481 ForceCompositorFrame();
2482
2483 EXPECT_TRUE(aura_webview->HasFocus());
2484 other_focusable_view->RequestFocus();
2485 EXPECT_TRUE(other_focusable_view->HasFocus());
2486 EXPECT_FALSE(aura_webview->HasFocus());
2487
2488 // Generate and send synthetic touch event.
2489 // TODO(wjmaclean): This is fragile ... if anyone alters the location/size
2490 // of the webview in accept_touch_events then this may miss its target.
2491 content::SimulateTouchPressAt(GetEmbedderWebContents(), gfx::Point(10, 10));
2492
2493 // Wait for the TouchStart to propagate and restore focus. Test times out
2494 // on failure.
2495 while (!aura_webview->HasFocus())
2496 base::MessageLoop::current()->RunUntilIdle();
2497 GiveItSomeTime(10 /* milliseconds */);
2498 }
2499 #endif
OLDNEW
« no previous file with comments | « no previous file | content/browser/frame_host/render_widget_host_view_guest.cc » ('j') | content/public/test/browser_test_utils.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698