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 "session/host/include/main_session.h"
18 #include "mock/mock_session_stage.h"
19 #include "window_event_channel_base.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 class MainSessionLifecycleTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 SessionInfo info;
33 sptr<MainSession::SpecificSessionCallback> specificCallback = nullptr;
34 sptr<MainSession> mainSession_;
35
36 private:
37 RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
38 };
39
SetUpTestCase()40 void MainSessionLifecycleTest::SetUpTestCase() {}
41
TearDownTestCase()42 void MainSessionLifecycleTest::TearDownTestCase() {}
43
SetUp()44 void MainSessionLifecycleTest::SetUp()
45 {
46 SessionInfo info;
47 info.abilityName_ = "testMainSession1";
48 info.moduleName_ = "testMainSession2";
49 info.bundleName_ = "testMainSession3";
50 mainSession_ = sptr<MainSession>::MakeSptr(info, specificCallback);
51 EXPECT_NE(nullptr, mainSession_);
52 }
53
TearDown()54 void MainSessionLifecycleTest::TearDown()
55 {
56 mainSession_ = nullptr;
57 }
58
CreateRSSurfaceNode()59 RSSurfaceNode::SharedPtr MainSessionLifecycleTest::CreateRSSurfaceNode()
60 {
61 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
62 rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
63 auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
64 return surfaceNode;
65 }
66
67 namespace {
68
69 /**
70 * @tc.name: Reconnect01
71 * @tc.desc: check func Reconnect
72 * @tc.type: FUNC
73 */
74 HWTEST_F(MainSessionLifecycleTest, Reconnect01, TestSize.Level1)
75 {
76 auto surfaceNode = CreateRSSurfaceNode();
77 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
78 ASSERT_NE(nullptr, property);
79 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
80 EXPECT_NE(nullptr, mockSessionStage);
81 sptr<TestWindowEventChannel> testWindowEventChannel = sptr<TestWindowEventChannel>::MakeSptr();
82 EXPECT_NE(nullptr, testWindowEventChannel);
83
84 auto result = mainSession_->Reconnect(nullptr, nullptr, nullptr, property);
85 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
86
87 result = mainSession_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
88 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
89
90 result = mainSession_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
91 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
92
93 result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
94 ASSERT_EQ(result, WSError::WS_OK);
95
96 property->SetWindowState(WindowState::STATE_HIDDEN);
97 result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
98 ASSERT_EQ(result, WSError::WS_OK);
99
100 property->SetWindowState(WindowState::STATE_SHOWN);
101 result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
102 ASSERT_EQ(result, WSError::WS_OK);
103
104 mainSession_->pcFoldScreenController_ = nullptr;
105 result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
106 ASSERT_EQ(result, WSError::WS_OK);
107 }
108
109 /**
110 * @tc.name: NotifyForegroundInteractiveStatus01
111 * @tc.desc: check func NotifyForegroundInteractiveStatus
112 * @tc.type: FUNC
113 */
114 HWTEST_F(MainSessionLifecycleTest, NotifyForegroundInteractiveStatus01, TestSize.Level1)
115 {
116 mainSession_->isVisible_ = true;
117 mainSession_->SetSessionState(SessionState::STATE_DISCONNECT);
118 mainSession_->NotifyForegroundInteractiveStatus(true);
119 mainSession_->NotifyForegroundInteractiveStatus(false);
120 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
121
122 mainSession_->isVisible_ = false;
123 mainSession_->SetSessionState(SessionState::STATE_ACTIVE);
124 mainSession_->NotifyForegroundInteractiveStatus(true);
125 mainSession_->NotifyForegroundInteractiveStatus(false);
126 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
127
128 mainSession_->isVisible_ = false;
129 mainSession_->SetSessionState(SessionState::STATE_FOREGROUND);
130 mainSession_->NotifyForegroundInteractiveStatus(true);
131 mainSession_->NotifyForegroundInteractiveStatus(false);
132 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
133 }
134
135 /**
136 * @tc.name: NotifyForegroundInteractiveStatus02
137 * @tc.desc: check func NotifyForegroundInteractiveStatus
138 * @tc.type: FUNC
139 */
140 HWTEST_F(MainSessionLifecycleTest, NotifyForegroundInteractiveStatus02, TestSize.Level1)
141 {
142 auto surfaceNode = CreateRSSurfaceNode();
143 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
144 ASSERT_NE(nullptr, property);
145 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
146 EXPECT_NE(nullptr, mockSessionStage);
147 sptr<TestWindowEventChannel> testWindowEventChannel = sptr<TestWindowEventChannel>::MakeSptr();
148 EXPECT_NE(nullptr, testWindowEventChannel);
149
150 mainSession_->SetSessionState(SessionState::STATE_CONNECT);
151 WSError reconnect = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
152 ASSERT_EQ(reconnect, WSError::WS_OK);
153
154 mainSession_->isVisible_ = false;
155 mainSession_->NotifyForegroundInteractiveStatus(true);
156 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
157
158 mainSession_->isVisible_ = true;
159 mainSession_->NotifyForegroundInteractiveStatus(true);
160 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
161
162 mainSession_->SetSessionState(SessionState::STATE_ACTIVE);
163 mainSession_->NotifyForegroundInteractiveStatus(true);
164 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
165
166 mainSession_->SetSessionState(SessionState::STATE_FOREGROUND);
167 mainSession_->NotifyForegroundInteractiveStatus(true);
168 ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
169 }
170 } // namespace
171 } // namespace Rosen
172 } // namespace OHOS