• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "ability_context_impl.h"
18 #include "mock_session.h"
19 #include "window_session_impl.h"
20 #include "mock_uicontent.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 
28 class MockWindowChangeListener : public IWindowChangeListener {
29 public:
30     MOCK_METHOD3(OnSizeChange, void(Rect rect, WindowSizeChangeReason reason,
31         const std::shared_ptr<RSTransaction>& rsTransaction));
32 };
33 
34 class MockWindowLifeCycleListener : public IWindowLifeCycle {
35 public:
36     MOCK_METHOD0(AfterForeground, void(void));
37     MOCK_METHOD0(AfterBackground, void(void));
38     MOCK_METHOD0(AfterFocused, void(void));
39     MOCK_METHOD0(AfterUnfocused, void(void));
40     MOCK_METHOD1(ForegroundFailed, void(int32_t));
41     MOCK_METHOD0(AfterActive, void(void));
42     MOCK_METHOD0(AfterInactive, void(void));
43 };
44 
45 class WindowSessionImplTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp() override;
50     void TearDown() override;
51 
52     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
53 };
54 
SetUpTestCase()55 void WindowSessionImplTest::SetUpTestCase()
56 {
57 }
58 
TearDownTestCase()59 void WindowSessionImplTest::TearDownTestCase()
60 {
61 }
62 
SetUp()63 void WindowSessionImplTest::SetUp()
64 {
65     abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
66 }
67 
TearDown()68 void WindowSessionImplTest::TearDown()
69 {
70     abilityContext_ = nullptr;
71 }
72 
73 namespace {
74 /**
75  * @tc.name: CreateWindowAndDestroy01
76  * @tc.desc: Create window and destroy window
77  * @tc.type: FUNC
78  */
79 HWTEST_F(WindowSessionImplTest, CreateWindowAndDestroy01, Function | SmallTest | Level2)
80 {
81     sptr<WindowOption> option = new WindowOption();
82     option->SetWindowName("CreateWindow01");
83     sptr<WindowSessionImpl> window = new WindowSessionImpl(option);
84 
85     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
86     sptr<SessionMocker> session = new(std::nothrow) SessionMocker(sessionInfo);
87     ASSERT_NE(nullptr, session);
88     ASSERT_EQ(WMError::WM_OK, window->Create(nullptr, session));
89     ASSERT_EQ(WMError::WM_OK, window->Create(abilityContext_, session));
90     ASSERT_EQ(WMError::WM_OK, window->Create(abilityContext_, session));
91     window->property_->SetPersistentId(1);
92     ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Destroy());
93     // session is null
94     window = new WindowSessionImpl(option);
95     ASSERT_EQ(WMError::WM_OK, window->Create(abilityContext_, nullptr));
96     ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Destroy());
97 }
98 
99 /**
100  * @tc.name: Connect01
101  * @tc.desc: Connect session
102  * @tc.type: FUNC
103  */
104 HWTEST_F(WindowSessionImplTest, Connect01, Function | SmallTest | Level2)
105 {
106     sptr<WindowOption> option = new WindowOption();
107     option->SetWindowName("Connect01");
108     sptr<WindowSessionImpl> window = new(std::nothrow) WindowSessionImpl(option);
109     ASSERT_NE(nullptr, window);
110     window->property_->SetPersistentId(1);
111     // connect with null session
112     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, window->Connect());
113 
114     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
115     sptr<SessionMocker> session = new(std::nothrow) SessionMocker(sessionInfo);
116     ASSERT_NE(nullptr, session);
117     window->hostSession_ = session;
118     EXPECT_CALL(*(session), Connect(_, _, _, _, _, _)).WillOnce(Return(WSError::WS_ERROR_NULLPTR));
119     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, window->Connect());
120     EXPECT_CALL(*(session), Connect(_, _, _, _, _, _)).WillOnce(Return(WSError::WS_OK));
121     ASSERT_EQ(WMError::WM_OK, window->Connect());
122     ASSERT_EQ(WMError::WM_OK, window->Destroy());
123 }
124 
125 /**
126  * @tc.name: Show01
127  * @tc.desc: Show session
128  * @tc.type: FUNC
129  */
130 HWTEST_F(WindowSessionImplTest, Show01, Function | SmallTest | Level2)
131 {
132     sptr<WindowOption> option = new WindowOption();
133     option->SetWindowName("Show01");
134     sptr<WindowSessionImpl> window = new(std::nothrow) WindowSessionImpl(option);
135     ASSERT_NE(nullptr, window);
136     window->property_->SetPersistentId(1);
137     // show with null session
138     ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Show());
139 
140     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
141     sptr<SessionMocker> session = new(std::nothrow) SessionMocker(sessionInfo);
142     ASSERT_NE(nullptr, session);
143     window->hostSession_ = session;
144     EXPECT_CALL(*(session), Foreground(_)).WillOnce(Return(WSError::WS_OK));
145     ASSERT_EQ(WMError::WM_OK, window->Show());
146     ASSERT_EQ(WMError::WM_OK, window->Show());
147     window->state_ = WindowState::STATE_CREATED;
148     EXPECT_CALL(*(session), Foreground(_)).WillOnce(Return(WSError::WS_ERROR_INVALID_SESSION));
149     ASSERT_EQ(WMError::WM_ERROR_INVALID_SESSION, window->Show());
150     ASSERT_EQ(WMError::WM_OK, window->Destroy());
151 }
152 
153 /**
154  * @tc.name: Hide01
155  * @tc.desc: Hide session
156  * @tc.type: FUNC
157  */
158 HWTEST_F(WindowSessionImplTest, Hide01, Function | SmallTest | Level2)
159 {
160     sptr<WindowOption> option = new WindowOption();
161     option->SetWindowName("Hide01");
162     sptr<WindowSessionImpl> window = new(std::nothrow) WindowSessionImpl(option);
163     ASSERT_NE(nullptr, window);
164     window->property_->SetPersistentId(1);
165     // show with null session
166     ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Hide());
167 
168     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
169     sptr<SessionMocker> session = new(std::nothrow) SessionMocker(sessionInfo);
170     ASSERT_NE(nullptr, session);
171     window->hostSession_ = session;
172     ASSERT_EQ(WMError::WM_OK, window->Hide());
173     ASSERT_EQ(WMError::WM_OK, window->Hide());
174     window->state_ = WindowState::STATE_CREATED;
175     ASSERT_EQ(WMError::WM_OK, window->Hide());
176     window->state_ = WindowState::STATE_SHOWN;
177     window->property_->type_ = WindowType::WINDOW_TYPE_FLOAT;
178     ASSERT_EQ(WMError::WM_OK, window->Hide());
179     window->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
180     ASSERT_EQ(WMError::WM_OK, window->Destroy());
181 }
182 
183 /**
184  * @tc.name: SetResizeByDragEnabled01
185  * @tc.desc: SetResizeByDragEnabled and check the retCode
186  * @tc.type: FUNC
187  */
188 HWTEST_F(WindowSessionImplTest, SetResizeByDragEnabled01, Function | SmallTest | Level2)
189 {
190     sptr<WindowOption> option = new WindowOption();
191     option->SetWindowName("SetResizeByDragEnabled01");
192     sptr<WindowSessionImpl> window = new(std::nothrow) WindowSessionImpl(option);
193     ASSERT_NE(nullptr, window);
194     WMError retCode = window->SetResizeByDragEnabled(true);
195     ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_WINDOW);
196     window->property_->SetPersistentId(1);
197     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
198     sptr<SessionMocker> session = new(std::nothrow) SessionMocker(sessionInfo);
199     ASSERT_NE(nullptr, session);
200     window->hostSession_ = session;
201     window->state_ = WindowState::STATE_CREATED;
202     retCode = window->SetResizeByDragEnabled(true);
203     ASSERT_EQ(retCode, WMError::WM_DO_NOTHING);
204 }
205 
206 /**
207  * @tc.name: SetRaiseByClickEnabled01
208  * @tc.desc: SetRaiseByClickEnabled and check the retCode
209  * @tc.type: FUNC
210  */
211 HWTEST_F(WindowSessionImplTest, SetRaiseByClickEnabled01, Function | SmallTest | Level2)
212 {
213     sptr<WindowOption> option = new WindowOption();
214     option->SetWindowName("SetRaiseByClickEnabled01");
215     sptr<WindowSessionImpl> window = new(std::nothrow) WindowSessionImpl(option);
216     ASSERT_NE(nullptr, window);
217     WMError retCode = window->SetRaiseByClickEnabled(true);
218     ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_WINDOW);
219     window->property_->SetPersistentId(1);
220     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
221     sptr<SessionMocker> session = new(std::nothrow) SessionMocker(sessionInfo);
222     ASSERT_NE(nullptr, session);
223     window->hostSession_ = session;
224     window->state_ = WindowState::STATE_CREATED;
225     retCode = window->SetRaiseByClickEnabled(true);
226     ASSERT_EQ(retCode, WMError::WM_DO_NOTHING);
227 }
228 }
229 } // namespace Rosen
230 } // namespace OHOS
231