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 "nweb_input_event_consumer_test.h"
17 #include "nweb_test_log.h"
18
19 namespace OHOS::NWeb {
NWebInputEventConsumerTest(std::shared_ptr<NWeb> nweb)20 NWebInputEventConsumerTest::NWebInputEventConsumerTest(std::shared_ptr<NWeb> nweb)
21 : nwebweak_(nweb) {}
22
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const23 bool NWebInputEventConsumerTest::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
24 {
25 DispatchPointerEvent(pointerEvent);
26 pointerEvent->MarkProcessed();
27 return true;
28 }
29
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const30 bool NWebInputEventConsumerTest::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
31 {
32 DispatchKeyEvent(keyEvent);
33 keyEvent->MarkProcessed();
34 return true;
35 }
36
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const37 bool NWebInputEventConsumerTest::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
38 {
39 // do nothing
40 return false;
41 }
42
DispatchPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const43 void NWebInputEventConsumerTest::DispatchPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
44 {
45 if (nwebweak_.expired()) {
46 TESTLOG_W("nweb instance has expired");
47 return;
48 }
49 auto nweb = nwebweak_.lock();
50 if (pointerEvent == nullptr) {
51 TESTLOG_E("pointerEvent is invalid.");
52 return;
53 }
54 int32_t pointerID = pointerEvent->GetPointerId();
55 MMI::PointerEvent::PointerItem item;
56 bool ret = pointerEvent->GetPointerItem(pointerID, item);
57 if (!ret) {
58 TESTLOG_E("get pointer item failed.");
59 return;
60 }
61 int32_t action = pointerEvent->GetPointerAction();
62 switch (action) {
63 case MMI::PointerEvent::POINTER_ACTION_DOWN: {
64 TESTLOG_I("nweb receive pointer touch down event");
65 nweb->OnTouchPress(pointerID, item.GetWindowX(), item.GetWindowY());
66 break;
67 }
68 case MMI::PointerEvent::POINTER_ACTION_UP: {
69 TESTLOG_I("nweb receive pointer touch up event");
70 nweb->OnTouchRelease(pointerID);
71 break;
72 }
73 case MMI::PointerEvent::POINTER_ACTION_MOVE: {
74 TESTLOG_I("nweb receive pointer touch move event");
75 nweb->OnTouchMove(pointerID, item.GetWindowX(), item.GetWindowY());
76 break;
77 }
78 case MMI::PointerEvent::POINTER_ACTION_CANCEL: {
79 TESTLOG_I("nweb receive pointer touch cancel event");
80 nweb->OnTouchCancel();
81 break;
82 }
83 default: {
84 TESTLOG_W("unsupported PointerEvent action[%{public}d] received", action);
85 break;
86 }
87 }
88 }
89
DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const90 void NWebInputEventConsumerTest::DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
91 {
92 if (nwebweak_.expired()) {
93 TESTLOG_W("nweb instance has expired");
94 return;
95 }
96 auto nweb = nwebweak_.lock();
97 if (keyEvent == nullptr) {
98 TESTLOG_E("keyEvent is invalid.");
99 return;
100 }
101 int32_t keyCode = keyEvent->GetKeyCode();
102 if (keyCode == MMI::KeyEvent::KEYCODE_BACK) {
103 TESTLOG_I("nweb receive key back event");
104 nweb->OnNavigateBack();
105 } else {
106 TESTLOG_W("unsupported KeyEvent code[%{public}d] received", keyCode);
107 }
108 }
109 } // namespace OHOS::NWeb
110