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 <gtest/gtest.h>
17
18 #include "mouse_device_state.h"
19
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 using namespace testing::ext;
24 }
25 class MouseDeviceStateTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 };
32
SetUpTestCase(void)33 void MouseDeviceStateTest::SetUpTestCase(void)
34 {
35 }
36
TearDownTestCase(void)37 void MouseDeviceStateTest::TearDownTestCase(void)
38 {
39 }
40
SetUp()41 void MouseDeviceStateTest::SetUp()
42 {
43 }
44
TearDown()45 void MouseDeviceStateTest::TearDown()
46 {
47 }
48
49 /**
50 * @tc.name: MouseDeviceStateTest_GetMouseCoordsX_001
51 * @tc.desc: Test GetMouseCoordsX
52 * @tc.type: FUNC
53 * @tc.require:
54 */
55 HWTEST_F(MouseDeviceStateTest, MouseDeviceStateTest_GetMouseCoordsX_001, TestSize.Level1)
56 {
57 int32_t idNames = 0;
58 ASSERT_EQ(MouseState->GetMouseCoordsX(), idNames);
59 }
60
61 /**
62 * @tc.name: MouseDeviceStateTest_IsLeftBtnPressed_002
63 * @tc.desc: Test IsLeftBtnPressed
64 * @tc.type: FUNC
65 * @tc.require:
66 */
67 HWTEST_F(MouseDeviceStateTest, MouseDeviceStateTest_IsLeftBtnPressed_002, TestSize.Level1)
68 {
69 int32_t x = 0;
70 int32_t y = 0;
71 MouseState->SetMouseCoords(x, y);
72 bool isPress = false;
73 ASSERT_EQ(MouseState->IsLeftBtnPressed(), isPress);
74 MouseState->MouseBtnStateCounts(MouseDeviceState::LIBINPUT_LEFT_BUTTON_CODE, BUTTON_STATE_PRESSED);
75 isPress = true;
76 ASSERT_EQ(MouseState->IsLeftBtnPressed(), isPress);
77 }
78
79 /**
80 * @tc.name: MouseDeviceStateTest_GetPressedButtons_003
81 * @tc.desc: Test GetPressedButtons
82 * @tc.type: FUNC
83 * @tc.require:
84 */
85 HWTEST_F(MouseDeviceStateTest, MouseDeviceStateTest_GetPressedButtons_003, TestSize.Level1)
86 {
87 MouseState->MouseBtnStateCounts(MouseDeviceState::LIBINPUT_LEFT_BUTTON_CODE, BUTTON_STATE_PRESSED);
88 std::vector<int32_t> pressedButtons;
89 MouseState->GetPressedButtons(pressedButtons);
90 std::vector<int32_t> idNames = {PointerEvent::MOUSE_BUTTON_LEFT};
91 ASSERT_EQ(pressedButtons, idNames);
92 }
93
94 /**
95 * @tc.name: MouseDeviceStateTest_LibinputChangeToPointer_004
96 * @tc.desc: Test LibinputChangeToPointer
97 * @tc.type: FUNC
98 * @tc.require:
99 */
100 HWTEST_F(MouseDeviceStateTest, MouseDeviceStateTest_LibinputChangeToPointer_004, TestSize.Level1)
101 {
102 const uint32_t keyValue = MouseDeviceState::LIBINPUT_LEFT_BUTTON_CODE;
103 int32_t idNames = PointerEvent::MOUSE_BUTTON_LEFT;
104 ASSERT_EQ(MouseState->LibinputChangeToPointer(keyValue), idNames);
105 }
106
107 /**
108 * @tc.name: MouseDeviceStateTest_ChangeMouseState
109 * @tc.desc: Test ChangeMouseState
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113 HWTEST_F(MouseDeviceStateTest, MouseDeviceStateTest_ChangeMouseState, TestSize.Level1)
114 {
115 int32_t btnStateCount = 1;
116 MouseState->ChangeMouseState(BUTTON_STATE_PRESSED, btnStateCount);
117 EXPECT_EQ(btnStateCount, 2);
118 btnStateCount = 2;
119 MouseState->ChangeMouseState(BUTTON_STATE_RELEASED, btnStateCount);
120 EXPECT_EQ(btnStateCount, 1);
121 btnStateCount = 10;
122 MouseState->ChangeMouseState(BUTTON_STATE_PRESSED, btnStateCount);
123 EXPECT_EQ(btnStateCount, 8);
124 btnStateCount = -1;
125 MouseState->ChangeMouseState(BUTTON_STATE_RELEASED, btnStateCount);
126 EXPECT_EQ(btnStateCount, 0);
127 }
128 }
129 }