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