1 /*
2 * Copyright (c) 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 <gtest/gtest.h>
17 #include <common_event_manager.h>
18
19 #include "screen_session_manager/include/publish/screen_session_publish.h"
20 #include "window_manager_hilog.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr uint32_t SLEEP_TIME_US = 100000;
29 const std::string CAST_PLUG_IN_FLAG_DATA = "1";
30 const std::string CAST_PLUG_OUT_FLAG_DATA = "0";
31 const std::string COMMON_EVENT_DISPLAY_ROTATION_CHANGED = "custom.event.display_rotation_changed";
32 const std::string COMMON_EVENT_CAST_PLUGGED_CHANGED = "custom.event.cast_plugged_changed";
33 const std::string COMMON_EVENT_SMART_NOTIFICATION = "hicare.event.SMART_NOTIFICATION";
34 const std::string COMMON_EVENT_LOW_TEMP_WARNING = "usual.event.thermal.LOW_TEMP_WARNING";
35 constexpr int32_t PUBLISH_FAILURE = -1;
36 const std::string FAULT_DESCRIPTION = "842003014";
37 const std::string FAULT_SUGGESTION = "542003014";
38 }
39
40 class ScreenSessionPublishTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp() override;
45 void TearDown() override;
46 };
47
SetUpTestCase()48 void ScreenSessionPublishTest::SetUpTestCase()
49 {
50 }
51
TearDownTestCase()52 void ScreenSessionPublishTest::TearDownTestCase()
53 {
54 }
55
SetUp()56 void ScreenSessionPublishTest::SetUp()
57 {
58 }
59
TearDown()60 void ScreenSessionPublishTest::TearDown()
61 {
62 usleep(SLEEP_TIME_US);
63 }
64
65 namespace {
66
67 /**
68 * @tc.name: InitPublishEvents
69 * @tc.desc: InitPublishEvents
70 * @tc.type: FUNC
71 */
72 HWTEST_F(ScreenSessionPublishTest, InitPublishEvents, TestSize.Level1)
73 {
74 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
75 screenSessionPublish.InitPublishEvents();
76 ASSERT_NE(screenSessionPublish.publishInfo_, nullptr);
77
78 screenSessionPublish.publishInfo_ = nullptr;
79 screenSessionPublish.cesWantMap_.clear();
80 std::string event = "test";
81 sptr<EventFwk::Want> want = nullptr;
82 screenSessionPublish.cesWantMap_.insert(std::make_pair(event, want));
83 screenSessionPublish.InitPublishEvents();
84 ASSERT_NE(screenSessionPublish.publishInfo_, nullptr);
85 ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 1);
86 }
87
88 /**
89 * @tc.name: PublishEvents
90 * @tc.desc: PublishEvents
91 * @tc.type: FUNC
92 */
93 HWTEST_F(ScreenSessionPublishTest, PublishEvents, TestSize.Level1)
94 {
95 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
96 EventFwk::CommonEventData eventData;
97 std::string bundleName = "test";
98 int32_t ret = screenSessionPublish.PublishEvents(eventData, bundleName);
99 ASSERT_EQ(ret, PUBLISH_FAILURE);
100
101 screenSessionPublish.InitPublishEvents();
102 ret = screenSessionPublish.PublishEvents(eventData, bundleName);
103 ASSERT_EQ(ret, PUBLISH_FAILURE);
104
105 bundleName = "";
106 ret = screenSessionPublish.PublishEvents(eventData, bundleName);
107 ASSERT_EQ(ret, PUBLISH_FAILURE);
108 }
109
110 /**
111 * @tc.name: PublishCastPluggedEvent
112 * @tc.desc: PublishCastPluggedEvent
113 * @tc.type: FUNC
114 */
115 HWTEST_F(ScreenSessionPublishTest, PublishCastPluggedEvent, TestSize.Level1)
116 {
117 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
118 bool isEnable = false;
119 screenSessionPublish.PublishCastPluggedEvent(isEnable);
120 ASSERT_FALSE(isEnable);
121
122 isEnable = true;
123 sptr<EventFwk::Want> want = new (std::nothrow) EventFwk::Want();
124 screenSessionPublish.cesWantMap_.insert(std::make_pair(COMMON_EVENT_CAST_PLUGGED_CHANGED, want));
125 screenSessionPublish.PublishCastPluggedEvent(isEnable);
126 ASSERT_TRUE(isEnable);
127 }
128
129 /**
130 * @tc.name: PublishDisplayRotationEvent
131 * @tc.desc: PublishDisplayRotationEvent
132 * @tc.type: FUNC
133 */
134 HWTEST_F(ScreenSessionPublishTest, PublishDisplayRotationEvent, TestSize.Level1)
135 {
136 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
137 screenSessionPublish.cesWantMap_.clear();
138 ScreenId screenId = 1;
139 Rotation displayRotation = Rotation::ROTATION_0;
140 screenSessionPublish.PublishDisplayRotationEvent(screenId, displayRotation);
141 ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 1);
142
143 screenSessionPublish.cesWantMap_.clear();
144 sptr<EventFwk::Want> want = new (std::nothrow) EventFwk::Want();
145 screenSessionPublish.cesWantMap_.insert(std::make_pair(COMMON_EVENT_DISPLAY_ROTATION_CHANGED, want));
146 screenSessionPublish.PublishDisplayRotationEvent(screenId, displayRotation);
147 ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 2);
148 }
149
150 /**
151 * @tc.name: PublishSmartNotificationEvent
152 * @tc.desc: PublishSmartNotificationEvent
153 * @tc.type: FUNC
154 */
155 HWTEST_F(ScreenSessionPublishTest, PublishSmartNotificationEvent, TestSize.Level1)
156 {
157 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
158 screenSessionPublish.cesWantMap_.clear();
159 screenSessionPublish.PublishSmartNotificationEvent(FAULT_DESCRIPTION, FAULT_SUGGESTION);
160 ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 1);
161
162 screenSessionPublish.cesWantMap_.clear();
163 sptr<EventFwk::Want> want = new (std::nothrow) EventFwk::Want();
164 screenSessionPublish.cesWantMap_.insert(std::make_pair(COMMON_EVENT_SMART_NOTIFICATION, want));
165 screenSessionPublish.PublishSmartNotificationEvent(FAULT_DESCRIPTION, FAULT_SUGGESTION);
166 ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 1);
167 }
168
169 /**
170 * @tc.name: RegisterLowTempSubscriber
171 * @tc.desc: RegisterLowTempSubscriber
172 * @tc.type: FUNC
173 */
174 HWTEST_F(ScreenSessionPublishTest, RegisterLowTempSubscriber, TestSize.Level1)
175 {
176 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
177 screenSessionPublish.subscriber_ = nullptr;
178 ASSERT_EQ(screenSessionPublish.RegisterLowTempSubscriber(), true);
179 ASSERT_EQ(screenSessionPublish.RegisterLowTempSubscriber(), false);
180 }
181
182 /**
183 * @tc.name: UnRegisterLowTempSubscriber
184 * @tc.desc: UnRegisterLowTempSubscriber
185 * @tc.type: FUNC
186 */
187 HWTEST_F(ScreenSessionPublishTest, UnRegisterLowTempSubscriber, TestSize.Level1)
188 {
189 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
190 screenSessionPublish.RegisterLowTempSubscriber();
191 ASSERT_EQ(screenSessionPublish.UnRegisterLowTempSubscriber(), true);
192 screenSessionPublish.subscriber_ = nullptr;
193 ASSERT_EQ(screenSessionPublish.UnRegisterLowTempSubscriber(), false);
194 }
195
196 /**
197 * @tc.name: RegisterUserSwitchedSubscriber
198 * @tc.desc: RegisterUserSwitchedSubscriber
199 * @tc.type: FUNC
200 */
201 HWTEST_F(ScreenSessionPublishTest, RegisterUserSwitchedSubscriber, TestSize.Level1)
202 {
203 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
204 screenSessionPublish.userSwitchedSubscriber_ = nullptr;
205 ASSERT_EQ(screenSessionPublish.RegisterUserSwitchedSubscriber(), true);
206 ASSERT_EQ(screenSessionPublish.RegisterUserSwitchedSubscriber(), false);
207 }
208
209 /**
210 * @tc.name: UnRegisterUserSwitchedSubscriber
211 * @tc.desc: UnRegisterUserSwitchedSubscriber
212 * @tc.type: FUNC
213 */
214 HWTEST_F(ScreenSessionPublishTest, UnRegisterUserSwitchedSubscriber, TestSize.Level1)
215 {
216 auto screenSessionPublish = ScreenSessionPublish::GetInstance();
217 screenSessionPublish.RegisterUserSwitchedSubscriber();
218 ASSERT_EQ(screenSessionPublish.UnRegisterUserSwitchedSubscriber(), true);
219 screenSessionPublish.userSwitchedSubscriber_ = nullptr;
220 ASSERT_EQ(screenSessionPublish.UnRegisterUserSwitchedSubscriber(), false);
221 }
222 }
223 } // namespace Rosen
224 } // namespace OHOS