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