• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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     constexpr int32_t PUBLISH_FAILURE = -1;
34 }
35 
36 class ScreenSessionPublishTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42 };
43 
SetUpTestCase()44 void ScreenSessionPublishTest::SetUpTestCase()
45 {
46 }
47 
TearDownTestCase()48 void ScreenSessionPublishTest::TearDownTestCase()
49 {
50 }
51 
SetUp()52 void ScreenSessionPublishTest::SetUp()
53 {
54 }
55 
TearDown()56 void ScreenSessionPublishTest::TearDown()
57 {
58     usleep(SLEEP_TIME_US);
59 }
60 
61 namespace {
62 
63     /**
64      * @tc.name: InitPublishEvents
65      * @tc.desc: InitPublishEvents
66      * @tc.type: FUNC
67      */
68     HWTEST_F(ScreenSessionPublishTest, InitPublishEvents, Function | SmallTest | Level3)
69     {
70         auto screenSessionPublish = ScreenSessionPublish::GetInstance();
71         screenSessionPublish.InitPublishEvents();
72         ASSERT_NE(screenSessionPublish.publishInfo_, nullptr);
73 
74         screenSessionPublish.publishInfo_ = nullptr;
75         screenSessionPublish.cesWantMap_.clear();
76         std::string event = "test";
77         sptr<EventFwk::Want> want = nullptr;
78         screenSessionPublish.cesWantMap_.insert(std::make_pair(event, want));
79         screenSessionPublish.InitPublishEvents();
80         ASSERT_NE(screenSessionPublish.publishInfo_, nullptr);
81         ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 1);
82     }
83 
84     /**
85      * @tc.name: PublishEvents
86      * @tc.desc: PublishEvents
87      * @tc.type: FUNC
88      */
89     HWTEST_F(ScreenSessionPublishTest, PublishEvents, Function | SmallTest | Level3)
90     {
91         auto screenSessionPublish = ScreenSessionPublish::GetInstance();
92         EventFwk::CommonEventData eventData;
93         std::string bundleName = "test";
94         int32_t ret = screenSessionPublish.PublishEvents(eventData, bundleName);
95         ASSERT_EQ(ret, PUBLISH_FAILURE);
96 
97         screenSessionPublish.InitPublishEvents();
98         ret = screenSessionPublish.PublishEvents(eventData, bundleName);
99         ASSERT_EQ(ret, PUBLISH_FAILURE);
100 
101         bundleName = "";
102         ret = screenSessionPublish.PublishEvents(eventData, bundleName);
103         ASSERT_EQ(ret, PUBLISH_FAILURE);
104     }
105 
106     /**
107      * @tc.name: PublishCastPluggedEvent
108      * @tc.desc: PublishCastPluggedEvent
109      * @tc.type: FUNC
110      */
111     HWTEST_F(ScreenSessionPublishTest, PublishCastPluggedEvent, Function | SmallTest | Level3)
112     {
113         auto screenSessionPublish = ScreenSessionPublish::GetInstance();
114         bool isEnable = false;
115         screenSessionPublish.PublishCastPluggedEvent(isEnable);
116         ASSERT_FALSE(isEnable);
117 
118         isEnable = true;
119         sptr<EventFwk::Want> want = new (std::nothrow) EventFwk::Want();
120         screenSessionPublish.cesWantMap_.insert(std::make_pair(COMMON_EVENT_CAST_PLUGGED_CHANGED, want));
121         screenSessionPublish.PublishCastPluggedEvent(isEnable);
122         ASSERT_TRUE(isEnable);
123     }
124 
125     /**
126      * @tc.name: PublishDisplayRotationEvent
127      * @tc.desc: PublishDisplayRotationEvent
128      * @tc.type: FUNC
129      */
130     HWTEST_F(ScreenSessionPublishTest, PublishDisplayRotationEvent, Function | SmallTest | Level3)
131     {
132         auto screenSessionPublish = ScreenSessionPublish::GetInstance();
133         screenSessionPublish.cesWantMap_.clear();
134         ScreenId screenId = 1;
135         Rotation displayRotation = Rotation::ROTATION_0;
136         screenSessionPublish.PublishDisplayRotationEvent(screenId, displayRotation);
137         ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 1);
138 
139         screenSessionPublish.cesWantMap_.clear();
140         sptr<EventFwk::Want> want = new (std::nothrow) EventFwk::Want();
141         screenSessionPublish.cesWantMap_.insert(std::make_pair(COMMON_EVENT_DISPLAY_ROTATION_CHANGED, want));
142         screenSessionPublish.PublishDisplayRotationEvent(screenId, displayRotation);
143         ASSERT_EQ(screenSessionPublish.cesWantMap_.size(), 2);
144     }
145 }
146 } // namespace Rosen
147 } // namespace OHOS