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 <cstring>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include <ui/rs_surface_node.h>
20 #include <unordered_map>
21
22 #include "key_event.h"
23 #include "nweb_create_window.h"
24
25 #define private public
26 #include "nweb_input_event_consumer.h"
27 #include "nweb_helper.h"
28 #undef private
29
30 #include "nweb.h"
31 #include "nweb_adapter_helper.h"
32 #include "pointer_event.h"
33 #include "window.h"
34
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace OHOS::MMI;
39
40 namespace OHOS::NWeb {
41 namespace {
42 std::shared_ptr<NWeb> g_nweb;
43 std::shared_ptr<NWebInputEventConsumer> g_input;
44 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
45 } // namespace
46
47 class NWebInputEventTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53 };
54
SetUpTestCase(void)55 void NWebInputEventTest::SetUpTestCase(void)
56 {
57 NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
58 bool result = NWebAdapterHelper::Instance().Init(false);
59 EXPECT_TRUE(result);
60 }
61
TearDownTestCase(void)62 void NWebInputEventTest::TearDownTestCase(void)
63 {}
64
SetUp(void)65 void NWebInputEventTest::SetUp(void)
66 {}
67
TearDown(void)68 void NWebInputEventTest::TearDown(void)
69 {}
70
71 /**
72 * @tc.name: NWebInputEvent_NWebInputEventConsumer_001.
73 * @tc.desc: Test the NWebInputEventConsumer.
74 * @tc.type: FUNC
75 * @tc.require:issueI5NXG9
76 */
77 HWTEST_F(NWebInputEventTest, NWebInputEvent_NWebInputEventConsumer_001, TestSize.Level1)
78 {
79 g_input = std::make_shared<NWebInputEventConsumer>(g_nweb);
80 EXPECT_NE(g_input, nullptr);
81 }
82
83 /**
84 * @tc.name: NWebInputEvent_OnInputEvent_002.
85 * @tc.desc: Test the OnInputEvent.
86 * @tc.type: FUNC
87 * @tc.require:issueI5OURV
88 */
89 HWTEST_F(NWebInputEventTest, NWebInputEvent_OnInputEvent_002, TestSize.Level1)
90 {
91 const int32_t POINTER_EVENT = 11;
92 const int32_t POINTERID = 1;
93 bool result;
94 PointerEvent::PointerItem pointerItem;
95 int32_t keyCode = MMI::KeyEvent::KEYCODE_BACK;
96 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
97 EXPECT_NE(keyEvent, nullptr);
98 std::shared_ptr<MMI::PointerEvent> event = MMI::PointerEvent::Create();
99 EXPECT_NE(event, nullptr);
100 result = g_input->OnInputEvent(event);
101 EXPECT_TRUE(result);
102 result = g_input->OnInputEvent(keyEvent);
103 EXPECT_TRUE(result);
104
105 std::shared_ptr<NWeb> mock = std::make_shared<NWebMock>();
106 EXPECT_NE(mock, nullptr);
107 std::shared_ptr<NWebInputEventConsumer> input = std::make_shared<NWebInputEventConsumer>(mock);
108 EXPECT_NE(input, nullptr);
109 result = input->OnInputEvent(event);
110 EXPECT_TRUE(result);
111 pointerItem.SetPointerId(POINTERID);
112 event->SetPointerId(POINTERID);
113 event->AddPointerItem(pointerItem);
114 for (int32_t i = 0; i <= POINTER_EVENT; i++) {
115 event->SetPointerAction(i);
116 result = g_input->OnInputEvent(event);
117 EXPECT_TRUE(result);
118 }
119
120 result = input->OnInputEvent(keyEvent);
121 EXPECT_TRUE(result);
122 std::shared_ptr<MMI::AxisEvent> axisevent = MMI::AxisEvent::Create();
123 result = g_input->OnInputEvent(axisevent);
124 EXPECT_FALSE(result);
125 keyEvent->SetKeyCode(keyCode);
126 result = input->OnInputEvent(keyEvent);
127 EXPECT_TRUE(result);
128
129 keyEvent = nullptr;
130 event = nullptr;
131 input->DispatchKeyEvent(keyEvent);
132 input->DispatchPointerEvent(event);
133 result = input->OnInputEvent(event);
134 EXPECT_FALSE(result);
135 result = input->OnInputEvent(keyEvent);
136 EXPECT_FALSE(result);
137 }
138
139 /**
140 * @tc.name: NWebInputEvent_CreateNWeb_003.
141 * @tc.desc: Test the CreateNWeb.
142 * @tc.type: FUNC
143 * @tc.require:issueI5NXG9
144 */
145 HWTEST_F(NWebInputEventTest, NWebInputEvent_CreateNWeb_003, TestSize.Level1)
146 {
147 sptr<OHOS::Rosen::Window> window = CreateWindow();
148 EXPECT_NE(window, nullptr);
149 NWebHelper::Instance().libHandleWebEngine_ = nullptr;
150 g_nweb = NWebAdapterHelper::Instance().CreateNWeb(window.GetRefPtr(), GetInitArgs());
151 EXPECT_EQ(g_nweb, nullptr);
152 }
153 }
154