File: emoji_suggester_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 (442 lines) | stat: -rw-r--r-- 17,565 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
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
// 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/emoji_suggester.h"

#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ash/input_method/input_method_engine.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"

namespace ash {
namespace input_method {
namespace {

using AssistiveSuggestion = ime::AssistiveSuggestion;
using AssistiveSuggestionMode = ime::AssistiveSuggestionMode;
using AssistiveSuggestionType = ime::AssistiveSuggestionType;

}  // namespace

ui::KeyEvent CreateKeyEventFromCode(const ui::DomCode& code) {
  return ui::KeyEvent(ui::EventType::kKeyPressed, ui::VKEY_UNKNOWN, code,
                      ui::EF_NONE, ui::DomKey::NONE, ui::EventTimeForNow());
}

const char kEmojiData[] = "happy,😀;😃;😄";
const int kContextId = 24601;

class TestSuggestionHandler : public SuggestionHandlerInterface {
 public:
  bool SetButtonHighlighted(int context_id,
                            const ui::ime::AssistiveWindowButton& button,
                            bool highlighted,
                            std::string* error) override {
    switch (button.id) {
      case ui::ime::ButtonId::kLearnMore:
        learn_more_button_highlighted_ = highlighted;
        return true;
      case ui::ime::ButtonId::kSuggestion:
        // If highlighted, needs to unhighlight previously highlighted button.
        if (currently_highlighted_index_ != INT_MAX && highlighted) {
          candidate_highlighted_[currently_highlighted_index_] = 0;
        }
        currently_highlighted_index_ =
            highlighted ? button.suggestion_index : INT_MAX;
        candidate_highlighted_[button.suggestion_index] = highlighted ? 1 : 0;
        return true;
      default:
        return false;
    }
  }

  bool SetAssistiveWindowProperties(
      int context_id,
      const AssistiveWindowProperties& assistive_window,
      std::string* error) override {
    context_id_ = context_id;
    candidate_highlighted_.clear();
    for (size_t i = 0; i < assistive_window.candidates.size(); i++) {
      candidate_highlighted_.push_back(0);
    }
    show_indices_ = assistive_window.show_indices;
    show_setting_link_ = assistive_window.show_setting_link;
    return true;
  }

  void VerifyShowIndices(bool show_indices) {
    EXPECT_EQ(show_indices_, show_indices);
  }

  void VerifyLearnMoreButtonHighlighted(const bool highlighted) {
    EXPECT_EQ(learn_more_button_highlighted_, highlighted);
  }

  void VerifyCandidateHighlighted(const int index, const bool highlighted) {
    int expect = highlighted ? 1 : 0;
    EXPECT_EQ(candidate_highlighted_[index], expect);
  }

  void VerifyShowSettingLink(const bool show_setting_link) {
    EXPECT_EQ(show_setting_link_, show_setting_link);
  }

  void VerifyContextId(const int context_id) {
    EXPECT_EQ(context_id_, context_id);
  }

  bool DismissSuggestion(int context_id, std::string* error) override {
    return false;
  }

  bool AcceptSuggestion(int context_id, std::string* error) override {
    return false;
  }

  void OnSuggestionsChanged(
      const std::vector<std::string>& suggestions) override {}

  void ClickButton(const ui::ime::AssistiveWindowButton& button) override {}

  bool AcceptSuggestionCandidate(int context_id,
                                 const std::u16string& candidate,
                                 size_t delete_previous_utf16_len,
                                 std::string* error) override {
    return false;
  }

  bool SetSuggestion(int context_id,
                     const ui::ime::SuggestionDetails& details,
                     std::string* error) override {
    return false;
  }

  void Announce(const std::u16string& message) override {}

 private:
  bool show_indices_ = false;
  bool show_setting_link_ = false;
  bool learn_more_button_highlighted_ = false;
  std::vector<int> candidate_highlighted_;
  size_t currently_highlighted_index_ = INT_MAX;
  int context_id_ = -1;
};

class EmojiSuggesterTest : public testing::Test {
 protected:
  void SetUp() override {
    engine_ = std::make_unique<TestSuggestionHandler>();
    profile_ = std::make_unique<TestingProfile>();
    emoji_suggester_ =
        std::make_unique<EmojiSuggester>(engine_.get(), profile_.get());
    emoji_suggester_->LoadEmojiMapForTesting(kEmojiData);
    chrome_keyboard_controller_client_ =
        ChromeKeyboardControllerClient::CreateForTest();
    chrome_keyboard_controller_client_->set_keyboard_visible_for_test(false);
    emoji_suggester_->OnFocus(kContextId);
  }

  SuggestionStatus Press(ui::DomCode code) {
    return emoji_suggester_->HandleKeyEvent(CreateKeyEventFromCode(code));
  }

  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<EmojiSuggester> emoji_suggester_;
  std::unique_ptr<TestSuggestionHandler> engine_;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<ChromeKeyboardControllerClient>
      chrome_keyboard_controller_client_;
};

TEST_F(EmojiSuggesterTest, SuggestWhenStringEndsWithSpace) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
}

TEST_F(EmojiSuggesterTest, SuggestWhenStringEndsWithSpaceInNewLine) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(
      u"oldline\nhappy ", gfx::Range(14)));
}

TEST_F(EmojiSuggesterTest, PassesContextIdToHandlerOnSuggestion) {
  emoji_suggester_->TrySuggestWithSurroundingText(u"happy ", gfx::Range(6));
  engine_->VerifyContextId(kContextId);
}

TEST_F(EmojiSuggesterTest, SuggestWhenStringStartsWithOpenBracket) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"(happy ",
                                                              gfx::Range(7)));
}

TEST_F(EmojiSuggesterTest, SuggestWhenStringEndsWithSpaceAndIsUppercase) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"HAPPY ",
                                                              gfx::Range(6)));
}

TEST_F(EmojiSuggesterTest, DoNotSuggestWhenStringEndsWithNewLine) {
  EXPECT_FALSE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy\n",
                                                               gfx::Range(6)));
}

TEST_F(EmojiSuggesterTest, DoNotSuggestWhenStringDoesNotEndWithSpace) {
  EXPECT_FALSE(
      emoji_suggester_->TrySuggestWithSurroundingText(u"happy", gfx::Range(5)));
}

TEST_F(EmojiSuggesterTest, DoNotSuggestOnWhenContainsCursorSelection) {
  EXPECT_FALSE(emoji_suggester_->TrySuggestWithSurroundingText(
      u"happy ", gfx::Range(6, 2)));
}

TEST_F(EmojiSuggesterTest, DoNotSuggestOnWhenNotAtEndOfText) {
  EXPECT_FALSE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                               gfx::Range(3)));
}

TEST_F(EmojiSuggesterTest, DoNotSuggestWhenWordNotInMap) {
  EXPECT_FALSE(
      emoji_suggester_->TrySuggestWithSurroundingText(u"hapy ", gfx::Range(5)));
}

TEST_F(EmojiSuggesterTest, DoNotSuggestAfterBlur) {
  emoji_suggester_->OnBlur();
  EXPECT_FALSE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                               gfx::Range(6)));
}

TEST_F(EmojiSuggesterTest, DoNotShowSuggestionWhenVirtualKeyboardEnabled) {
  chrome_keyboard_controller_client_->set_keyboard_visible_for_test(true);
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  EXPECT_FALSE(emoji_suggester_->HasSuggestions());
}

TEST_F(EmojiSuggesterTest, ReturnkBrowsingWhenPressingDown) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  ui::KeyEvent event = CreateKeyEventFromCode(ui::DomCode::ARROW_DOWN);
  EXPECT_EQ(SuggestionStatus::kBrowsing,
            emoji_suggester_->HandleKeyEvent(event));
}

TEST_F(EmojiSuggesterTest, ReturnkBrowsingWhenPressingUp) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  ui::KeyEvent event = CreateKeyEventFromCode(ui::DomCode::ARROW_UP);
  EXPECT_EQ(SuggestionStatus::kBrowsing,
            emoji_suggester_->HandleKeyEvent(event));
}

TEST_F(EmojiSuggesterTest, ReturnkDismissWhenPressingEsc) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  ui::KeyEvent event = CreateKeyEventFromCode(ui::DomCode::ESCAPE);
  EXPECT_EQ(SuggestionStatus::kDismiss,
            emoji_suggester_->HandleKeyEvent(event));
}

TEST_F(EmojiSuggesterTest, ReturnkNotHandledWhenPressDownThenValidNumber) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  ui::KeyEvent event1 = CreateKeyEventFromCode(ui::DomCode::ARROW_DOWN);
  emoji_suggester_->HandleKeyEvent(event1);
  ui::KeyEvent event2 = CreateKeyEventFromCode(ui::DomCode::DIGIT1);
  EXPECT_EQ(SuggestionStatus::kNotHandled,
            emoji_suggester_->HandleKeyEvent(event2));
}

TEST_F(EmojiSuggesterTest, ReturnkNotHandledWhenPressDownThenNotANumber) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  ui::KeyEvent event1 = CreateKeyEventFromCode(ui::DomCode::ARROW_DOWN);
  emoji_suggester_->HandleKeyEvent(event1);
  ui::KeyEvent event2 = CreateKeyEventFromCode(ui::DomCode::US_A);
  EXPECT_EQ(SuggestionStatus::kNotHandled,
            emoji_suggester_->HandleKeyEvent(event2));
}

TEST_F(EmojiSuggesterTest,
       ReturnkNotHandledWhenPressingEnterAndACandidateHasNotBeenChosen) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  ui::KeyEvent event = CreateKeyEventFromCode(ui::DomCode::ENTER);
  EXPECT_EQ(SuggestionStatus::kNotHandled,
            emoji_suggester_->HandleKeyEvent(event));
}

TEST_F(EmojiSuggesterTest,
       ReturnkAcceptWhenPressingEnterAndACandidateHasBeenChosenByPressingDown) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  // Press ui::DomCode::ARROW_DOWN to choose a candidate.
  ui::KeyEvent event1 = CreateKeyEventFromCode(ui::DomCode::ARROW_DOWN);
  emoji_suggester_->HandleKeyEvent(event1);
  ui::KeyEvent event2 = CreateKeyEventFromCode(ui::DomCode::ENTER);
  EXPECT_EQ(SuggestionStatus::kAccept,
            emoji_suggester_->HandleKeyEvent(event2));
}

TEST_F(EmojiSuggesterTest, HighlightFirstCandidateWhenPressingDown) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  Press(ui::DomCode::ARROW_DOWN);
  engine_->VerifyCandidateHighlighted(0, true);
}

TEST_F(EmojiSuggesterTest, HighlightButtonCorrectlyWhenPressingUp) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));

  // Go into the window.
  Press(ui::DomCode::ARROW_DOWN);

  // Press ui::DomCode::ARROW_UP to choose learn more button.
  Press(ui::DomCode::ARROW_UP);
  engine_->VerifyLearnMoreButtonHighlighted(true);

  // Press ui::DomCode::ARROW_UP to go through candidates;
  for (size_t i = emoji_suggester_->GetCandidatesSizeForTesting(); i > 0; i--) {
    Press(ui::DomCode::ARROW_UP);
    engine_->VerifyCandidateHighlighted(i - 1, true);
    engine_->VerifyLearnMoreButtonHighlighted(false);
    if (i != emoji_suggester_->GetCandidatesSizeForTesting()) {
      engine_->VerifyCandidateHighlighted(i, false);
    }
  }

  // Press ui::DomCode::ARROW_UP to go to learn more button from first
  // candidate.
  Press(ui::DomCode::ARROW_UP);
  engine_->VerifyLearnMoreButtonHighlighted(true);
}

TEST_F(EmojiSuggesterTest, HighlightButtonCorrectlyWhenPressingDown) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));

  // Press ui::DomCode::ARROW_DOWN to go through candidates.
  for (size_t i = 0; i < emoji_suggester_->GetCandidatesSizeForTesting(); i++) {
    Press(ui::DomCode::ARROW_DOWN);
    engine_->VerifyCandidateHighlighted(i, true);
    engine_->VerifyLearnMoreButtonHighlighted(false);
    if (i != 0) {
      engine_->VerifyCandidateHighlighted(i - 1, false);
    }
  }

  // Go to LearnMore Button
  Press(ui::DomCode::ARROW_DOWN);
  engine_->VerifyLearnMoreButtonHighlighted(true);
  engine_->VerifyCandidateHighlighted(
      emoji_suggester_->GetCandidatesSizeForTesting() - 1, false);

  // Go to first candidate
  Press(ui::DomCode::ARROW_DOWN);
  engine_->VerifyLearnMoreButtonHighlighted(false);
  engine_->VerifyCandidateHighlighted(0, true);
}

TEST_F(EmojiSuggesterTest,
       OpenSettingWhenPressingEnterAndLearnMoreButtonIsChosen) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));

  // Go into the window.
  Press(ui::DomCode::ARROW_DOWN);
  // Choose Learn More Button.
  Press(ui::DomCode::ARROW_UP);
  engine_->VerifyLearnMoreButtonHighlighted(true);

  EXPECT_EQ(Press(ui::DomCode::ENTER), SuggestionStatus::kOpenSettings);
}

TEST_F(EmojiSuggesterTest, DoesNotShowIndicesWhenFirstSuggesting) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));

  engine_->VerifyShowIndices(false);
}

TEST_F(EmojiSuggesterTest, DoesNotShowIndexAfterPressingDown) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  Press(ui::DomCode::ARROW_DOWN);

  engine_->VerifyShowIndices(false);
}

TEST_F(EmojiSuggesterTest, DoesNotShowIndicesAfterGettingSuggestionsTwice) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));

  engine_->VerifyShowIndices(false);
}

TEST_F(EmojiSuggesterTest,
       DoesNotShowIndicesAfterPressingDownThenGetNewSuggestions) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  Press(ui::DomCode::ARROW_DOWN);
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));

  engine_->VerifyShowIndices(false);
}

TEST_F(EmojiSuggesterTest, ShowSettingLinkCorrectly) {
  for (int i = 0; i < kEmojiSuggesterShowSettingMaxCount; i++) {
    emoji_suggester_->TrySuggestWithSurroundingText(u"happy ", gfx::Range(6));
    // Dismiss suggestion.
    Press(ui::DomCode::ESCAPE);
    engine_->VerifyShowSettingLink(true);
  }
  emoji_suggester_->TrySuggestWithSurroundingText(u"happy ", gfx::Range(6));
  engine_->VerifyShowSettingLink(false);
}

TEST_F(EmojiSuggesterTest, IsShowingSuggestionTrueWhenCandidatesAvailable) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  EXPECT_TRUE(emoji_suggester_->HasSuggestions());
}

TEST_F(EmojiSuggesterTest, IsShowingSuggestionFalseWhenCandidatesUnavailable) {
  EXPECT_FALSE(
      emoji_suggester_->TrySuggestWithSurroundingText(u"hapy", gfx::Range(4)));
  EXPECT_FALSE(emoji_suggester_->HasSuggestions());
}

TEST_F(EmojiSuggesterTest, GetSuggestionReturnsCandidatesWhenAvailable) {
  EXPECT_TRUE(emoji_suggester_->TrySuggestWithSurroundingText(u"happy ",
                                                              gfx::Range(6)));
  EXPECT_EQ(
      emoji_suggester_->GetSuggestions(),
      (std::vector<AssistiveSuggestion>{
          AssistiveSuggestion{.mode = AssistiveSuggestionMode::kPrediction,
                              .type = AssistiveSuggestionType::kAssistiveEmoji,
                              .text = "😀"},
          AssistiveSuggestion{.mode = AssistiveSuggestionMode::kPrediction,
                              .type = AssistiveSuggestionType::kAssistiveEmoji,
                              .text = "😃"},
          AssistiveSuggestion{.mode = AssistiveSuggestionMode::kPrediction,
                              .type = AssistiveSuggestionType::kAssistiveEmoji,
                              .text = "😄"},
      }));
}

TEST_F(EmojiSuggesterTest,
       GetSuggestionDoesNotReturnCandidatesWhenUnavailable) {
  EXPECT_FALSE(
      emoji_suggester_->TrySuggestWithSurroundingText(u"hapy", gfx::Range(4)));
  EXPECT_TRUE(emoji_suggester_->GetSuggestions().empty());
}
}  // namespace input_method
}  // namespace ash