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