• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "nweb_log.h"
20 #include "ohos_adapter_helper.h"
21 
22 #define private public
23 #include "imf_adapter_impl.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS::NWeb {
29 namespace {
30 std::shared_ptr<IMFAdapterImpl> g_imf = nullptr;
31 } // namespace
32 
33 class NWebIMFAdapterTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 };
40 
41 class IMFTextListenerTest : public IMFTextListenerAdapter {
42 public:
43     IMFTextListenerTest() = default;
44     ~IMFTextListenerTest() override = default;
InsertText(const std::u16string & text)45     void InsertText(const std::u16string &text) override
46     {
47         WVLOG_I("test InsertText");
48         isInsertText_ = true;
49     }
DeleteForward(int32_t length)50     void DeleteForward(int32_t length) override
51     {
52         WVLOG_I("test DeleteForward");
53         isDeleteForward_ = true;
54     }
DeleteBackward(int32_t length)55     void DeleteBackward(int32_t length) override
56     {
57         WVLOG_I("test DeleteBackward");
58         isDeleteBackward_ = true;
59     }
SendKeyEventFromInputMethod()60     void SendKeyEventFromInputMethod() override
61     {
62         WVLOG_I("test SendKeyEventFromInputMethod");
63         isSendKeyEventFromInputMethod_ = true;
64     }
SendKeyboardInfo(const IMFAdapterKeyboardInfo & info)65     void SendKeyboardInfo(const IMFAdapterKeyboardInfo &info) override
66     {
67         WVLOG_I("test SendKeyboardInfo");
68         isSendKeyboardInfo_ = true;
69     }
SetKeyboardStatus(bool status)70     void SetKeyboardStatus(bool status) override
71     {
72         WVLOG_I("test SetKeyboardStatus");
73         isSetKeyboardStatus_ = true;
74     }
MoveCursor(const IMFAdapterDirection direction)75     void MoveCursor(const IMFAdapterDirection direction) override
76     {
77         WVLOG_I("test MoveCursor");
78         isMoveCursor_ = true;
79     }
HandleSetSelection(int32_t start,int32_t end)80     void HandleSetSelection(int32_t start, int32_t end) override
81     {
82         WVLOG_I("test HandleSetSelection");
83         isHandleSetSelection_ = true;
84     }
HandleExtendAction(int32_t action)85     void HandleExtendAction(int32_t action) override
86     {
87         WVLOG_I("test HandleExtendAction");
88         isHandleExtendAction_ = true;
89     }
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)90     void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override
91     {
92         WVLOG_I("test HandleSelect");
93         isHandleSelect_ = true;
94     }
VerifyAllSuccess()95     bool VerifyAllSuccess()
96     {
97         return isInsertText_ && isDeleteForward_ && isDeleteBackward_ && isSendKeyEventFromInputMethod_ &&
98                isSendKeyboardInfo_ && isSetKeyboardStatus_ && isMoveCursor_ && isHandleSetSelection_ &&
99                isHandleExtendAction_ && isHandleSelect_;
100     }
101 
102 private:
103     bool isInsertText_ = false;
104     bool isDeleteForward_ = false;
105     bool isDeleteBackward_ = false;
106     bool isSendKeyEventFromInputMethod_ = false;
107     bool isSendKeyboardInfo_ = false;
108     bool isSetKeyboardStatus_ = false;
109     bool isMoveCursor_ = false;
110     bool isHandleSetSelection_ = false;
111     bool isHandleExtendAction_ = false;
112     bool isHandleSelect_ = false;
113 };
114 
SetUpTestCase(void)115 void NWebIMFAdapterTest::SetUpTestCase(void)
116 {
117     g_imf = std::make_unique<IMFAdapterImpl>();
118     ASSERT_NE(g_imf, nullptr);
119 }
120 
TearDownTestCase(void)121 void NWebIMFAdapterTest::TearDownTestCase(void) {}
122 
SetUp(void)123 void NWebIMFAdapterTest::SetUp(void) {}
124 
TearDown(void)125 void NWebIMFAdapterTest::TearDown(void) {}
126 
127 /**
128  * @tc.name: NWebIMFAdapterTest_IMFAdapterImpl_001.
129  * @tc.desc: IMF adapter unittest.
130  * @tc.type: FUNC.
131  * @tc.require:
132  */
133 HWTEST_F(NWebIMFAdapterTest, NWebIMFAdapterTest_IMFAdapterImpl_001, TestSize.Level1)
134 {
135     bool res = g_imf->Attach(nullptr, false);
136     EXPECT_EQ(g_imf->textListener_, nullptr);
137     EXPECT_FALSE(res);
138 }
139 
140 /**
141  * @tc.name: NWebIMFAdapterTest_IMFAdapterImpl_002.
142  * @tc.desc: IMF adapter unittest.
143  * @tc.type: FUNC.
144  * @tc.require:
145  */
146 HWTEST_F(NWebIMFAdapterTest, NWebIMFAdapterTest_IMFAdapterImpl_002, TestSize.Level1)
147 {
148     auto listener = std::make_shared<IMFTextListenerTest>();
149     bool res = g_imf->Attach(listener, true);
150     EXPECT_TRUE(res);
151     EXPECT_NE(g_imf->textListener_, nullptr);
152     g_imf->HideTextInput();
153 }
154 
155 /**
156  * @tc.name: NWebIMFAdapterTest_IMFAdapterImpl_003.
157  * @tc.desc: IMF adapter unittest.
158  * @tc.type: FUNC.
159  * @tc.require:
160  */
161 HWTEST_F(NWebIMFAdapterTest, NWebIMFAdapterTest_IMFAdapterImpl_003, TestSize.Level1)
162 {
163     auto listener = std::make_shared<IMFTextListenerTest>();
164     bool res = g_imf->Attach(listener, false);
165     EXPECT_TRUE(res);
166     EXPECT_NE(g_imf->textListener_, nullptr);
167     g_imf->ShowCurrentInput(IMFAdapterTextInputType::TEXT);
168     g_imf->ShowCurrentInput(IMFAdapterTextInputType::NUMBER);
169     g_imf->HideTextInput();
170 }
171 
172 /**
173  * @tc.name: NWebIMFAdapterTest_IMFAdapterImpl_004.
174  * @tc.desc: IMF adapter unittest.
175  * @tc.type: FUNC.
176  * @tc.require:
177  */
178 HWTEST_F(NWebIMFAdapterTest, NWebIMFAdapterTest_IMFAdapterImpl_004, TestSize.Level1)
179 {
180     IMFAdapterCursorInfo cursorInfo;
181     g_imf->OnCursorUpdate(cursorInfo);
182     std::u16string text;
183     g_imf->OnSelectionChange(text, 0, 0);
184     g_imf->Close();
185 }
186 
187 /**
188  * @tc.name: NWebIMFAdapterTest_IMFAdapterImpl_005.
189  * @tc.desc: IMF adapter unittest.
190  * @tc.type: FUNC.
191  * @tc.require:
192  */
193 HWTEST_F(NWebIMFAdapterTest, NWebIMFAdapterTest_IMFAdapterImpl_005, TestSize.Level1)
194 {
195     auto imf_adapter = OhosAdapterHelper::GetInstance().CreateMMIAdapter();
196     EXPECT_NE(imf_adapter, nullptr);
197     auto listener = std::make_shared<IMFTextListenerTest>();
198     auto listenerTest = std::make_shared<IMFTextListenerAdapterImpl>(listener);
199     std::u16string text;
200     MiscServices::KeyEvent event;
201     MiscServices::KeyboardInfo info;
202     listenerTest->InsertText(text);
203     listenerTest->DeleteForward(0);
204     listenerTest->DeleteBackward(0);
205     listenerTest->SendKeyEventFromInputMethod(event);
206     listenerTest->SendKeyboardInfo(info);
207     info.SetKeyboardStatus(static_cast<int32_t>(MiscServices::KeyboardStatus::SHOW));
208     listenerTest->SendKeyboardInfo(info);
209     info.SetKeyboardStatus(static_cast<int32_t>(MiscServices::KeyboardStatus::HIDE));
210     info.SetFunctionKey(static_cast<int32_t>(MiscServices::FunctionKey::CONFIRM));
211     listenerTest->SendKeyboardInfo(info);
212     listenerTest->SetKeyboardStatus(true);
213     listenerTest->MoveCursor(MiscServices::Direction::NONE);
214     listenerTest->MoveCursor(MiscServices::Direction::UP);
215     listenerTest->MoveCursor(MiscServices::Direction::DOWN);
216     listenerTest->MoveCursor(MiscServices::Direction::LEFT);
217     listenerTest->MoveCursor(MiscServices::Direction::RIGHT);
218     listenerTest->HandleSetSelection(0, 0);
219     listenerTest->HandleExtendAction(0);
220     listenerTest->HandleSelect(0, 0);
221     EXPECT_EQ(listener->VerifyAllSuccess(), true);
222 }
223 
224 /**
225  * @tc.name: NWebIMFAdapterTest_InsertText_006.
226  * @tc.desc: IMF adapter unittest.
227  * @tc.type: FUNC.
228  * @tc.require:
229  */
230 HWTEST_F(NWebIMFAdapterTest, NWebIMFAdapterTest_InsertText_006, TestSize.Level1)
231 {
232     auto listenerTest = std::make_shared<IMFTextListenerAdapterImpl>(nullptr);
233     EXPECT_NE(listenerTest, nullptr);
234     std::u16string text;
235     MiscServices::KeyEvent event;
236     MiscServices::KeyboardInfo info;
237     listenerTest->InsertText(text);
238     listenerTest->DeleteForward(0);
239     listenerTest->DeleteBackward(0);
240     listenerTest->SendKeyEventFromInputMethod(event);
241     listenerTest->SendKeyboardInfo(info);
242     listenerTest->SetKeyboardStatus(true);
243     listenerTest->MoveCursor(MiscServices::Direction::NONE);
244     listenerTest->HandleSetSelection(0, 0);
245     listenerTest->HandleExtendAction(0);
246     listenerTest->HandleSelect(0, 0);
247 }
248 } // namespace OHOS::NWeb