1 /*
2 * Copyright (c) 2022 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 <array>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include "ohos_adapter_helper.h"
21 #include "mmi_adapter.h"
22 #include "key_event.h"
23
24 #define private public
25 #include "mmi_adapter_impl.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS::NWeb {
31 namespace {
32 const int RESULT_OK = 0;
33 const int RESULT_ERROR = -1;
34 std::shared_ptr<MMIAdapterImpl> g_mmi;
35 } // namespace
36
37 class NWebMMIAdapterTest : public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 };
44
45 class MMIListenerTest : public MMIListenerAdapter {
46 public:
47 MMIListenerTest() = default;
48 virtual ~MMIListenerTest() = default;
OnDeviceAdded(int32_t deviceId,const std::string & type)49 void OnDeviceAdded(int32_t deviceId, const std::string &type) override {};
OnDeviceRemoved(int32_t deviceId,const std::string & type)50 void OnDeviceRemoved(int32_t deviceId, const std::string &type) override {};
51 };
52
SetUpTestCase(void)53 void NWebMMIAdapterTest::SetUpTestCase(void)
54 {
55 g_mmi = std::make_shared<MMIAdapterImpl>();
56 ASSERT_NE(g_mmi, nullptr);
57 }
58
TearDownTestCase(void)59 void NWebMMIAdapterTest::TearDownTestCase(void) {}
60
SetUp(void)61 void NWebMMIAdapterTest::SetUp(void) {}
62
TearDown(void)63 void NWebMMIAdapterTest::TearDown(void) {}
64
65 /**
66 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_001.
67 * @tc.desc: MMI adapter unittest.
68 * @tc.type: FUNC.
69 * @tc.require:I5P001
70 */
71 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIAdapterImpl_001, TestSize.Level1)
72 {
73 auto listener = std::make_shared<MMIListenerTest>();
74 int32_t ret = g_mmi->RegisterDevListener("change", listener);
75 EXPECT_EQ(ret, RESULT_OK);
76
77 ret = g_mmi->UnregisterDevListener("change");
78 EXPECT_EQ(ret, RESULT_OK);
79 }
80
81 /**
82 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_002.
83 * @tc.desc: MMI adapter unittest.
84 * @tc.type: FUNC.
85 * @tc.require:I5P001
86 */
87 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIAdapterImpl_002, TestSize.Level1)
88 {
__anon7d2ca7bd0202(int32_t type) 89 auto callback = [](int32_t type) {};
90 int32_t ret = g_mmi->GetKeyboardType(1, callback);
91 EXPECT_EQ(ret, RESULT_OK);
92 }
93
94 /**
95 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_003.
96 * @tc.desc: MMI adapter unittest.
97 * @tc.type: FUNC.
98 * @tc.require:I5P001
99 */
100 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIAdapterImpl_003, TestSize.Level1)
101 {
__anon7d2ca7bd0302(std::vector<int32_t>& devList) 102 auto callback = [](std::vector<int32_t>& devList) {};
103 int32_t ret = g_mmi->GetDeviceIds(callback);
104 EXPECT_EQ(ret, RESULT_OK);
105 }
106
107 /**
108 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_004.
109 * @tc.desc: MMI adapter unittest.
110 * @tc.type: FUNC.
111 * @tc.require:I5OZZ8
112 */
113 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIAdapterImpl_004, TestSize.Level1)
114 {
115 auto listener = std::make_shared<MMIListenerTest>();
116 auto listenerTest = std::make_shared<MMIListenerAdapterImpl>(listener);
117 listenerTest->OnDeviceAdded(1, "add");
118 listenerTest->OnDeviceRemoved(1, "remove");
119 }
120
121 /**
122 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_005.
123 * @tc.desc: MMI adapter unittest.
124 * @tc.type: FUNC.
125 * @tc.require:I5OZZ8
126 */
127 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIAdapterImpl_005, TestSize.Level1)
128 {
129 auto listener = std::make_shared<MMIListenerTest>();
130 int32_t ret = g_mmi->RegisterDevListener("change", nullptr);
131 EXPECT_EQ(ret, RESULT_ERROR);
132 }
133
134 /**
135 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_006.
136 * @tc.desc: MMI adapter unittest.
137 * @tc.type: FUNC.
138 * @tc.require:I5OZZ8
139 */
140 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIAdapterImpl_006, TestSize.Level1)
141 {
142 auto mmi_adapter = OhosAdapterHelper::GetInstance().CreateMMIAdapter();
143 EXPECT_NE(mmi_adapter, nullptr);
144 auto listener = std::make_shared<MMIListenerTest>();
145 auto listenerTest = std::make_shared<MMIListenerAdapterImpl>(listener);
146 listenerTest->listener_ = nullptr;
147 listenerTest->OnDeviceAdded(1, "add");
148 listenerTest->OnDeviceRemoved(1, "remove");
149
150 const char *code = g_mmi->KeyCodeToString(MMI::KeyEvent::KEYCODE_UNKNOWN);
151 EXPECT_NE(code, nullptr);
152 int32_t result = g_mmi->RegisterMMIInputListener(nullptr);
153 EXPECT_EQ(result, -1);
__anon7d2ca7bd0402(int32_t, int32_t) 154 InputEventCallback eventCallback = [](int32_t, int32_t) {};
155 result = g_mmi->RegisterMMIInputListener(std::move(eventCallback));
156 EXPECT_NE(result, -1);
157 g_mmi->UnregisterMMIInputListener(MMI::KeyEvent::KEYCODE_UNKNOWN);
158 }
159
160 /**
161 * @tc.name: NWebMMIAdapterTest_MMIAdapterImpl_007.
162 * @tc.desc: MMIInputListenerAdapterImpl.
163 * @tc.type: FUNC.
164 * @tc.require:I5OZZ8
165 */
166 HWTEST_F(NWebMMIAdapterTest, NWebMMIAdapterTest_MMIInputListenerAdapterImpl_007, TestSize.Level1)
167 {
__anon7d2ca7bd0502(int32_t, int32_t) 168 InputEventCallback listener = [](int32_t, int32_t) {};
169 MMIInputListenerAdapterImpl listenerAdapter(listener);
170 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
171 std::shared_ptr<MMI::PointerEvent> pointerEvent = nullptr;
172 std::shared_ptr<MMI::AxisEvent> axisEvent = nullptr;
173 keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
174 listenerAdapter.OnInputEvent(keyEvent);
175 listenerAdapter.OnInputEvent(pointerEvent);
176 listenerAdapter.OnInputEvent(axisEvent);
177
178 keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
179 listenerAdapter.OnInputEvent(keyEvent);
180 keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_CANCEL);
181 listenerAdapter.OnInputEvent(keyEvent);
182
183 MMIInputListenerAdapterImpl listenerAdapterImpl(nullptr);
184 listenerAdapterImpl.OnInputEvent(keyEvent);
185 }
186 } // namespace OHOS::NWeb