summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.cc
blob: 34ec260b2f15ead4bedba87eec004603e3b4dbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright 2013 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <vector>

#include "ash/shell.h"
#include "base/command_line.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/base/ime/input_method.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_switches.h"

namespace {

const base::FilePath kWebuiTestDir =
    base::FilePath(FILE_PATH_LITERAL("webui"));

const base::FilePath kVirtualKeyboardTestDir =
    base::FilePath(FILE_PATH_LITERAL("chromeos/virtual_keyboard"));

const base::FilePath kMockController =
    base::FilePath(FILE_PATH_LITERAL("mock_controller.js"));

const base::FilePath kMockTimer =
    base::FilePath(FILE_PATH_LITERAL("mock_timer.js"));

const base::FilePath kBaseKeyboardTestFramework =
    base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js"));

}  // namespace

class VirtualKeyboardBrowserTest : public InProcessBrowserTest {
 public:

  // Ensure that the virtual keyboard is enabled.
  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    command_line->AppendSwitch(
        keyboard::switches::kEnableVirtualKeyboard);
  }

  // Injects javascript in |file| into the keyboard page and runs test methods.
  void RunTest(const base::FilePath& file) {
    ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard"));

    content::WebContents* web_contents =
        browser()->tab_strip_model()->GetActiveWebContents();
    ASSERT_TRUE(web_contents);

    // Inject testing scripts.
    InjectJavascript(kWebuiTestDir, kMockController);
    InjectJavascript(kWebuiTestDir, kMockTimer);
    InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework);
    InjectJavascript(kVirtualKeyboardTestDir, file);

    ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_));

    // Inject DOM-automation test harness and run tests.
    std::vector<int> resource_ids;
    EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids));
  }

  void showVirtualKeyboard() {
    aura::Window *window = ash::Shell::GetPrimaryRootWindow();
    ui::InputMethod* input_method = window->GetProperty(
        aura::client::kRootWindowInputMethodKey);
    ASSERT_TRUE(input_method);
    input_method->ShowImeIfNeeded();
  }

  content::RenderViewHost* GetKeyboardRenderViewHost() {
    showVirtualKeyboard();
    std::string kVirtualKeyboardURL =
        "chrome-extension://mppnpdlheglhdfmldimlhpnegondlapf/";
    scoped_ptr<content::RenderWidgetHostIterator> widgets(
        content::RenderWidgetHost::GetRenderWidgetHosts());
    while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
      if (widget->IsRenderView()) {
        content::RenderViewHost* view = content::RenderViewHost::From(widget);
        std::string url = view->GetSiteInstance()->GetSiteURL().spec();
        if (url == kVirtualKeyboardURL) {
          content::WebContents* wc =
              content::WebContents::FromRenderViewHost(view);
          // Waits for Polymer to load.
          content::WaitForLoadStop(wc);
          return view;
        }
      }
    }
    return NULL;
  }

 private:

  // Injects javascript into the keyboard page.  The test |file| is in
  // directory |dir| relative to the root testing directory.
  void InjectJavascript(const base::FilePath& dir,
                        const base::FilePath& file) {
    base::FilePath path = ui_test_utils::GetTestFilePath(dir, file);
    std::string library_content;
    ASSERT_TRUE(base::ReadFileToString(path, &library_content))
        << path.value();
    utf8_content_.append(library_content);
    utf8_content_.append(";\n");
  }

  std::string utf8_content_;
};

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, AttributesTest) {
  RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js")));
}

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) {
  RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")));
}

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) {
  RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js")));
}

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, HideKeyboardKeyTest) {
  RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")));
}

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, KeysetTransitionTest) {
  RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js")));
}

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) {
  content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
  ASSERT_TRUE(keyboard_rvh);
  bool loaded = false;
  std::string script = "!!chrome.virtualKeyboardPrivate";
  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
      keyboard_rvh,
      "window.domAutomationController.send(" + script + ");",
      &loaded));
  // Catches the regression in crbug.com/308653.
  ASSERT_TRUE(loaded);
}

IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, EndToEndTest) {
  // Get the virtual keyboard's render view host.
  content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
  ASSERT_TRUE(keyboard_rvh);

  // Get the test page's render view host.
  content::RenderViewHost* browser_rvh = browser()->tab_strip_model()->
      GetActiveWebContents()->GetRenderViewHost();
  ASSERT_TRUE(browser_rvh);

  // Set up the test page.
  GURL url = ui_test_utils::GetTestUrl(
      base::FilePath(),
      base::FilePath(FILE_PATH_LITERAL(
          "chromeos/virtual_keyboard/end_to_end_test.html")));
  ui_test_utils::NavigateToURL(browser(), url);

  // Press 'a' on keyboard.
  base::FilePath path = ui_test_utils::GetTestFilePath(
      kVirtualKeyboardTestDir,
      base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js")));
  std::string script;
  ASSERT_TRUE(base::ReadFileToString(path, &script));
  EXPECT_TRUE(content::ExecuteScript(keyboard_rvh, script));
  // Verify 'a' appeared on test page.
  bool success = false;
  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
      browser_rvh,
      "success ? verifyInput('a') : waitForInput('a');",
      &success));
  ASSERT_TRUE(success);
}

// TODO(kevers|rsadam|bshe):  Add UI tests for remaining virtual keyboard
// functionality.