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.
5 #include "ui/views/controls/webview/webview.h"
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/test_browser_context.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "content/public/test/test_renderer_host.h"
13 #include "content/public/test/web_contents_tester.h"
14 #include "ui/base/ime/text_input_focus_manager.h"
15 #include "ui/base/ui_base_switches.h"
16 #include "ui/gl/gl_surface.h"
17 #include "ui/views/test/test_views_delegate.h"
18 #include "ui/views/test/webview_test_helper.h"
19 #include "ui/views/test/widget_test.h"
23 class WebViewTestViewsDelegate
: public views::TestViewsDelegate
{
25 WebViewTestViewsDelegate() {}
27 // Overriden from TestViewsDelegate.
28 content::WebContents
* CreateWebContents(
29 content::BrowserContext
* browser_context
,
30 content::SiteInstance
* site_instance
) override
{
31 return content::WebContentsTester::CreateTestWebContents(browser_context
,
36 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate
);
39 class WebViewInteractiveUiTest
: public views::test::WidgetTest
{
41 WebViewInteractiveUiTest()
42 : ui_thread_(content::BrowserThread::UI
, base::MessageLoop::current()) {}
44 void SetUp() override
{
45 gfx::GLSurface::InitializeOneOffForTests();
46 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down.
47 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate
);
52 content::BrowserContext
* browser_context() { return &browser_context_
; }
55 content::TestBrowserContext browser_context_
;
56 views::WebViewTestHelper webview_test_helper_
;
57 content::TestBrowserThread ui_thread_
;
59 DISALLOW_COPY_AND_ASSIGN(WebViewInteractiveUiTest
);
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
);
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
);
73 webview
->RequestFocus();
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)
82 EXPECT_TRUE(widget
->IsActive());
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());
90 // Case 1: Creates a new WebContents.
91 content::WebContents
* web_contents1
= webview
->GetWebContents();
92 content::RenderViewHostTester::For(web_contents1
->GetRenderViewHost())
93 ->CreateRenderView(base::string16(), MSG_ROUTING_NONE
, MSG_ROUTING_NONE
,
95 webview
->RequestFocus();
96 ui::TextInputClient
* client1
= webview
->GetTextInputClient();
97 EXPECT_NE(nullptr, client1
);
98 EXPECT_EQ(text_input_focus_manager
->GetFocusedTextInputClient(), client1
);
100 // Case 2: Replaces a WebContents by SetWebContents().
101 scoped_ptr
<content::WebContents
> web_contents2(
102 content::WebContentsTester::CreateTestWebContents(browser_context(),
104 content::RenderViewHostTester::For(web_contents2
->GetRenderViewHost())
105 ->CreateRenderView(base::string16(), MSG_ROUTING_NONE
, MSG_ROUTING_NONE
,
107 webview
->SetWebContents(web_contents2
.get());
108 ui::TextInputClient
* client2
= webview
->GetTextInputClient();
109 EXPECT_NE(nullptr, client2
);
110 EXPECT_EQ(text_input_focus_manager
->GetFocusedTextInputClient(), client2
);
111 EXPECT_NE(client1
, client2
);
114 RunPendingMessages();