File: assistive_window_controller_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 (482 lines) | stat: -rw-r--r-- 18,094 bytes parent folder | download | duplicates (5)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/input_method/assistive_window_controller.h"

#include "ash/constants/ash_pref_names.h"
#include "base/feature_list.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/browser/ash/input_method/assistive_window_controller_delegate.h"
#include "chrome/browser/ui/ash/input_method/announcement_view.h"
#include "chrome/browser/ui/ash/input_method/suggestion_details.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/services/ime/public/cpp/assistive_suggestions.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window.h"
#include "ui/base/ime/ash/ime_assistive_window_handler_interface.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/wm/core/window_util.h"

namespace {
const char16_t kAnnounceString[] = u"announce string";
}  // namespace

namespace ash {
namespace input_method {

constexpr size_t kShowSuggestionDelay = 5;

class MockDelegate : public AssistiveWindowControllerDelegate {
 public:
  ~MockDelegate() override = default;
  void AssistiveWindowButtonClicked(
      const ui::ime::AssistiveWindowButton& button) const override {}
  void AssistiveWindowChanged(
      const ash::ime::AssistiveWindow& window) const override {}
};

class TestAnnouncementView : public ui::ime::AnnouncementView {
 public:
  TestAnnouncementView() = default;
  void VerifyAnnouncement(const std::u16string& expected_text) {
    EXPECT_EQ(text_, expected_text);
  }

  void Announce(const std::u16string& text) override { text_ = text; }
  void AnnounceAfterDelay(const std::u16string& text,
                          base::TimeDelta delay) override {
    text_ = text;
  }

 private:
  std::u16string text_;
};

class AssistiveWindowControllerTest : public ChromeAshTestBase {
 protected:
  AssistiveWindowControllerTest()
      : ChromeAshTestBase(std::make_unique<content::BrowserTaskEnvironment>(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME)) {}

  ~AssistiveWindowControllerTest() override = default;

  void SetUp() override {
    ChromeAshTestBase::SetUp();
    std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(1));
    wm::ActivateWindow(window.get());

    profile_ = std::make_unique<TestingProfile>();
    announcement_view_ = std::make_unique<TestAnnouncementView>();
    controller_ = std::make_unique<AssistiveWindowController>(
        delegate_.get(), profile_.get(), announcement_view_.get());
    IMEBridge::Get()->SetAssistiveWindowHandler(controller_.get());

    // TODO(crbug.com/40138718): Create MockSuggestionWindowView to be
    // independent of SuggestionWindowView's implementation.
    static_cast<views::TestViewsDelegate*>(views::ViewsDelegate::GetInstance())
        ->set_layout_provider(ChromeLayoutProvider::CreateLayoutProvider());
  }

  void TearDown() override {
    controller_.reset();
    ChromeAshTestBase::TearDown();
  }

  std::vector<std::u16string> Candidates() {
    std::vector<std::u16string> candidates;
    for (int i = 0; i < 3; i++) {
      std::string candidate = base::NumberToString(i);
      candidates.push_back(base::UTF8ToUTF16(candidate));
    }
    return candidates;
  }

  void InitEmojiSuggestionWindow() {
    emoji_window_.type = ash::ime::AssistiveWindowType::kEmojiSuggestion;
    emoji_window_.visible = true;
    emoji_window_.candidates = Candidates();
  }

  void InitEmojiButton() {
    emoji_button_.window_type = ash::ime::AssistiveWindowType::kEmojiSuggestion;
    emoji_button_.announce_string = kAnnounceString;
  }

  void WaitForSuggestionWindowDelay() {
    task_environment()->FastForwardBy(
        base::Milliseconds(kShowSuggestionDelay + 1));
  }

  std::unique_ptr<AssistiveWindowController> controller_;
  std::unique_ptr<MockDelegate> delegate_ = std::make_unique<MockDelegate>();
  std::unique_ptr<TestingProfile> profile_;
  const std::u16string suggestion_ = u"test";
  ui::ime::AssistiveWindowButton emoji_button_;
  AssistiveWindowProperties emoji_window_;
  std::unique_ptr<TestAnnouncementView> announcement_view_;
};

TEST_F(AssistiveWindowControllerTest, ShowSuggestionDelaysWindowDisplay) {
  ui::ime::SuggestionDetails details;
  details.text = u"asdf";
  details.confirmed_length = 3;

  controller_->ShowSuggestion(details);
  ui::ime::SuggestionWindowView* window_before_delay =
      controller_->GetSuggestionWindowViewForTesting();
  WaitForSuggestionWindowDelay();
  ui::ime::SuggestionWindowView* window_after_delay =
      controller_->GetSuggestionWindowViewForTesting();

  EXPECT_EQ(window_before_delay, nullptr);
  EXPECT_NE(window_after_delay, nullptr);
}

TEST_F(AssistiveWindowControllerTest,
       SetBoundsAfterShowSuggestionCancelsDelay) {
  ui::ime::SuggestionDetails details;
  details.text = u"asdf";
  details.confirmed_length = 3;
  gfx::Rect caret_bounds(0, 0, 100, 100);

  controller_->ShowSuggestion(details);
  controller_->SetBounds(Bounds{.caret = caret_bounds});
  ui::ime::SuggestionWindowView* suggestion_window_view =
      controller_->GetSuggestionWindowViewForTesting();

  EXPECT_NE(suggestion_window_view, nullptr);
}

TEST_F(AssistiveWindowControllerTest, ShowSuggestionSetsConfirmedLength) {
  ui::ime::SuggestionDetails details;
  details.text = u"asdf";
  details.confirmed_length = 3;

  controller_->ShowSuggestion(details);

  EXPECT_EQ(controller_->GetConfirmedLength(), 3u);
}

TEST_F(AssistiveWindowControllerTest, ConfirmedLength0SetsBoundsToCaretBounds) {
  ui::ime::SuggestionDetails details;
  details.text = suggestion_;
  details.confirmed_length = 0;

  controller_->ShowSuggestion(details);
  WaitForSuggestionWindowDelay();
  ui::ime::SuggestionWindowView* suggestion_view =
      controller_->GetSuggestionWindowViewForTesting();

  ASSERT_NE(suggestion_view, nullptr);
  gfx::Rect current_bounds = suggestion_view->GetAnchorRect();
  gfx::Rect caret_bounds(0, 0, 100, 100);
  Bounds bounds;
  bounds.caret = caret_bounds;
  controller_->SetBounds(bounds);

  EXPECT_NE(suggestion_view->GetAnchorRect(), current_bounds);
  EXPECT_EQ(suggestion_view->GetAnchorRect(), caret_bounds);
}

TEST_F(AssistiveWindowControllerTest, ConfirmedLengthNSetsBoundsToCaretBounds) {
  ui::ime::SuggestionDetails details;
  details.text = suggestion_;
  details.confirmed_length = 1;

  controller_->ShowSuggestion(details);
  WaitForSuggestionWindowDelay();
  ui::ime::SuggestionWindowView* suggestion_view =
      controller_->GetSuggestionWindowViewForTesting();

  ASSERT_NE(suggestion_view, nullptr);
  gfx::Rect current_bounds = suggestion_view->GetAnchorRect();
  gfx::Rect caret_bounds(0, 0, 100, 100);
  Bounds bounds;
  bounds.caret = caret_bounds;
  controller_->SetBounds(bounds);

  EXPECT_NE(suggestion_view->GetAnchorRect(), current_bounds);
  EXPECT_EQ(suggestion_view->GetAnchorRect(), caret_bounds);
}

TEST_F(AssistiveWindowControllerTest, WindowTracksCaretBounds) {
  ui::ime::SuggestionDetails details;
  details.text = suggestion_;
  details.confirmed_length = 0;

  controller_->ShowSuggestion(details);
  WaitForSuggestionWindowDelay();
  ui::ime::SuggestionWindowView* suggestion_view =
      controller_->GetSuggestionWindowViewForTesting();

  ASSERT_NE(suggestion_view, nullptr);
  gfx::Rect current_bounds = suggestion_view->GetAnchorRect();
  gfx::Rect caret_bounds_after_one_key(current_bounds.width() + 1,
                                       current_bounds.height());
  gfx::Rect caret_bounds_after_two_key(current_bounds.width() + 2,
                                       current_bounds.height());

  // One char entered
  controller_->SetBounds(Bounds{.caret = caret_bounds_after_one_key});

  // Mimic tracking the last suggestion
  details.confirmed_length = 1;
  controller_->ShowSuggestion(details);

  // Second char entered to text input
  controller_->SetBounds(Bounds{.caret = caret_bounds_after_two_key});

  // Anchor should track the new caret position.
  EXPECT_EQ(suggestion_view->GetAnchorRect(), caret_bounds_after_two_key);
}

TEST_F(AssistiveWindowControllerTest,
       SuggestionViewBoundIsResetAfterHideSuggestionThenShowAgain) {
  // Sets up suggestion_view with confirmed_length = 1.
  ui::ime::SuggestionDetails details;
  details.text = suggestion_;
  details.confirmed_length = 1;

  controller_->ShowSuggestion(details);
  WaitForSuggestionWindowDelay();

  gfx::Rect current_bounds =
      controller_->GetSuggestionWindowViewForTesting()->GetAnchorRect();
  controller_->HideSuggestion();

  // Create new suggestion window.
  AssistiveWindowProperties properties;
  properties.type = ash::ime::AssistiveWindowType::kEmojiSuggestion;
  properties.visible = true;
  properties.candidates = std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(properties);

  gfx::Rect new_caret_bounds(current_bounds.width() + 1,
                             current_bounds.height());
  Bounds bounds;
  bounds.caret = new_caret_bounds;
  controller_->SetBounds(bounds);
  EXPECT_EQ(controller_->GetSuggestionWindowViewForTesting()->GetAnchorRect(),
            new_caret_bounds);
}

TEST_F(AssistiveWindowControllerTest, SetsUndoWindowAnchorRectCorrectly) {
  gfx::Rect autocorrect_bounds(1, 1);
  gfx::Rect caret_bounds(2, 2);

  Bounds bounds;
  bounds.caret = caret_bounds;
  bounds.autocorrect = autocorrect_bounds;
  controller_->SetBounds(bounds);

  AssistiveWindowProperties window;
  window.type = ash::ime::AssistiveWindowType::kUndoWindow;
  window.visible = true;
  controller_->SetAssistiveWindowProperties(window);

  ASSERT_TRUE(controller_->GetUndoWindowForTesting() != nullptr);
  autocorrect_bounds.Inset(-4);
  EXPECT_EQ(controller_->GetUndoWindowForTesting()->GetAnchorRect(),
            autocorrect_bounds);
}

TEST_F(AssistiveWindowControllerTest, SetsEmojiWindowOrientationVertical) {
  // Create new suggestion window.
  AssistiveWindowProperties properties;
  properties.type = ash::ime::AssistiveWindowType::kEmojiSuggestion;
  properties.visible = true;
  properties.candidates = std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(properties);

  ASSERT_TRUE(controller_->GetSuggestionWindowViewForTesting() != nullptr);
  views::BoxLayout::Orientation layout_orientation =
      static_cast<views::BoxLayout*>(
          controller_->GetSuggestionWindowViewForTesting()
              ->multiple_candidate_area_for_testing()
              ->GetLayoutManager())
          ->GetOrientation();
  EXPECT_EQ(layout_orientation, views::BoxLayout::Orientation::kVertical);
}

TEST_F(AssistiveWindowControllerTest,
       SetsPersonalInfoWindowOrientationVertical) {
  // Create new suggestion window.
  AssistiveWindowProperties properties;
  properties.type = ash::ime::AssistiveWindowType::kPersonalInfoSuggestion;
  properties.visible = true;
  properties.candidates = std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(properties);

  ASSERT_TRUE(controller_->GetSuggestionWindowViewForTesting() != nullptr);
  views::BoxLayout::Orientation layout_orientation =
      static_cast<views::BoxLayout*>(
          controller_->GetSuggestionWindowViewForTesting()
              ->multiple_candidate_area_for_testing()
              ->GetLayoutManager())
          ->GetOrientation();
  EXPECT_EQ(layout_orientation, views::BoxLayout::Orientation::kVertical);
}

TEST_F(AssistiveWindowControllerTest, SetsMultiWordWindowOrientationVertical) {
  // Create new suggestion window.
  AssistiveWindowProperties properties;
  properties.type = ash::ime::AssistiveWindowType::kMultiWordSuggestion;
  properties.visible = true;
  properties.candidates = std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(properties);

  ASSERT_TRUE(controller_->GetSuggestionWindowViewForTesting() != nullptr);
  views::BoxLayout::Orientation layout_orientation =
      static_cast<views::BoxLayout*>(
          controller_->GetSuggestionWindowViewForTesting()
              ->multiple_candidate_area_for_testing()
              ->GetLayoutManager())
          ->GetOrientation();
  EXPECT_EQ(layout_orientation, views::BoxLayout::Orientation::kVertical);
}

TEST_F(AssistiveWindowControllerTest,
       SetsDiacriticsWindowOrientationHorizontal) {
  // Create new suggestion window.
  AssistiveWindowProperties properties;
  properties.type =
      ash::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion;
  properties.visible = true;
  properties.candidates = std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(properties);

  ASSERT_TRUE(controller_->GetSuggestionWindowViewForTesting() != nullptr);
  views::BoxLayout::Orientation layout_orientation =
      static_cast<views::BoxLayout*>(
          controller_->GetSuggestionWindowViewForTesting()
              ->multiple_candidate_area_for_testing()
              ->GetLayoutManager())
          ->GetOrientation();
  EXPECT_EQ(layout_orientation, views::BoxLayout::Orientation::kHorizontal);
}

TEST_F(AssistiveWindowControllerTest,
       SetsWindowOrientationHorizontalWhenVerticalWindowAlreadyInitialised) {
  AssistiveWindowProperties init_vertical_properties;
  init_vertical_properties.type =
      ash::ime::AssistiveWindowType::kMultiWordSuggestion;
  init_vertical_properties.visible = true;
  init_vertical_properties.candidates =
      std::vector<std::u16string>({u"vertical"});
  controller_->SetAssistiveWindowProperties(init_vertical_properties);

  AssistiveWindowProperties horizontal_properties;
  horizontal_properties.type =
      ash::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion;
  horizontal_properties.visible = true;
  horizontal_properties.candidates =
      std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(horizontal_properties);

  ASSERT_TRUE(controller_->GetSuggestionWindowViewForTesting() != nullptr);
  views::BoxLayout::Orientation layout_orientation =
      static_cast<views::BoxLayout*>(
          controller_->GetSuggestionWindowViewForTesting()
              ->multiple_candidate_area_for_testing()
              ->GetLayoutManager())
          ->GetOrientation();
  EXPECT_EQ(layout_orientation, views::BoxLayout::Orientation::kHorizontal);
}

TEST_F(AssistiveWindowControllerTest,
       SetsWindowOrientationVerticalWhenHorizontalWindowAlreadyInitialised) {
  AssistiveWindowProperties init_horizontal_properties;
  init_horizontal_properties.type =
      ash::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion;
  init_horizontal_properties.visible = true;
  init_horizontal_properties.candidates =
      std::vector<std::u16string>({u"horizontal"});
  controller_->SetAssistiveWindowProperties(init_horizontal_properties);

  AssistiveWindowProperties vertical_properties;
  vertical_properties.type =
      ash::ime::AssistiveWindowType::kMultiWordSuggestion;
  vertical_properties.visible = true;
  vertical_properties.candidates = std::vector<std::u16string>({u"candidate"});
  controller_->SetAssistiveWindowProperties(vertical_properties);

  ASSERT_TRUE(controller_->GetSuggestionWindowViewForTesting() != nullptr);
  views::BoxLayout::Orientation layout_orientation =
      static_cast<views::BoxLayout*>(
          controller_->GetSuggestionWindowViewForTesting()
              ->multiple_candidate_area_for_testing()
              ->GetLayoutManager())
          ->GetOrientation();
  EXPECT_EQ(layout_orientation, views::BoxLayout::Orientation::kVertical);
}
TEST_F(AssistiveWindowControllerTest,
       AnnouncesWhenSetButtonHighlightedInEmojiWindowHasAnnounceString) {
  profile_->GetPrefs()->SetBoolean(
      ash::prefs::kAccessibilitySpokenFeedbackEnabled, true);
  InitEmojiSuggestionWindow();
  InitEmojiButton();

  controller_->SetAssistiveWindowProperties(emoji_window_);
  controller_->SetButtonHighlighted(emoji_button_, true);
  task_environment()->RunUntilIdle();

  announcement_view_->VerifyAnnouncement(kAnnounceString);
}

TEST_F(
    AssistiveWindowControllerTest,
    DoesNotAnnounceWhenSetButtonHighlightedInEmojiWindowDoesNotHaveAnnounceString) {
  profile_->GetPrefs()->SetBoolean(
      ash::prefs::kAccessibilitySpokenFeedbackEnabled, true);
  InitEmojiSuggestionWindow();
  InitEmojiButton();
  emoji_button_.announce_string.clear();

  controller_->SetAssistiveWindowProperties(emoji_window_);
  controller_->SetButtonHighlighted(emoji_button_, true);
  task_environment()->RunUntilIdle();

  announcement_view_->VerifyAnnouncement(std::u16string());
}

TEST_F(AssistiveWindowControllerTest,
       AnnouncesWhenSetButtonHighlightedInUndoWindowHasAnnounceString) {
  profile_->GetPrefs()->SetBoolean(
      ash::prefs::kAccessibilitySpokenFeedbackEnabled, true);
  AssistiveWindowProperties window;
  window.type = ash::ime::AssistiveWindowType::kUndoWindow;
  window.visible = true;
  ui::ime::AssistiveWindowButton button;
  button.window_type = ash::ime::AssistiveWindowType::kUndoWindow;
  button.announce_string = kAnnounceString;

  controller_->SetAssistiveWindowProperties(window);
  controller_->SetButtonHighlighted(button, true);
  task_environment()->RunUntilIdle();

  announcement_view_->VerifyAnnouncement(kAnnounceString);
}

TEST_F(
    AssistiveWindowControllerTest,
    DoesNotAnnounceWhenSetButtonHighlightedAndSuggestionWindowViewIsNotActive) {
  profile_->GetPrefs()->SetBoolean(
      ash::prefs::kAccessibilitySpokenFeedbackEnabled, true);
  InitEmojiButton();

  controller_->SetButtonHighlighted(emoji_button_, true);
  task_environment()->RunUntilIdle();

  announcement_view_->VerifyAnnouncement(std::u16string());
}

}  // namespace input_method
}  // namespace ash