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