File: virtual_keyboard_private_api_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (231 lines) | stat: -rw-r--r-- 8,523 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_private_api.h"

#include <memory>

#include "base/memory/raw_ptr.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_delegate.h"
#include "extensions/browser/api_unittest.h"

namespace extensions {

namespace {

class MockVirtualKeyboardDelegate : public VirtualKeyboardDelegate {
 public:
  MockVirtualKeyboardDelegate() = default;

  MockVirtualKeyboardDelegate(const MockVirtualKeyboardDelegate&) = delete;
  MockVirtualKeyboardDelegate& operator=(const MockVirtualKeyboardDelegate&) =
      delete;

  ~MockVirtualKeyboardDelegate() override = default;

  // VirtualKeyboardDelegate impl:
  void GetKeyboardConfig(
      OnKeyboardSettingsCallback on_settings_callback) override {}
  void OnKeyboardConfigChanged() override {}
  bool HideKeyboard() override { return false; }
  bool InsertText(const std::u16string& text) override { return false; }
  bool OnKeyboardLoaded() override { return false; }
  void SetHotrodKeyboard(bool enable) override {}
  bool LockKeyboard(bool state) override { return false; }
  bool SendKeyEvent(const std::string& type,
                    int char_value,
                    int key_code,
                    const std::string& key_name,
                    int modifiers) override {
    return false;
  }
  bool ShowLanguageSettings() override { return false; }
  bool ShowSuggestionSettings() override { return false; }
  bool IsSettingsEnabled() override { return false; }
  bool SetVirtualKeyboardMode(api::virtual_keyboard_private::KeyboardMode mode,
                              gfx::Rect target_bounds,
                              OnSetModeCallback on_set_mode_callback) override {
    return false;
  }
  bool SetDraggableArea(
      const api::virtual_keyboard_private::Bounds& rect) override {
    return false;
  }
  bool SetRequestedKeyboardState(
      api::virtual_keyboard_private::KeyboardState state) override {
    return false;
  }

  bool SetOccludedBounds(const std::vector<gfx::Rect>& bounds) override {
    occluded_bounds_ = bounds;
    return true;
  }
  const std::vector<gfx::Rect>& GetOccludedBounds() { return occluded_bounds_; }

  bool SetHitTestBounds(const std::vector<gfx::Rect>& bounds) override {
    hit_test_bounds_ = bounds;
    return true;
  }
  const std::vector<gfx::Rect>& GetHitTestBounds() { return hit_test_bounds_; }

  bool SetAreaToRemainOnScreen(const gfx::Rect& bounds) override {
    area_to_remain_on_screen_ = bounds;
    return true;
  }
  const gfx::Rect& GetAreaToRemainOnScreen() {
    return area_to_remain_on_screen_;
  }

  bool SetWindowBoundsInScreen(const gfx::Rect& bounds_in_screen) override {
    window_bounds_ = bounds_in_screen;
    return true;
  }
  const gfx::Rect& GetWindowBounds() { return window_bounds_; }

  void GetClipboardHistory(
      OnGetClipboardHistoryCallback get_history_callback) override {}
  bool PasteClipboardItem(const std::string& clipboard_item_id) override {
    return false;
  }
  bool DeleteClipboardItem(const std::string& clipboard_item_id) override {
    return false;
  }

  void RestrictFeatures(
      const api::virtual_keyboard::RestrictFeatures::Params& params,
      OnRestrictFeaturesCallback callback) override {
    std::move(callback).Run(api::virtual_keyboard::FeatureRestrictions());
  }

 private:
  std::vector<gfx::Rect> occluded_bounds_;
  std::vector<gfx::Rect> hit_test_bounds_;
  gfx::Rect area_to_remain_on_screen_;
  gfx::Rect window_bounds_;
};

class TestVirtualKeyboardExtensionsAPIClient : public ExtensionsAPIClient {
 public:
  TestVirtualKeyboardExtensionsAPIClient() = default;

  TestVirtualKeyboardExtensionsAPIClient(
      const TestVirtualKeyboardExtensionsAPIClient&) = delete;
  TestVirtualKeyboardExtensionsAPIClient& operator=(
      const TestVirtualKeyboardExtensionsAPIClient&) = delete;

  ~TestVirtualKeyboardExtensionsAPIClient() override = default;

  // ExtensionsAPIClient implementation.
  std::unique_ptr<VirtualKeyboardDelegate> CreateVirtualKeyboardDelegate(
      content::BrowserContext* browser_context) const override {
    auto delegate = std::make_unique<MockVirtualKeyboardDelegate>();
    delegates_[browser_context] = delegate.get();
    return std::move(delegate);
  }

  MockVirtualKeyboardDelegate* GetDelegateForBrowserContext(
      content::BrowserContext* browser_context) const {
    return delegates_[browser_context];
  }

 private:
  // Points to the last mock delegate created for each browser context. Does not
  // own the delegates.
  mutable std::map<content::BrowserContext*,
                   raw_ptr<MockVirtualKeyboardDelegate, CtnExperimental>>
      delegates_;
};

}  // namespace

class VirtualKeyboardPrivateApiUnittest : public ApiUnitTest {
 public:
  VirtualKeyboardPrivateApiUnittest() = default;
  ~VirtualKeyboardPrivateApiUnittest() override = default;

  const TestVirtualKeyboardExtensionsAPIClient& client() const {
    return extensions_api_client_;
  }

 protected:
  TestVirtualKeyboardExtensionsAPIClient extensions_api_client_;
};

TEST_F(VirtualKeyboardPrivateApiUnittest, SetOccludedBoundsWithNoBounds) {
  RunFunction(new VirtualKeyboardPrivateSetOccludedBoundsFunction(), "[[]]");

  const auto bounds = client()
                          .GetDelegateForBrowserContext(browser_context())
                          ->GetOccludedBounds();
  EXPECT_EQ(0U, bounds.size());
}

TEST_F(VirtualKeyboardPrivateApiUnittest, SetOccludedBoundsWithOneBound) {
  RunFunction(new VirtualKeyboardPrivateSetOccludedBoundsFunction(),
              R"([[{ "left": 0, "top": 10, "width": 20, "height": 30 }]])");

  const auto bounds = client()
                          .GetDelegateForBrowserContext(browser_context())
                          ->GetOccludedBounds();
  ASSERT_EQ(1U, bounds.size());
  EXPECT_EQ(gfx::Rect(0, 10, 20, 30), bounds[0]);
}

TEST_F(VirtualKeyboardPrivateApiUnittest, SetOccludedBoundsWithTwoBounds) {
  RunFunction(new VirtualKeyboardPrivateSetOccludedBoundsFunction(),
              R"([[{ "left": 0, "top": 10, "width": 20, "height": 30 },
      { "left": 10, "top": 20, "width": 30, "height": 40 }]])");

  const auto bounds = client()
                          .GetDelegateForBrowserContext(browser_context())
                          ->GetOccludedBounds();
  ASSERT_EQ(2U, bounds.size());
  EXPECT_EQ(gfx::Rect(0, 10, 20, 30), bounds[0]);
  EXPECT_EQ(gfx::Rect(10, 20, 30, 40), bounds[1]);
}

TEST_F(VirtualKeyboardPrivateApiUnittest, SetHitTestBoundsWithNoBounds) {
  RunFunction(new VirtualKeyboardPrivateSetHitTestBoundsFunction(), "[[]]");

  const auto bounds = client()
                          .GetDelegateForBrowserContext(browser_context())
                          ->GetHitTestBounds();
  EXPECT_EQ(0U, bounds.size());
}

TEST_F(VirtualKeyboardPrivateApiUnittest, SetHitTestBoundsWithMultipleBounds) {
  RunFunction(new VirtualKeyboardPrivateSetHitTestBoundsFunction(),
              R"([[{ "left": 0, "top": 10, "width": 20, "height": 30 },
      { "left": 10, "top": 20, "width": 30, "height": 40 }]])");

  const auto bounds = client()
                          .GetDelegateForBrowserContext(browser_context())
                          ->GetHitTestBounds();
  ASSERT_EQ(2U, bounds.size());
  EXPECT_EQ(gfx::Rect(0, 10, 20, 30), bounds[0]);
  EXPECT_EQ(gfx::Rect(10, 20, 30, 40), bounds[1]);
}

TEST_F(VirtualKeyboardPrivateApiUnittest, SetAreaToRemainOnScreenWithBounds) {
  RunFunction(new VirtualKeyboardPrivateSetAreaToRemainOnScreenFunction(),
              R"([{ "left": 0, "top": 0, "width": 10, "height": 20 }])");

  const gfx::Rect bounds = client()
                               .GetDelegateForBrowserContext(browser_context())
                               ->GetAreaToRemainOnScreen();
  EXPECT_EQ(gfx::Rect(0, 0, 10, 20), bounds);
}

TEST_F(VirtualKeyboardPrivateApiUnittest, SetWindowBoundsInScreenWithBounds) {
  RunFunction(new VirtualKeyboardPrivateSetWindowBoundsInScreenFunction(),
              R"([{ "left": 120, "top": 300, "width": 400, "height": 250 }])");

  const gfx::Rect bounds = client()
                               .GetDelegateForBrowserContext(browser_context())
                               ->GetWindowBounds();
  EXPECT_EQ(gfx::Rect(120, 300, 400, 250), bounds);
}

}  // namespace extensions