• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "devicestatus_module_test.h"
17 
18 #include <chrono>
19 #include <iostream>
20 #include <thread>
21 
22 #include <gtest/gtest.h>
23 #include <if_system_ability_manager.h>
24 #include <ipc_skeleton.h>
25 #include <string_ex.h>
26 
27 #include "fi_log.h"
28 #include "stationary_manager.h"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 using namespace testing::ext;
34 namespace {
35 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceStatusModuleTest" };
36 } // namespace
37 
38 Type DeviceStatusModuleTest::g_moduleTest = Type::TYPE_INVALID;
39 
OnDeviceStatusChanged(const Data & devicestatusData)40 void DeviceStatusModuleTest::DeviceStatusModuleTestCallback::OnDeviceStatusChanged(const Data& devicestatusData)
41 {
42     GTEST_LOG_(INFO) << "DeviceStatusModuleTestCallback type: " << devicestatusData.type;
43     GTEST_LOG_(INFO) << "DeviceStatusModuleTestCallback value: " << devicestatusData.value;
44     EXPECT_EQ(true, devicestatusData.type == g_moduleTest &&
45         (devicestatusData.value >= OnChangedValue::VALUE_INVALID &&
46         devicestatusData.value <= OnChangedValue::VALUE_EXIT)) <<
47         "DeviceStatusModuleTestCallback failed";
48 }
49 
50 namespace {
51 /**
52  * @tc.name: DeviceStatusCallbackTest
53  * @tc.desc: test devicestatus callback in proxy
54  * @tc.type: FUNC
55  */
56 HWTEST_F(DeviceStatusModuleTest, DeviceStatusCallbackTest, TestSize.Level0)
57 {
58     g_moduleTest = Type::TYPE_ABSOLUTE_STILL;
59     Type type = g_moduleTest;
60     auto stationaryManager = StationaryManager::GetInstance();
61     sptr<IRemoteDevStaCallback> cb = new (std::nothrow) DeviceStatusModuleTestCallback();
62     EXPECT_FALSE(cb == nullptr);
63     GTEST_LOG_(INFO) << "Start register";
64     stationaryManager->SubscribeCallback(type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, cb);
65     GTEST_LOG_(INFO) << "Cancel register";
66     stationaryManager->UnsubscribeCallback(type, ActivityEvent::ENTER_EXIT, cb);
67 }
68 
69 /**
70  * @tc.name: GetDeviceStatusDataTest
71  * @tc.desc: test get devicestatus data in proxy
72  * @tc.type: FUNC
73  */
74 HWTEST_F(DeviceStatusModuleTest, GetDeviceStatusDataTest001, TestSize.Level0)
75 {
76     CALL_TEST_DEBUG;
77     g_moduleTest = Type::TYPE_HORIZONTAL_POSITION;
78     Type type = g_moduleTest;
79     auto stationaryManager = StationaryManager::GetInstance();
80     Data data = stationaryManager->GetDeviceStatusData(type);
81     GTEST_LOG_(INFO) << "type: " << data.type;
82     GTEST_LOG_(INFO) << "value: " << data.value;
83     EXPECT_EQ(true, data.type == g_moduleTest &&
84         data.value >= OnChangedValue::VALUE_INVALID &&
85         data.value <= OnChangedValue::VALUE_EXIT) << "GetDeviceStatusData failed";
86 }
87 
88 /**
89  * @tc.name: GetDeviceStatusDataTest
90  * @tc.desc: test get devicestatus data in proxy
91  * @tc.type: FUNC
92  */
93 HWTEST_F(DeviceStatusModuleTest, GetDeviceStatusDataTest002, TestSize.Level0)
94 {
95     CALL_TEST_DEBUG;
96     g_moduleTest = Type::TYPE_ABSOLUTE_STILL;
97     Type type = g_moduleTest;
98     auto stationaryManager = StationaryManager::GetInstance();
99     Data data = stationaryManager->GetDeviceStatusData(type);
100     GTEST_LOG_(INFO) << "type: " << data.type;
101     GTEST_LOG_(INFO) << "value: " << data.value;
102     EXPECT_EQ(true, data.type == g_moduleTest &&
103         data.value >= OnChangedValue::VALUE_INVALID &&
104         data.value <= OnChangedValue::VALUE_EXIT) << "GetDeviceStatusData failed";
105 }
106 
107 /**
108  * @tc.name: GetDeviceStatusDataTest
109  * @tc.desc: test get devicestatus data in proxy
110  * @tc.type: FUNC
111  */
112 HWTEST_F(DeviceStatusModuleTest, GetDeviceStatusDataTest003, TestSize.Level0)
113 {
114     CALL_TEST_DEBUG;
115     g_moduleTest = Type::TYPE_VERTICAL_POSITION;
116     Type type = g_moduleTest;
117     auto stationaryManager = StationaryManager::GetInstance();
118     Data data = stationaryManager->GetDeviceStatusData(type);
119     GTEST_LOG_(INFO) << "type: " << data.type;
120     GTEST_LOG_(INFO) << "value: " << data.value;
121     EXPECT_EQ(true, data.type == g_moduleTest &&
122         data.value >= OnChangedValue::VALUE_INVALID &&
123         data.value <= OnChangedValue::VALUE_EXIT) << "GetDeviceStatusData failed";
124 }
125 } // namespace
126 } // namespace DeviceStatus
127 } // namespace Msdp
128 } // namespace OHOS
129