File: mock_ime_input_context_handler.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 5,054 bytes parent folder | download | duplicates (8)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_IME_ASH_MOCK_IME_INPUT_CONTEXT_HANDLER_H_
#define UI_BASE_IME_ASH_MOCK_IME_INPUT_CONTEXT_HANDLER_H_

#include <stdint.h>

#include <string>
#include <string_view>

#include "base/component_export.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "ui/base/ime/ash/text_input_target.h"
#include "ui/base/ime/composition_text.h"
#include "ui/events/event.h"
#include "ui/gfx/range/range.h"

namespace ui {
class InputMethod;
}

namespace ash {

class COMPONENT_EXPORT(UI_BASE_IME_ASH) MockIMEInputContextHandler
    : public TextInputTarget {
 public:
  struct UpdateCompositionTextArg {
    ui::CompositionText composition_text;
    gfx::Range selection;
    bool is_visible;
  };

  struct DeleteSurroundingTextArg {
    uint32_t num_char16s_before_cursor;
    uint32_t num_char16s_after_cursor;
  };

  struct ReplaceSurroundingTextArg {
    uint32_t length_before_selection;
    uint32_t length_after_selection;
    std::u16string replacement_text;
  };

  class Observer : public base::CheckedObserver {
   public:
    // Called whenever the commit text is updated.
    virtual void OnCommitText(const std::u16string& text) = 0;
  };

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  MockIMEInputContextHandler();
  virtual ~MockIMEInputContextHandler();

  void CommitText(
      const std::u16string& text,
      ui::TextInputClient::InsertTextCursorBehavior cursor_behavior) override;
  void UpdateCompositionText(const ui::CompositionText& text,
                             uint32_t cursor_pos,
                             bool visible) override;

  bool SetCompositionRange(
      uint32_t before,
      uint32_t after,
      const std::vector<ui::ImeTextSpan>& text_spans) override;
  bool SetComposingRange(
      uint32_t start,
      uint32_t end,
      const std::vector<ui::ImeTextSpan>& text_spans) override;
  gfx::Range GetAutocorrectRange() override;
  void SetAutocorrectRange(const gfx::Range& range,
                           SetAutocorrectRangeDoneCallback callback) override;
  bool ClearGrammarFragments(const gfx::Range& range) override;
  std::optional<ui::GrammarFragment> GetGrammarFragmentAtCursor() override;
  bool AddGrammarFragments(
      const std::vector<ui::GrammarFragment>& fragments) override;
  void DeleteSurroundingText(uint32_t num_char16s_before_cursor,
                             uint32_t num_char16s_after_cursor) override;
  void ReplaceSurroundingText(uint32_t length_before_selection,
                              uint32_t length_after_selection,
                              std::u16string_view replacement_text) override;
  SurroundingTextInfo GetSurroundingTextInfo() override;
  void SendKeyEvent(ui::KeyEvent* event) override;
  ui::InputMethod* GetInputMethod() override;
  void ConfirmComposition(bool reset_engine) override;
  bool HasCompositionText() override;
  ukm::SourceId GetClientSourceForMetrics() override;

  std::vector<ui::GrammarFragment> get_grammar_fragments() const {
    return grammar_fragments_;
  }

  void set_cursor_range(gfx::Range range) { cursor_range_ = range; }
  int commit_text_call_count() const { return commit_text_call_count_; }
  int update_preedit_text_call_count() const {
    return update_preedit_text_call_count_;
  }

  int delete_surrounding_text_call_count() const {
    return delete_surrounding_text_call_count_;
  }

  int send_key_event_call_count() const { return sent_key_events_.size(); }

  const std::u16string& last_commit_text() const { return last_commit_text_; }

  const UpdateCompositionTextArg& last_update_composition_arg() const {
    return last_update_composition_arg_;
  }

  const DeleteSurroundingTextArg& last_delete_surrounding_text_arg() const {
    return last_delete_surrounding_text_arg_;
  }

  const ReplaceSurroundingTextArg& last_replace_surrounding_text_arg() const {
    return last_replace_surrounding_text_arg_;
  }

  void set_autocorrect_enabled(bool enabled) {
    autocorrect_enabled_ = enabled;
    if (!enabled) {
      autocorrect_range_ = gfx::Range();
    }
  }

  const std::vector<ui::KeyEvent>& sent_key_events() const {
    return sent_key_events_;
  }

  // Resets all call count.
  void Reset();

 private:
  int commit_text_call_count_;
  int update_preedit_text_call_count_;
  int delete_surrounding_text_call_count_;
  std::u16string last_commit_text_;
  std::vector<ui::KeyEvent> sent_key_events_;
  UpdateCompositionTextArg last_update_composition_arg_;
  DeleteSurroundingTextArg last_delete_surrounding_text_arg_;
  gfx::Range autocorrect_range_;
  bool autocorrect_enabled_ = true;
  std::vector<ui::GrammarFragment> grammar_fragments_;
  gfx::Range cursor_range_;
  ReplaceSurroundingTextArg last_replace_surrounding_text_arg_;
  base::ObserverList<Observer> observers_;
};

}  // namespace ash

#endif  // UI_BASE_IME_ASH_MOCK_IME_INPUT_CONTEXT_HANDLER_H_