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_ = new (std::nothrow) MainSession(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 = new (std::nothrow) WindowSessionProperty();
81     ASSERT_NE(nullptr, property);
82     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
83     EXPECT_NE(nullptr, mockSessionStage);
84     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
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  * @tc.name: NotifyForegroundInteractiveStatus02
135  * @tc.desc: check func NotifyForegroundInteractiveStatus
136  * @tc.type: FUNC
137  */
138 HWTEST_F(MainSessionLifecycleTest, NotifyForegroundInteractiveStatus02, Function | SmallTest | Level2)
139 {
140     auto surfaceNode = CreateRSSurfaceNode();
141     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
142     ASSERT_NE(nullptr, property);
143     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
144     EXPECT_NE(nullptr, mockSessionStage);
145     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
146     EXPECT_NE(nullptr, testWindowEventChannel);
147 
148     mainSession_->SetSessionState(SessionState::STATE_CONNECT);
149     WSError reconnect = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
150     ASSERT_EQ(reconnect, WSError::WS_OK);
151 
152     mainSession_->isVisible_ = false;
153     mainSession_->NotifyForegroundInteractiveStatus(true);
154     ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
155 
156     mainSession_->isVisible_ = true;
157     mainSession_->NotifyForegroundInteractiveStatus(true);
158     ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
159 
160     mainSession_->SetSessionState(SessionState::STATE_ACTIVE);
161     mainSession_->NotifyForegroundInteractiveStatus(true);
162     ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
163 
164     mainSession_->SetSessionState(SessionState::STATE_FOREGROUND);
165     mainSession_->NotifyForegroundInteractiveStatus(true);
166     ASSERT_EQ(WSError::WS_OK, mainSession_->SetFocusable(false));
167 }
168 }
169 }
170 }