• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "device_state_action_native_test.h"
17 
18 #include <ipc_skeleton.h>
19 
20 #include "actions/irunning_lock_action.h"
21 #include "display_power_mgr_client.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS::PowerMgr;
25 using namespace OHOS;
26 using namespace std;
27 
SetUpTestCase()28 void DeviceStateActionNativeTest::SetUpTestCase()
29 {
30 }
31 
32 namespace {
33 using SuspendCallback1 = std::function<void(uint32_t)>;
34 
DeviceStateActionCallback(uint32_t trigger)35 void DeviceStateActionCallback(uint32_t trigger)
36 {
37 }
38 
39 /**
40  * @tc.name: DeviceStateActionNative001
41  * @tc.desc: test init in deviceStateAction
42  * @tc.type: FUNC
43  */
44 HWTEST_F (DeviceStateActionNativeTest, DeviceStateActionNative001, TestSize.Level0)
45 {
46     POWER_HILOGI(LABEL_TEST, "DeviceStateActionNative001::fun is start!");
47     auto deviceStateAction = std::make_shared<DeviceStateAction>();
48     SuspendCallback1 sd =  DeviceStateActionCallback;
49     deviceStateAction->RegisterCallback(sd);
50     DisplayState state = DisplayState::DISPLAY_OFF;
51     StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_INIT;
52     EXPECT_TRUE(deviceStateAction->SetDisplayState(state, reason) == ActionResult::SUCCESS);
53     EXPECT_TRUE(deviceStateAction->GetDisplayState() == DisplayState::DISPLAY_OFF);
54     state = DisplayState::DISPLAY_DIM;
55     EXPECT_TRUE(deviceStateAction->SetDisplayState(state, reason) == ActionResult::SUCCESS);
56     EXPECT_TRUE(deviceStateAction->GetDisplayState() == DisplayState::DISPLAY_DIM);
57     state = DisplayState::DISPLAY_ON;
58     EXPECT_TRUE(deviceStateAction->SetDisplayState(state, reason) == ActionResult::SUCCESS);
59     EXPECT_TRUE(deviceStateAction->GetDisplayState() == DisplayState::DISPLAY_ON);
60     state = DisplayState::DISPLAY_SUSPEND;
61     EXPECT_TRUE(deviceStateAction->SetDisplayState(state, reason) == ActionResult::SUCCESS);
62     EXPECT_TRUE(deviceStateAction->GetDisplayState() == DisplayState::DISPLAY_SUSPEND);
63     state = DisplayState::DISPLAY_UNKNOWN;
64     EXPECT_TRUE(deviceStateAction->SetDisplayState(state, reason) == ActionResult::FAILED);
65     EXPECT_FALSE(deviceStateAction->GetDisplayState() == DisplayState::DISPLAY_UNKNOWN);
66 
67     DisplayPowerMgr::DisplayState stateType = DisplayPowerMgr::DisplayState::DISPLAY_ON;
68     deviceStateAction->dispCallback_->OnDisplayStateChanged(
69         DISPLAYID, stateType, static_cast<uint32_t>(StateChangeReason::STATE_CHANGE_REASON_APPLICATION));
70     deviceStateAction->dispCallback_->OnDisplayStateChanged(
71         DISPLAYID_A, stateType, static_cast<uint32_t>(StateChangeReason::STATE_CHANGE_REASON_APPLICATION));
72     stateType = DisplayPowerMgr::DisplayState::DISPLAY_OFF;
73     deviceStateAction->dispCallback_->OnDisplayStateChanged(
74         DISPLAYID_A, stateType, static_cast<uint32_t>(StateChangeReason::STATE_CHANGE_REASON_APPLICATION));
75     stateType = DisplayPowerMgr::DisplayState::DISPLAY_UNKNOWN;
76     deviceStateAction->dispCallback_->OnDisplayStateChanged(
77         DISPLAYID_A, stateType, static_cast<uint32_t>(StateChangeReason::STATE_CHANGE_REASON_APPLICATION));
78     stateType = static_cast<DisplayPowerMgr::DisplayState>(UNDISPLAYSTATE);
79     EXPECT_TRUE(DisplayPowerMgr::DisplayPowerMgrClient::GetInstance().SetDisplayState(stateType,
80                                                                         reason) == ActionResult::FAILED);
81     EXPECT_TRUE(deviceStateAction->GetDisplayState() == DisplayState::DISPLAY_UNKNOWN);
82     deviceStateAction->dispCallback_->notify_ = nullptr;
83     deviceStateAction->dispCallback_->NotifyDisplayActionDone(DISPLAYID);
84     auto& powerMgrClient = PowerMgrClient::GetInstance();
85     powerMgrClient.SuspendDevice();
86     powerMgrClient.WakeupDevice();
87 
88     POWER_HILOGI(LABEL_TEST, "DeviceStateActionNative001::fun is end!");
89     GTEST_LOG_(INFO) << "DeviceStateActionNative001: Suspend Device end.";
90 }
91 }
92