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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <wrl/event.h>
#include <memory>
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/test/task_environment.h"
#include "base/win/windows_version.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/virtual_keyboard_controller_observer.h"
#include "ui/base/ime/win/on_screen_keyboard_display_manager_input_pane.h"
#include "ui/base/ime/win/on_screen_keyboard_display_manager_tab_tip.h"
namespace ui {
class MockVirtualKeyboardControllerObserver
: public VirtualKeyboardControllerObserver {
public:
MockVirtualKeyboardControllerObserver() = default;
MockVirtualKeyboardControllerObserver(
const MockVirtualKeyboardControllerObserver&) = delete;
MockVirtualKeyboardControllerObserver& operator=(
const MockVirtualKeyboardControllerObserver&) = delete;
~MockVirtualKeyboardControllerObserver() override = default;
MOCK_METHOD1(OnKeyboardVisible, void(const gfx::Rect&));
MOCK_METHOD0(OnKeyboardHidden, void());
};
class MockInputPane
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::UI::ViewManagement::IInputPane2,
ABI::Windows::UI::ViewManagement::IInputPane> {
public:
using InputPaneEventHandler = ABI::Windows::Foundation::ITypedEventHandler<
ABI::Windows::UI::ViewManagement::InputPane*,
ABI::Windows::UI::ViewManagement::InputPaneVisibilityEventArgs*>;
MockInputPane() = default;
MockInputPane(const MockInputPane&) = delete;
MockInputPane& operator=(const MockInputPane&) = delete;
IFACEMETHODIMP TryShow(boolean*) override {
if (showing_)
return S_OK;
showing_ = true;
EXPECT_NE(nullptr, show_handler_.Get());
show_handler_->Invoke(this, nullptr);
return S_OK;
}
IFACEMETHODIMP TryHide(boolean*) override {
if (!showing_)
return S_OK;
showing_ = false;
EXPECT_NE(nullptr, hide_handler_.Get());
hide_handler_->Invoke(this, nullptr);
return S_OK;
}
IFACEMETHODIMP
add_Showing(InputPaneEventHandler* handler,
EventRegistrationToken* token) override {
EXPECT_EQ(nullptr, show_handler_.Get());
show_handler_ = handler;
return S_OK;
}
IFACEMETHODIMP
remove_Showing(EventRegistrationToken token) override {
EXPECT_NE(nullptr, show_handler_.Get());
show_handler_.Reset();
return S_OK;
}
IFACEMETHODIMP add_Hiding(InputPaneEventHandler* handler,
EventRegistrationToken* token) override {
EXPECT_EQ(nullptr, hide_handler_.Get());
hide_handler_ = handler;
return S_OK;
}
IFACEMETHODIMP
remove_Hiding(EventRegistrationToken token) override {
EXPECT_NE(nullptr, hide_handler_.Get());
hide_handler_.Reset();
return S_OK;
}
IFACEMETHODIMP
get_OccludedRect(ABI::Windows::Foundation::Rect* rect) override {
rect->X = rect->Y = rect->Width = rect->Height = showing_ ? 10 : 0;
return S_OK;
}
bool CallbacksValid() const { return show_handler_ && hide_handler_; }
private:
~MockInputPane() override = default;
bool showing_ = false;
Microsoft::WRL::ComPtr<InputPaneEventHandler> show_handler_;
Microsoft::WRL::ComPtr<InputPaneEventHandler> hide_handler_;
};
class OnScreenKeyboardTest : public ::testing::Test {
public:
OnScreenKeyboardTest(const OnScreenKeyboardTest&) = delete;
OnScreenKeyboardTest& operator=(const OnScreenKeyboardTest&) = delete;
protected:
OnScreenKeyboardTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {}
std::unique_ptr<OnScreenKeyboardDisplayManagerTabTip> CreateTabTip() {
return std::make_unique<OnScreenKeyboardDisplayManagerTabTip>(nullptr);
}
std::unique_ptr<OnScreenKeyboardDisplayManagerInputPane> CreateInputPane() {
return std::make_unique<OnScreenKeyboardDisplayManagerInputPane>(nullptr);
}
void WaitForEventsWithTimeDelay(int64_t time_delta_ms = 10) {
base::RunLoop run_loop;
task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(time_delta_ms));
run_loop.Run();
}
private:
base::test::TaskEnvironment task_environment_;
};
// This test validates the on screen keyboard path (tabtip.exe) which is read
// from the registry.
TEST_F(OnScreenKeyboardTest, OSKPath) {
std::unique_ptr<OnScreenKeyboardDisplayManagerTabTip>
keyboard_display_manager(CreateTabTip());
EXPECT_NE(nullptr, keyboard_display_manager);
std::wstring osk_path;
EXPECT_TRUE(keyboard_display_manager->GetOSKPath(&osk_path));
EXPECT_FALSE(osk_path.empty());
EXPECT_TRUE(osk_path.find(L"tabtip.exe") != std::wstring::npos);
// The path read from the registry can be quoted. To check for the existence
// of the file we use the base::PathExists function which internally uses the
// GetFileAttributes API which does not accept quoted strings. Our workaround
// is to look for quotes in the first and last position in the string and
// erase them.
if ((osk_path.front() == L'"') && (osk_path.back() == L'"'))
osk_path = osk_path.substr(1, osk_path.size() - 2);
EXPECT_TRUE(base::PathExists(base::FilePath(osk_path)));
}
TEST_F(OnScreenKeyboardTest, InputPane) {
// InputPane is supported only on RS1 and later.
if (base::win::GetVersion() < base::win::Version::WIN10_RS1)
return;
std::unique_ptr<OnScreenKeyboardDisplayManagerInputPane>
keyboard_display_manager = CreateInputPane();
std::unique_ptr<MockVirtualKeyboardControllerObserver> observer =
std::make_unique<MockVirtualKeyboardControllerObserver>();
Microsoft::WRL::ComPtr<MockInputPane> input_pane =
Microsoft::WRL::Make<MockInputPane>();
keyboard_display_manager->SetInputPaneForTesting(input_pane);
EXPECT_CALL(*observer, OnKeyboardVisible(testing::_)).Times(1);
keyboard_display_manager->AddObserver(observer.get());
keyboard_display_manager->DisplayVirtualKeyboard();
// Additional 300ms for debounce timer.
WaitForEventsWithTimeDelay(400);
testing::Mock::VerifyAndClearExpectations(observer.get());
EXPECT_CALL(*observer, OnKeyboardHidden()).Times(1);
keyboard_display_manager->DismissVirtualKeyboard();
// Additional 300ms for debounce timer.
WaitForEventsWithTimeDelay(400);
keyboard_display_manager->RemoveObserver(observer.get());
}
TEST_F(OnScreenKeyboardTest, InputPaneDebounceTimerTest) {
// InputPane is supported only on RS1 and later.
if (base::win::GetVersion() < base::win::Version::WIN10_RS1)
return;
std::unique_ptr<OnScreenKeyboardDisplayManagerInputPane>
keyboard_display_manager = CreateInputPane();
std::unique_ptr<MockVirtualKeyboardControllerObserver> observer =
std::make_unique<MockVirtualKeyboardControllerObserver>();
Microsoft::WRL::ComPtr<MockInputPane> input_pane =
Microsoft::WRL::Make<MockInputPane>();
keyboard_display_manager->SetInputPaneForTesting(input_pane);
EXPECT_CALL(*observer, OnKeyboardVisible(testing::_)).Times(1);
keyboard_display_manager->AddObserver(observer.get());
keyboard_display_manager->DisplayVirtualKeyboard();
keyboard_display_manager->DismissVirtualKeyboard();
keyboard_display_manager->DisplayVirtualKeyboard();
keyboard_display_manager->DismissVirtualKeyboard();
keyboard_display_manager->DisplayVirtualKeyboard();
// Additional 300ms for debounce timer.
WaitForEventsWithTimeDelay(400);
testing::Mock::VerifyAndClearExpectations(observer.get());
EXPECT_CALL(*observer, OnKeyboardHidden()).Times(1);
keyboard_display_manager->DismissVirtualKeyboard();
keyboard_display_manager->DisplayVirtualKeyboard();
keyboard_display_manager->DismissVirtualKeyboard();
// Additional 300ms for debounce timer.
WaitForEventsWithTimeDelay(400);
keyboard_display_manager->RemoveObserver(observer.get());
}
TEST_F(OnScreenKeyboardTest, InputPaneDestruction) {
// InputPane is supported only on RS1 and later.
if (base::win::GetVersion() < base::win::Version::WIN10_RS1)
return;
std::unique_ptr<OnScreenKeyboardDisplayManagerInputPane>
keyboard_display_manager = CreateInputPane();
std::unique_ptr<MockVirtualKeyboardControllerObserver> observer =
std::make_unique<MockVirtualKeyboardControllerObserver>();
Microsoft::WRL::ComPtr<MockInputPane> input_pane =
Microsoft::WRL::Make<MockInputPane>();
keyboard_display_manager->SetInputPaneForTesting(input_pane);
EXPECT_CALL(*observer, OnKeyboardVisible(testing::_)).Times(1);
keyboard_display_manager->AddObserver(observer.get());
keyboard_display_manager->DisplayVirtualKeyboard();
// Additional 300ms for debounce timer.
WaitForEventsWithTimeDelay(400);
EXPECT_TRUE(input_pane->CallbacksValid());
testing::Mock::VerifyAndClearExpectations(observer.get());
EXPECT_CALL(*observer, OnKeyboardHidden()).Times(1);
keyboard_display_manager->DismissVirtualKeyboard();
// Additional 300ms for debounce timer.
WaitForEventsWithTimeDelay(400);
keyboard_display_manager->RemoveObserver(observer.get());
keyboard_display_manager = nullptr;
WaitForEventsWithTimeDelay(400);
EXPECT_FALSE(input_pane->CallbacksValid());
}
} // namespace ui
|