1 /*
2 * Copyright (c) 2025 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 <cstdio>
17 #include <dlfcn.h>
18 #include <gtest/gtest.h>
19
20 #include <sys/epoll.h>
21 #include <sys/timerfd.h>
22
23 #include "accesstoken_kit.h"
24 #include "devicestatus_data_define.h"
25 #include "devicestatus_define.h"
26 #include "devicestatus_data_parse.h"
27 #include "devicestatus_msdp_mock.h"
28 #include "devicestatus_msdp_interface.h"
29 #include "devicestatus_msdp_mock.h"
30 #include "devicestatus_msdp_client_impl.h"
31 #include "sensor_data_callback.h"
32
33 #undef LOG_TAG
34 #define LOG_TAG "DeviceStatusMsdpAlgoMocKTest"
35
36 namespace OHOS {
37 namespace Msdp {
38 namespace DeviceStatus {
39 using namespace testing::ext;
40 namespace {
41 std::shared_ptr<DeviceStatusMsdpMock> g_testMock;
42
43 } // namespace
44
45 class DeviceStatusMsdpAlgoMocKTest : public testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp();
50 void TearDown();
51 protected:
52 DeviceStatusMsdpMock deviceStatusMsdpMock;
53 };
54
SetUpTestCase()55 void DeviceStatusMsdpAlgoMocKTest::SetUpTestCase()
56 {
57 g_testMock = std::make_shared<DeviceStatusMsdpMock>();
58 }
59
TearDownTestCase()60 void DeviceStatusMsdpAlgoMocKTest::TearDownTestCase()
61 {
62 g_testMock = nullptr;
63 }
64
SetUp()65 void DeviceStatusMsdpAlgoMocKTest::SetUp() {}
66
TearDown()67 void DeviceStatusMsdpAlgoMocKTest::TearDown() {}
68
69 /**
70 * @tc.name: DeviceStatusMsdpAlgoMocKTest001
71 * @tc.desc: test devicestatus Mock in Algorithm
72 * @tc.type: FUNC
73 */
74 HWTEST_F(DeviceStatusMsdpAlgoMocKTest, DeviceStatusMsdpAlgoMocKTest001, TestSize.Level1)
75 {
76 CALL_TEST_DEBUG;
77 EXPECT_TRUE(g_testMock->Init());
78 auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
79 EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
80 EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
81 }
82
83 /**
84 * @tc.name: DeviceStatusMsdpAlgoMocKTest002
85 * @tc.desc: test DisableCount
86 * @tc.type: FUNC
87 */
88 HWTEST_F(DeviceStatusMsdpAlgoMocKTest, DeviceStatusMsdpAlgoMocKTest002, TestSize.Level1)
89 {
90 CALL_TEST_DEBUG;
91 Type type = (Type)1;
92 ErrCode ret = g_testMock->DisableCount(type);
93 EXPECT_TRUE(ret == ERR_OK);
94 }
95
96 /**
97 * @tc.name: DeviceStatusMsdpAlgoMocKTest005
98 * @tc.desc: test SetTimerInterval
99 * @tc.type: FUNC
100 */
101 HWTEST_F(DeviceStatusMsdpAlgoMocKTest, DeviceStatusMsdpAlgoMocKTest005, TestSize.Level1)
102 {
103 CALL_TEST_DEBUG;
104 g_testMock->timerFd_ = -1;
105 int32_t interval = -1;
106 EXPECT_TRUE(g_testMock->SetTimerInterval(interval) == RET_ERR);
107 g_testMock->timerFd_ = 1;
108 EXPECT_TRUE(g_testMock->SetTimerInterval(interval) == RET_ERR);
109 }
110
111 /**
112 * @tc.name: DeviceStatusMsdpAlgoMocKTest005
113 * @tc.desc: test NotifyMsdpImpl
114 * @tc.type: FUNC
115 */
116 HWTEST_F(DeviceStatusMsdpAlgoMocKTest, DeviceStatusMsdpAlgoMocKTest006, TestSize.Level1)
117 {
118 CALL_TEST_DEBUG;
119 struct Data data;
120 EXPECT_TRUE(g_testMock->NotifyMsdpImpl(data) == RET_ERR);
121 }
122
123 /**
124 * @tc.name: DeviceStatusMsdpAlgoMocKTest007
125 * @tc.desc: test DisableCount
126 * @tc.type: FUNC
127 */
128 HWTEST_F(DeviceStatusMsdpAlgoMocKTest, DeviceStatusMsdpAlgoMocKTest007, TestSize.Level1)
129 {
130 CALL_TEST_DEBUG;
131 Type type = (Type)1;
132 g_testMock->dataParse_ = nullptr;
133 ErrCode ret = g_testMock->DisableCount(type);
134 EXPECT_TRUE(ret == RET_ERR);
135 }
136 } // namespace DeviceStatus
137 } // namespace Msdp
138 } // namespace OHOS