• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_agent_test.h"
17 
18 #include "devicestatus_common.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::Msdp;
22 using namespace OHOS;
23 using namespace std;
24 
25 static std::shared_ptr<DeviceStatusAgent> agent1_;
26 static std::shared_ptr<DeviceStatusAgent> agent2_;
27 
SetUpTestCase()28 void DevicestatusAgentTest::SetUpTestCase()
29 {
30 }
31 
TearDownTestCase()32 void DevicestatusAgentTest::TearDownTestCase()
33 {
34 }
35 
SetUp()36 void DevicestatusAgentTest::SetUp()
37 {
38     agent1_ = std::make_shared<DeviceStatusAgent>();
39     agent2_ = std::make_shared<DeviceStatusAgent>();
40 }
41 
TearDown()42 void DevicestatusAgentTest::TearDown()
43 {
44 }
45 
OnEventResult(const DevicestatusDataUtils::DevicestatusData & devicestatusData)46 bool DevicestatusAgentListenerMockFirstClient::OnEventResult(
47     const DevicestatusDataUtils::DevicestatusData& devicestatusData)
48 {
49     GTEST_LOG_(INFO) << "agent type: " << devicestatusData.type;
50     GTEST_LOG_(INFO) << "agent value: " << devicestatusData.value;
51     EXPECT_EQ(true, devicestatusData.type == DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
52     return true;
53 }
54 
OnEventResult(const DevicestatusDataUtils::DevicestatusData & devicestatusData)55 bool DevicestatusAgentListenerMockSecondClient::OnEventResult(
56     const DevicestatusDataUtils::DevicestatusData& devicestatusData)
57 {
58     GTEST_LOG_(INFO) << "agent type: " << devicestatusData.type;
59     GTEST_LOG_(INFO) << "agent value: " << devicestatusData.value;
60     EXPECT_EQ(true, devicestatusData.type == DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
61     return true;
62 }
63 
64 namespace {
65 /**
66  * @tc.name: DevicestatusAgentTest001
67  * @tc.desc: test subscribing lid open event
68  * @tc.type: FUNC
69  */
70 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest001, TestSize.Level1)
71 {
72     GTEST_LOG_(INFO) << "DevicestatusAgentTest001 start";
73     std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
74         std::make_shared<DevicestatusAgentListenerMockFirstClient>();
75     int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
76     EXPECT_EQ(true, ret == ERR_OK);
77     GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
78     sleep(10);
79     agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
80     GTEST_LOG_(INFO) << "DevicestatusAgentTest001 end";
81 }
82 
83 /**
84  * @tc.name: DevicestatusAgentTest002
85  * @tc.desc: test subscribing lid open event repeatedly
86  * @tc.type: FUNC
87  */
88 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest002, TestSize.Level1)
89 {
90     GTEST_LOG_(INFO) << "DevicestatusAgentTest002 start";
91     std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
92         std::make_shared<DevicestatusAgentListenerMockFirstClient>();
93     int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
94     EXPECT_EQ(true, ret == ERR_OK);
95     GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
96     sleep(5);
97     ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
98     EXPECT_EQ(true, ret == ERR_OK);
99     GTEST_LOG_(INFO) << "Open and close the lid, and event will not report";
100     sleep(5);
101     ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
102     EXPECT_EQ(true, ret == ERR_OK);
103     GTEST_LOG_(INFO) << "Open and close the lid, and event will report again";
104     sleep(5);
105     agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
106     GTEST_LOG_(INFO) << "DevicestatusAgentTest002 end";
107 }
108 
109 /**
110  * @tc.name: DevicestatusAgentTest003
111  * @tc.desc: test subscribing lid open event for 2 client
112  * @tc.type: FUNC
113  */
114 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest003, TestSize.Level1)
115 {
116     GTEST_LOG_(INFO) << "DevicestatusAgentTest003 start";
117     std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent1 =
118         std::make_shared<DevicestatusAgentListenerMockFirstClient>();
119     std::shared_ptr<DevicestatusAgentListenerMockSecondClient> agentEvent2 =
120         std::make_shared<DevicestatusAgentListenerMockSecondClient>();
121     int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent1);
122     EXPECT_EQ(true, ret == ERR_OK);
123     ret = agent2_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent2);
124     EXPECT_EQ(true, ret == ERR_OK);
125     GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
126     sleep(5);
127     GTEST_LOG_(INFO) << "UnSubscribe agentEvent1";
128     agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
129     sleep(5);
130     GTEST_LOG_(INFO) << "UnSubscribe agentEvent2";
131     agent2_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
132     GTEST_LOG_(INFO) << "DevicestatusAgentTest003 end";
133 }
134 }