File: render_widget_host_view_ios_uiview_unittest.mm

package info (click to toggle)
chromium 142.0.7444.175-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,303,984 kB
  • sloc: cpp: 35,488,370; ansic: 7,479,680; javascript: 4,259,373; python: 1,466,844; xml: 757,444; asm: 710,716; pascal: 187,980; sh: 89,247; perl: 88,690; objc: 79,984; sql: 56,984; cs: 42,192; fortran: 24,137; makefile: 22,919; tcl: 15,277; php: 14,018; yacc: 9,005; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (353 lines) | stat: -rw-r--r-- 11,807 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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/render_widget_host_view_ios_uiview.h"

#include "testing/platform_test.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"

@interface RenderWidgetUIView (Testing)
- (BOOL)shouldInsertCharacter:(const blink::WebKeyboardEvent&)webKeyboardEvent;
- (std::string)moveSelectionCommand:(UITextLayoutDirection)direction;
- (std::string)extendSelectionCommand:(UITextLayoutDirection)direction;
- (std::vector<std::string>)
    moveSelectionCommands:(UITextStorageDirection)direction
            byGranularity:(UITextGranularity)granularity;
- (std::vector<std::string>)
    extendSelectionCommands:(UITextStorageDirection)direction
              byGranularity:(UITextGranularity)granularity;
- (std::vector<std::string>)
    deleteSelectionCommands:(UITextStorageDirection)direction
              toGranularity:(UITextGranularity)granularity;
@end

namespace content {

class RenderWidgetHostViewIOSUIViewTest : public PlatformTest {
 public:
  void SetUp() override {
    uiview_ = [[RenderWidgetUIView alloc] initWithWidget:nil];
  }

 protected:
  RenderWidgetUIView* uiview_;
};

blink::WebKeyboardEvent CreateKeyboardEvent(char16_t character,
                                            int modifiers = 0) {
  blink::WebKeyboardEvent event;
  event.text[0] = character;
  event.text[1] = 0;  // Null-terminate the string
  event.SetModifiers(modifiers);
  return event;
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, ShouldInsertCharacter) {
  struct {
    std::string category;
    std::vector<char16_t> characters;
    bool expected_result;
  } test_cases[] = {
      {"Basic Latin", {u'a', u'Z', u'0', u'9', u'@', u' '}, true},
      {"Control Characters", {0x00, 0x01, 0x1F}, false},
      {"Latin-1 Supplement", {0x00A1, 0x00FF}, true},  // ¡, ÿ
      {"Currency Symbols", {0x20AC, 0x00A5}, true},    // €, ¥
      {"Mathematical Symbols", {0x2200, 0x22FF}, true},
  };

  for (const auto& test_case : test_cases) {
    for (char16_t c : test_case.characters) {
      EXPECT_EQ([uiview_ shouldInsertCharacter:CreateKeyboardEvent(c)],
                test_case.expected_result);
    }
  }
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, ShouldInsertMultiCharacterInput) {
  blink::WebKeyboardEvent emoji_event;

  // Set up a multi-character emoji sequence
  const std::vector<char16_t> emoji = {0xD83D, 0xDE00,
                                       0};  // 😀 (surrogate pair)
  int i = 0;
  for (char16_t e : emoji) {
    if (e != 0) {
      emoji_event.text[i++] = e;
    }
  }

  EXPECT_TRUE([uiview_ shouldInsertCharacter:emoji_event]);
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, ShouldNotInsertWithControlModifiers) {
  // Control key should prevent character insertion for ASCII characters
  EXPECT_FALSE([uiview_
      shouldInsertCharacter:CreateKeyboardEvent(
                                u'a', blink::WebInputEvent::kControlKey)]);

  // Meta key should also prevent character insertion for ASCII characters
  EXPECT_FALSE([uiview_
      shouldInsertCharacter:CreateKeyboardEvent(
                                u'b', blink::WebInputEvent::kMetaKey)]);

  // Other modifiers should not prevent insertion
  EXPECT_TRUE([uiview_
      shouldInsertCharacter:CreateKeyboardEvent(
                                u'c', blink::WebInputEvent::kShiftKey)]);
  EXPECT_TRUE(
      [uiview_ shouldInsertCharacter:CreateKeyboardEvent(
                                         u'd', blink::WebInputEvent::kAltKey)]);
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, MoveSelectionCommand) {
  // Create a map of directions to expected commands
  struct {
    UITextLayoutDirection direction;
    std::string expected_command;
  } test_cases[] = {
      {UITextLayoutDirectionLeft, "moveLeft"},
      {UITextLayoutDirectionRight, "moveRight"},
      {UITextLayoutDirectionUp, "moveUp"},
      {UITextLayoutDirectionDown, "moveDown"},
  };

  // Verify each case
  for (const auto& test_case : test_cases) {
    std::string command = [uiview_ moveSelectionCommand:test_case.direction];
    EXPECT_EQ(command, test_case.expected_command);
  }
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, ExtendSelectionCommand) {
  // Create a map of directions to expected commands
  struct {
    UITextLayoutDirection direction;
    std::string expected_command;
  } test_cases[] = {
      {UITextLayoutDirectionLeft, "moveLeftAndModifySelection"},
      {UITextLayoutDirectionRight, "moveRightAndModifySelection"},
      {UITextLayoutDirectionUp, "moveUpAndModifySelection"},
      {UITextLayoutDirectionDown, "moveDownAndModifySelection"},
  };

  // Verify each case
  for (const auto& test_case : test_cases) {
    std::string command = [uiview_ extendSelectionCommand:test_case.direction];
    EXPECT_EQ(command, test_case.expected_command);
  }
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, MoveSelectionCommands) {
  // Define all test cases
  struct {
    UITextStorageDirection direction;
    UITextGranularity granularity;
    std::vector<std::string> expected_commands;
  } test_cases[] = {
      // Character granularity
      {UITextStorageDirectionForward,
       UITextGranularityCharacter,
       {"moveForward"}},
      {UITextStorageDirectionBackward,
       UITextGranularityCharacter,
       {"moveBackward"}},

      // Word granularity
      {UITextStorageDirectionForward,
       UITextGranularityWord,
       {"moveWordForward"}},
      {UITextStorageDirectionBackward,
       UITextGranularityWord,
       {"moveWordBackward"}},

      // Sentence granularity
      {UITextStorageDirectionForward,
       UITextGranularitySentence,
       {"moveToEndOfSentence"}},
      {UITextStorageDirectionBackward,
       UITextGranularitySentence,
       {"moveToBeginningOfSentence"}},

      // Paragraph granularity
      {UITextStorageDirectionForward,
       UITextGranularityParagraph,
       {"moveForward", "moveToEndOfParagraph"}},
      {UITextStorageDirectionBackward,
       UITextGranularityParagraph,
       {"moveBackward", "moveToBeginningOfParagraph"}},

      // Line granularity
      {UITextStorageDirectionForward,
       UITextGranularityLine,
       {"moveToEndOfLine"}},
      {UITextStorageDirectionBackward,
       UITextGranularityLine,
       {"moveToBeginningOfLine"}},

      // Document granularity
      {UITextStorageDirectionForward,
       UITextGranularityDocument,
       {"moveToEndOfDocument"}},
      {UITextStorageDirectionBackward,
       UITextGranularityDocument,
       {"moveToBeginningOfDocument"}},
  };

  // Verify each case
  for (const auto& test_case : test_cases) {
    std::vector<std::string> commands =
        [uiview_ moveSelectionCommands:test_case.direction
                         byGranularity:test_case.granularity];

    ASSERT_EQ(commands.size(), test_case.expected_commands.size());

    for (size_t i = 0; i < commands.size(); i++) {
      EXPECT_EQ(commands[i], test_case.expected_commands[i]);
    }
  }
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, ExtendSelectionCommands) {
  // Define all test cases
  struct {
    UITextStorageDirection direction;
    UITextGranularity granularity;
    std::vector<std::string> expected_commands;
  } test_cases[] = {
      // Character granularity
      {UITextStorageDirectionForward,
       UITextGranularityCharacter,
       {"moveBackwardAndModifySelection"}},
      {UITextStorageDirectionBackward,
       UITextGranularityCharacter,
       {"moveForwardAndModifySelection"}},

      // Word granularity
      {UITextStorageDirectionForward,
       UITextGranularityWord,
       {"moveWordForwardAndModifySelection"}},
      {UITextStorageDirectionBackward,
       UITextGranularityWord,
       {"moveWordBackwardAndModifySelection"}},

      // Sentence granularity
      {UITextStorageDirectionForward,
       UITextGranularitySentence,
       {"moveToEndOfSentenceAndModifySelection"}},
      {UITextStorageDirectionBackward,
       UITextGranularitySentence,
       {"moveToBeginningOfSentenceAndModifySelection"}},

      // Paragraph granularity
      {UITextStorageDirectionForward,
       UITextGranularityParagraph,
       {"moveForwardAndModifySelection",
        "moveToEndOfParagraphAndModifySelection"}},
      {UITextStorageDirectionBackward,
       UITextGranularityParagraph,
       {"moveBackwardAndModifySelection",
        "moveToBeginningOfParagraphAndModifySelection"}},

      // Line granularity
      {UITextStorageDirectionForward,
       UITextGranularityLine,
       {"moveToEndOfLineAndModifySelection"}},
      {UITextStorageDirectionBackward,
       UITextGranularityLine,
       {"moveToBeginningOfLineAndModifySelection"}},

      // Document granularity
      {UITextStorageDirectionForward,
       UITextGranularityDocument,
       {"moveToEndOfDocumentAndModifySelection"}},
      {UITextStorageDirectionBackward,
       UITextGranularityDocument,
       {"moveToBeginningOfDocumentAndModifySelection"}},
  };

  // Verify each case
  for (const auto& test_case : test_cases) {
    std::vector<std::string> commands =
        [uiview_ extendSelectionCommands:test_case.direction
                           byGranularity:test_case.granularity];

    ASSERT_EQ(commands.size(), test_case.expected_commands.size());

    for (size_t i = 0; i < commands.size(); i++) {
      EXPECT_EQ(commands[i], test_case.expected_commands[i]);
    }
  }
}

TEST_F(RenderWidgetHostViewIOSUIViewTest, DeleteSelectionCommands) {
  // Define all test cases
  struct {
    UITextStorageDirection direction;
    UITextGranularity granularity;
    std::vector<std::string> expected_commands;
  } test_cases[] = {
      // Character granularity
      {UITextStorageDirectionForward,
       UITextGranularityCharacter,
       {"deleteForward"}},
      {UITextStorageDirectionBackward,
       UITextGranularityCharacter,
       {"deleteBackward"}},

      // Word granularity
      {UITextStorageDirectionForward,
       UITextGranularityWord,
       {"deleteWordForward"}},
      {UITextStorageDirectionBackward,
       UITextGranularityWord,
       {"deleteWordBackward"}},

      // Sentence granularity
      {UITextStorageDirectionForward,
       UITextGranularitySentence,
       {"moveToEndOfSentenceAndModifySelection", "deleteBackward"}},
      {UITextStorageDirectionBackward,
       UITextGranularitySentence,
       {"moveToBeginningOfSentenceAndModifySelection", "deleteBackward"}},

      // Paragraph granularity
      {UITextStorageDirectionForward,
       UITextGranularityParagraph,
       {"deleteToEndOfParagraph"}},
      {UITextStorageDirectionBackward,
       UITextGranularityParagraph,
       {"deleteToBeginningOfParagraph"}},

      // Line granularity
      {UITextStorageDirectionForward,
       UITextGranularityLine,
       {"deleteToEndOfLine"}},
      {UITextStorageDirectionBackward,
       UITextGranularityLine,
       {"deleteToBeginningOfLine"}},

      // Document granularity (default case)
      {UITextStorageDirectionForward,
       UITextGranularityDocument,
       {"moveToEndOfDocumentAndModifySelection", "deleteBackward"}},
      {UITextStorageDirectionBackward,
       UITextGranularityDocument,
       {"moveToBeginningOfDocumentAndModifySelection", "deleteBackward"}},
  };

  // Verify each case
  for (const auto& test_case : test_cases) {
    std::vector<std::string> commands =
        [uiview_ deleteSelectionCommands:test_case.direction
                           toGranularity:test_case.granularity];

    ASSERT_EQ(commands.size(), test_case.expected_commands.size());

    for (size_t i = 0; i < commands.size(); i++) {
      EXPECT_EQ(commands[i], test_case.expected_commands[i]);
    }
  }
}

}  // namespace content