| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/views/controls/webview/webview.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "content/public/browser/web_contents.h" | |
| 10 #include "content/public/test/mock_render_process_host.h" | |
| 11 #include "content/public/test/test_browser_context.h" | |
| 12 #include "content/public/test/test_browser_thread.h" | |
| 13 #include "content/public/test/test_renderer_host.h" | |
| 14 #include "content/public/test/web_contents_tester.h" | |
| 15 #include "ui/base/ime/text_input_focus_manager.h" | |
| 16 #include "ui/base/ui_base_switches.h" | |
| 17 #include "ui/gl/gl_surface.h" | |
| 18 #include "ui/views/test/test_views_delegate.h" | |
| 19 #include "ui/views/test/webview_test_helper.h" | |
| 20 #include "ui/views/test/widget_test.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 class WebViewTestViewsDelegate : public views::TestViewsDelegate { | |
| 25 public: | |
| 26 WebViewTestViewsDelegate() {} | |
| 27 | |
| 28 // Overriden from TestViewsDelegate. | |
| 29 content::WebContents* CreateWebContents( | |
| 30 content::BrowserContext* browser_context, | |
| 31 content::SiteInstance* site_instance) override { | |
| 32 return content::WebContentsTester::CreateTestWebContents(browser_context, | |
| 33 site_instance); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate); | |
| 38 }; | |
| 39 | |
| 40 class WebViewInteractiveUiTest : public views::test::WidgetTest { | |
| 41 public: | |
| 42 WebViewInteractiveUiTest() | |
| 43 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()) {} | |
| 44 | |
| 45 void SetUp() override { | |
| 46 gfx::GLSurface::InitializeOneOffForTests(); | |
| 47 set_views_delegate(make_scoped_ptr(new WebViewTestViewsDelegate)); | |
| 48 WidgetTest::SetUp(); | |
| 49 } | |
| 50 | |
| 51 protected: | |
| 52 content::BrowserContext* browser_context() { return &browser_context_; } | |
| 53 | |
| 54 private: | |
| 55 content::TestBrowserContext browser_context_; | |
| 56 views::WebViewTestHelper webview_test_helper_; | |
| 57 content::TestBrowserThread ui_thread_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(WebViewInteractiveUiTest); | |
| 60 }; | |
| 61 | |
| 62 TEST_F(WebViewInteractiveUiTest, TextInputClientIsUpToDate) { | |
| 63 // WebViewInteractiveUiTest.TextInputClientIsUpToDate needs | |
| 64 // kEnableTextInputFocusManager flag to be enabled. | |
| 65 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 66 cmd_line->AppendSwitch(switches::kEnableTextInputFocusManager); | |
| 67 | |
| 68 // Create a top level widget and a webview as its content. | |
| 69 views::Widget* widget = CreateTopLevelFramelessPlatformWidget(); | |
| 70 views::WebView* webview = new views::WebView(browser_context()); | |
| 71 widget->SetContentsView(webview); | |
| 72 widget->Show(); | |
| 73 webview->RequestFocus(); | |
| 74 | |
| 75 // Mac needs to spin a run loop to activate. Don't fake it, so that the Widget | |
| 76 // still gets didBecomeKey notifications. There is just one widget, so a | |
| 77 // single spin of the runloop should be enough (it didn't flake in local tests | |
| 78 // but a WidgetObserver might be needed). | |
| 79 #if defined(OS_MACOSX) | |
| 80 RunPendingMessages(); | |
| 81 #endif | |
| 82 EXPECT_TRUE(widget->IsActive()); | |
| 83 | |
| 84 ui::TextInputFocusManager* text_input_focus_manager = | |
| 85 ui::TextInputFocusManager::GetInstance(); | |
| 86 EXPECT_EQ(nullptr, webview->GetTextInputClient()); | |
| 87 EXPECT_EQ(text_input_focus_manager->GetFocusedTextInputClient(), | |
| 88 webview->GetTextInputClient()); | |
| 89 | |
| 90 // Case 1: Creates a new WebContents. | |
| 91 content::WebContents* web_contents1 = webview->GetWebContents(); | |
| 92 web_contents1->GetRenderViewHost()->GetProcess()->Init(); | |
| 93 content::RenderViewHostTester::For(web_contents1->GetRenderViewHost()) | |
| 94 ->CreateTestRenderView(base::string16(), MSG_ROUTING_NONE, | |
| 95 MSG_ROUTING_NONE, -1, false); | |
| 96 webview->RequestFocus(); | |
| 97 ui::TextInputClient* client1 = webview->GetTextInputClient(); | |
| 98 EXPECT_NE(nullptr, client1); | |
| 99 EXPECT_EQ(text_input_focus_manager->GetFocusedTextInputClient(), client1); | |
| 100 | |
| 101 // Case 2: Replaces a WebContents by SetWebContents(). | |
| 102 scoped_ptr<content::WebContents> web_contents2( | |
| 103 content::WebContentsTester::CreateTestWebContents(browser_context(), | |
| 104 nullptr)); | |
| 105 web_contents2->GetRenderViewHost()->GetProcess()->Init(); | |
| 106 content::RenderViewHostTester::For(web_contents2->GetRenderViewHost()) | |
| 107 ->CreateTestRenderView(base::string16(), MSG_ROUTING_NONE, | |
| 108 MSG_ROUTING_NONE, -1, false); | |
| 109 webview->SetWebContents(web_contents2.get()); | |
| 110 ui::TextInputClient* client2 = webview->GetTextInputClient(); | |
| 111 EXPECT_NE(nullptr, client2); | |
| 112 EXPECT_EQ(text_input_focus_manager->GetFocusedTextInputClient(), client2); | |
| 113 EXPECT_NE(client1, client2); | |
| 114 | |
| 115 widget->Close(); | |
| 116 RunPendingMessages(); | |
| 117 } | |
| 118 | |
| 119 } // namespace | |
| OLD | NEW |