OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_PUBLIC_TEST_TEXT_INPUT_TEST_UTILS_H_ | |
6 #define CONTENT_PUBLIC_TEST_TEXT_INPUT_TEST_UTILS_H_ | |
7 | |
8 #include <string> | |
9 #include <unordered_map> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "ui/base/ime/text_input_mode.h" | |
14 #include "ui/base/ime/text_input_type.h" | |
15 | |
16 namespace content { | |
17 | |
18 class RenderWidgetHostView; | |
19 class RenderWidgetHostViewBase; | |
20 class WebContents; | |
21 struct TextInputState; | |
22 | |
23 // Returns the |TextInputState.type| from the TextInputManager owned by | |
24 // |web_contents|. | |
25 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents); | |
26 | |
27 // This method returns true of |view| is registered in the TextInputManager that | |
Charlie Reis
2016/06/02 22:03:29
nit: s/of/if/
| |
28 // is owned by |web_contents|. If that is the case, the value of |type| will be | |
29 // the |TextInputState.type| corresponding to the |view|. Returns false if|view| | |
Charlie Reis
2016/06/02 22:03:29
nit: Space after "if"
| |
30 // is not registered. | |
31 bool GetTextInputTypeForView(WebContents* web_contents, | |
32 RenderWidgetHostView* view, | |
33 ui::TextInputType* type); | |
34 | |
35 // Returns the RWHV corresponding to the frame with a focused <input> within the | |
36 // given WebContents. | |
37 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents); | |
38 | |
39 // This class provides the necessary API for accessing the state of and also | |
40 // observing the TextInputManager for WebContents. | |
41 class TextInputManagerTester { | |
42 public: | |
43 using Callback = base::Callback<void(TextInputManagerTester*)>; | |
44 | |
45 TextInputManagerTester(WebContents* web_contents); | |
46 virtual ~TextInputManagerTester(); | |
47 | |
48 // Sets a callback which is invoked when a RWHV calls UpdateTextInputState | |
49 // on the TextInputManager which is being observed. | |
50 void SetUpdateTextInputStateCalledCallback(const Callback& callback); | |
51 | |
52 // Returns true if there is a focused <input> and populates |type| with | |
53 // |TextInputState.type| of the TextInputManager. | |
54 bool GetTextInputType(ui::TextInputType* type); | |
55 | |
56 // Returns true if there is a focused <input> and populates |value| with | |
57 // |TextInputState.value| of the TextInputManager. | |
58 bool GetTextInputValue(std::string* value); | |
59 | |
60 // Returns the RenderWidgetHostView with a focused <input> element or nullptr | |
61 // if none exists. | |
62 const RenderWidgetHostView* GetActiveView(); | |
63 | |
64 // Returns the RenderWidgetHostView which has most recently called | |
65 // TextInputManager::UpdateTextInputState on the TextInputManager which is | |
66 // being observed. | |
67 const RenderWidgetHostView* GetUpdatedView(); | |
68 | |
69 // Returns true if a call to TextInputManager::UpdateTextInputState has led | |
70 // to a change in TextInputState (since the time the observer has been | |
71 // created). | |
72 bool IsTextInputStateChanged(); | |
73 | |
74 private: | |
75 // The actual internal observer of the TextInputManager. | |
76 class InternalObserver; | |
77 | |
78 std::unique_ptr<InternalObserver> observer_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(TextInputManagerTester); | |
81 }; | |
82 | |
83 // This class observes the lifetime of a RenderWidgetHostView. | |
84 class TestRenderWidgetHostViewDestructionObserver { | |
85 public: | |
86 TestRenderWidgetHostViewDestructionObserver(RenderWidgetHostView* view); | |
87 virtual ~TestRenderWidgetHostViewDestructionObserver(); | |
88 | |
89 // Waits for the RWHV which is being observed to get destroyed. | |
90 void Wait(); | |
91 | |
92 private: | |
93 // The actual internal observer of RenderWidgetHostViewBase. | |
94 class InternalObserver; | |
95 | |
96 std::unique_ptr<InternalObserver> observer_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(TestRenderWidgetHostViewDestructionObserver); | |
99 }; | |
100 | |
101 // Helper class to create TextInputState structs on the browser side and send it | |
102 // to the given RenderWidgetHostView. This class can be used for faking changes | |
103 // in TextInputState for testing on the browser side. | |
104 class TextInputStateSender { | |
105 public: | |
106 explicit TextInputStateSender(RenderWidgetHostView* view); | |
107 virtual ~TextInputStateSender(); | |
108 | |
109 void Send(); | |
110 | |
111 void SetFromCurrentState(); | |
112 | |
113 // The required setter methods. These setter methods can be used to call | |
114 // RenderWidgetHostViewBase::TextInputStateChanged with fake, customized | |
115 // TextInputState. Used for unit-testing on the browser side. | |
116 void SetType(ui::TextInputType type); | |
117 void SetMode(ui::TextInputMode mode); | |
118 void SetFlags(int flags); | |
119 void SetCanComposeInline(bool can_compose_inline); | |
120 void SetShowImeIfNeeded(bool show_ime_if_needed); | |
121 void SetIsNonImeChange(bool is_non_ime_change); | |
122 | |
123 private: | |
124 std::unique_ptr<TextInputState> text_input_state_; | |
125 RenderWidgetHostViewBase* const view_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(TextInputStateSender); | |
128 }; | |
129 | |
130 // This class is intended to observe the InputMethod. | |
131 class TestInputMethodObserver { | |
132 public: | |
133 // static | |
134 // Creates and returns a platform specific implementation of an | |
135 // InputMethodObserver. | |
136 static std::unique_ptr<TestInputMethodObserver> Create( | |
137 WebContents* web_contents); | |
138 | |
139 virtual ~TestInputMethodObserver(); | |
140 | |
141 virtual ui::TextInputType GetTextInputTypeFromClient() = 0; | |
142 | |
143 virtual void SetOnTextInputTypeChangedCallback( | |
144 const base::Closure& callback) = 0; | |
145 virtual void SetOnShowImeIfNeededCallback(const base::Closure& callback) = 0; | |
146 | |
147 protected: | |
148 TestInputMethodObserver(); | |
149 }; | |
150 | |
151 } // namespace content | |
152 | |
153 #endif // CONTENT_PUBLIC_TEST_TEXT_INPUT_TEST_UTILS_H_ | |
OLD | NEW |