1 /*
2  * Copyright (c) 2021-2022 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 // gtest
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include "window_test_utils.h"
20 #include "ability_context_impl.h"
21 #include "context_impl.h"
22 #include "iremote_object_mocker.h"
23 #include "mock_session.h"
24 #include "mock_window_adapter.h"
25 #include "window_session_impl.h"
26 #include "window_scene_session_impl.h"
27 #include "singleton_mocker.h"
28 
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace Rosen {
35 using Mocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
36 
37 using Utils = WindowTestUtils;
38 class WindowRecoverTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp() override;
43     void TearDown() override;
44 
45     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
46 };
47 
SetUpTestCase()48 void WindowRecoverTest::SetUpTestCase() {}
49 
TearDownTestCase()50 void WindowRecoverTest::TearDownTestCase() {}
51 
SetUp()52 void WindowRecoverTest::SetUp() {}
53 
TearDown()54 void WindowRecoverTest::TearDown() {}
55 
56 namespace {
57 /**
58  * @tc.name: RecoverAndReconnectSceneSession
59  * @tc.desc: RecoverAndReconnectSceneSession
60  * @tc.type: FUNC
61  */
HWTEST_F(WindowRecoverTest, RecoverAndReconnectSceneSession, Function | SmallTest | Level2)62 HWTEST_F(WindowRecoverTest, RecoverAndReconnectSceneSession, Function | SmallTest | Level2)
63 {
64     sptr<WindowOption> option = new (std::nothrow) WindowOption();
65     ASSERT_NE(nullptr, option);
66     option->SetWindowName("RecoverAndReconnectSceneSession");
67     sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
68     ASSERT_NE(nullptr, windowSceneSession);
69     windowSceneSession->property_->SetPersistentId(1112);
70     windowSceneSession->property_->SetParentId(1000);
71     windowSceneSession->property_->SetParentPersistentId(1000);
72     windowSceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
73 
74     std::shared_ptr<AbilityRuntime::AbilityContextImpl> context =
75         std::make_shared<AbilityRuntime::AbilityContextImpl>();
76     ASSERT_NE(nullptr, context);
77     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
78     sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
79     ASSERT_NE(nullptr, session);
80     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
81     context->SetAbilityInfo(abilityInfo);
82     sptr<IRemoteObject> sessionToken = new (std::nothrow) IRemoteObjectMocker();
83     ASSERT_NE(nullptr, sessionToken);
84     context->SetToken(sessionToken);
85     std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo = std::make_shared<AppExecFwk::ApplicationInfo>();
86     ASSERT_NE(nullptr, applicationInfo);
87     applicationInfo->apiCompatibleVersion = 12;
88     auto stageContent = std::make_shared<AbilityRuntime::ContextImpl>();
89     ASSERT_NE(nullptr, stageContent);
90     stageContent->SetApplicationInfo(applicationInfo);
91     stageContent->InitHapModuleInfo(abilityInfo);
92     context->stageContext_ = stageContent;
93 
94     struct RSSurfaceNodeConfig config;
95     std::shared_ptr<RSSurfaceNode> windowSceneSessionSurfaceNode = RSSurfaceNode::Create(config);
96     windowSceneSession->surfaceNode_ = windowSceneSessionSurfaceNode;
97     ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(context, session));
98 
99     std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
100     sptr<WindowSessionProperty> property = nullptr;
101     sptr<IRemoteObject> token = nullptr;
102     EXPECT_CALL(m->Mock(), RecoverAndReconnectSceneSession(_, _, _, _, _, _)).WillOnce(DoAll(
103         SaveArg<2>(&surfaceNode), SaveArg<4>(&property), SaveArg<5>(&token), Return(WMError::WM_OK)
104     ));
105     windowSceneSession->RecoverAndReconnectSceneSession();
106     ASSERT_EQ(surfaceNode, windowSceneSession->surfaceNode_);
107     ASSERT_EQ(property, windowSceneSession->property_);
108     ASSERT_EQ(token, sessionToken);
109     windowSceneSession->Destroy(false, false);
110 }
111 
112 /**
113  * @tc.name: RecoverAndConnectSpecificSession
114  * @tc.desc: RecoverAndConnectSpecificSession
115  * @tc.type: FUNC
116 */
HWTEST_F(WindowRecoverTest, RecoverAndConnectSpecificSession, Function | SmallTest | Level3)117 HWTEST_F(WindowRecoverTest, RecoverAndConnectSpecificSession, Function | SmallTest | Level3)
118 {
119     sptr<WindowOption> option = new (std::nothrow) WindowOption();
120     ASSERT_NE(nullptr, option);
121     option->SetWindowName("RecoverAndConnectSpecificSession");
122     sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
123     ASSERT_NE(nullptr, windowSceneSession);
124     windowSceneSession->property_->SetPersistentId(1112);
125     windowSceneSession->property_->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
126     windowSceneSession->property_->SetIsUIExtFirstSubWindow(true);
127 
128     std::shared_ptr<AbilityRuntime::AbilityContextImpl> context =
129         std::make_shared<AbilityRuntime::AbilityContextImpl>();
130     ASSERT_NE(nullptr, context);
131     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
132     sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
133     ASSERT_NE(nullptr, session);
134     sptr<IRemoteObject> sessionToken = new (std::nothrow) IRemoteObjectMocker();
135     ASSERT_NE(nullptr, sessionToken);
136     context->SetToken(sessionToken);
137     struct RSSurfaceNodeConfig config;
138     std::shared_ptr<RSSurfaceNode> windowSceneSessionSurfaceNode = RSSurfaceNode::Create(config);
139     ASSERT_NE(nullptr, windowSceneSessionSurfaceNode);
140     windowSceneSession->surfaceNode_ = windowSceneSessionSurfaceNode;
141     ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(context, session));
142 
143     std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
144     sptr<WindowSessionProperty> property = nullptr;
145     sptr<IRemoteObject> token = nullptr;
146     EXPECT_CALL(m->Mock(), RecoverAndConnectSpecificSession(_, _, _, _, _, _)).WillOnce(DoAll(
147         SaveArg<2>(&surfaceNode), SaveArg<3>(&property), SaveArg<5>(&token)
148     ));
149     windowSceneSession->RecoverAndConnectSpecificSession();
150     ASSERT_EQ(surfaceNode, windowSceneSession->surfaceNode_);
151     ASSERT_EQ(property, windowSceneSession->property_);
152     ASSERT_EQ(token, sessionToken);
153     windowSceneSession->Destroy(false, false);
154 }
155 } // namespace
156 } // namespace Rosen
157 } // namespace OHOS
158