1 /*
2 * Copyright (c) 2021-2024 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 "running_lock_hub_native_test.h"
17
18 #include <fcntl.h>
19 #include <ipc_skeleton.h>
20
21 #include <datetime_ex.h>
22 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
23 #include <input_manager.h>
24 #endif
25 #include <securec.h>
26
27 #include "actions/irunning_lock_action.h"
28 #include "running_lock_hub.h"
29 #include "suspend_controller.h"
30 #include "power_log.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS;
35 using namespace std;
36
SetUpTestCase()37 void RunningLockHubNativeTest::SetUpTestCase()
38 {
39 }
40
41 namespace {
RunningSuspendCallback()42 void RunningSuspendCallback()
43 {
44 }
45
46 /**
47 * @tc.name: RunningLockNative001
48 * @tc.desc: test Acquire and Release inrunningLockMgr
49 * @tc.type: FUNC
50 */
51 HWTEST_F(RunningLockHubNativeTest, RunningLockNative001, TestSize.Level1)
52 {
53 GTEST_LOG_(INFO) << "RunningLockNative001 start";
54 POWER_HILOGI(LABEL_TEST, "RunningLockNative001 function start!");
55 auto sc = std::make_shared<Suspend::SuspendController>();
56 auto runningLockHub = std::make_shared<Suspend::RunningLockHub>(sc);
57 runningLockHub->Acquire("777");
58 EXPECT_FALSE(runningLockHub->runningLockMap_.empty());
59 runningLockHub->Release("777");
60 runningLockHub->lockFd_ = static_cast<UniqueFd>(UNFD);
61 runningLockHub->Acquire("777");
62 runningLockHub->Release("777");
63 runningLockHub->Release("333");
64 EXPECT_TRUE(runningLockHub->runningLockMap_.empty());
65 POWER_HILOGI(LABEL_TEST, "RunningLockNative001 function end!");
66 GTEST_LOG_(INFO) << "RunningLockNative001 end";
67 }
68
69 /**
70 * @tc.name: RunningLockNative002
71 * @tc.desc: test Suspend inrunningLockMgr
72 * @tc.type: FUNC
73 */
74 HWTEST_F(RunningLockHubNativeTest, RunningLockNative002, TestSize.Level1)
75 {
76 GTEST_LOG_(INFO) << "RunningLockNative002 start";
77 POWER_HILOGI(LABEL_TEST, "RunningLockNative002 function start!");
78 auto SuspendController = std::make_shared<Suspend::SuspendController>();
79 SuspendController->Wakeup();
80 SuspendController->Suspend(RunningSuspendCallback, RunningSuspendCallback, false);
81 SuspendController->Suspend(RunningSuspendCallback, RunningSuspendCallback, false);
82 EXPECT_FALSE(SuspendController->suspend_->WriteWakeupCount("77"));
83 SuspendController->suspend_->wakeupCountFd =
84 UniqueFd(TEMP_FAILURE_RETRY(open(Suspend::RunningLockHub::LOCK_PATH, O_RDWR | O_CLOEXEC)));
85 EXPECT_TRUE(SuspendController->suspend_->WriteWakeupCount("77"));
86 SuspendController->IncSuspendBlockCounter();
87 SuspendController->IncSuspendBlockCounter();
88 SuspendController->DecSuspendBlockCounter();
89 SuspendController->DecSuspendBlockCounter();
90 POWER_HILOGI(LABEL_TEST, "RunningLockNative002 function end!");
91 GTEST_LOG_(INFO) << "RunningLockNative002 end";
92 }
93 } // namespace