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/test_browser_context.h"
11#include "content/public/test/test_browser_thread.h"
12#include "ui/base/ime/text_input_focus_manager.h"
13#include "ui/base/ui_base_switches.h"
14#include "ui/gl/gl_surface.h"
15#include "ui/views/test/webview_test_helper.h"
16#include "ui/views/test/widget_test.h"
17
18namespace {
19
20class WebViewInteractiveUiTest : public views::test::WidgetTest {
21 public:
22  WebViewInteractiveUiTest()
23      : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()) {}
24
25  virtual void SetUp() OVERRIDE {
26    gfx::GLSurface::InitializeOneOffForTests();
27    WidgetTest::SetUp();
28  }
29
30 protected:
31  content::BrowserContext* browser_context() { return &browser_context_; }
32
33 private:
34  content::TestBrowserContext browser_context_;
35  views::WebViewTestHelper webview_test_helper_;
36  content::TestBrowserThread ui_thread_;
37
38  DISALLOW_COPY_AND_ASSIGN(WebViewInteractiveUiTest);
39};
40
41TEST_F(WebViewInteractiveUiTest, TextInputClientIsUpToDate) {
42  // WebViewInteractiveUiTest.TextInputClientIsUpToDate needs
43  // kEnableTextInputFocusManager flag to be enabled.
44  base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
45  cmd_line->AppendSwitch(switches::kEnableTextInputFocusManager);
46
47  // Create a top level widget and a webview as its content.
48  views::Widget* widget = CreateTopLevelFramelessPlatformWidget();
49  views::WebView* webview = new views::WebView(browser_context());
50  widget->SetContentsView(webview);
51  widget->Show();
52  webview->RequestFocus();
53
54  ui::TextInputFocusManager* text_input_focus_manager =
55      ui::TextInputFocusManager::GetInstance();
56  const ui::TextInputClient* null_text_input_client = NULL;
57  EXPECT_EQ(null_text_input_client, webview->GetTextInputClient());
58  EXPECT_EQ(text_input_focus_manager->GetFocusedTextInputClient(),
59            webview->GetTextInputClient());
60
61  // Case 1: Creates a new WebContents.
62  webview->GetWebContents();
63  webview->RequestFocus();
64  ui::TextInputClient* client1 = webview->GetTextInputClient();
65  EXPECT_NE(null_text_input_client, client1);
66  EXPECT_EQ(text_input_focus_manager->GetFocusedTextInputClient(), client1);
67
68  // Case 2: Replaces a WebContents by SetWebContents().
69  content::WebContents::CreateParams params(browser_context());
70  scoped_ptr<content::WebContents> web_contents(
71      content::WebContents::Create(params));
72  webview->SetWebContents(web_contents.get());
73  ui::TextInputClient* client2 = webview->GetTextInputClient();
74  EXPECT_NE(null_text_input_client, client2);
75  EXPECT_EQ(text_input_focus_manager->GetFocusedTextInputClient(), client2);
76  EXPECT_NE(client1, client2);
77
78  widget->Close();
79  RunPendingMessages();
80}
81
82}  // namespace
83