• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <configuration.h>
17 #include <gtest/gtest.h>
18 
19 #include "ability_context_impl.h"
20 #include "mock_static_call.h"
21 #include "mock_session.h"
22 #include "modifier_render_thread/rs_modifiers_draw_thread.h"
23 #include "oh_window.h"
24 #include "singleton_mocker.h"
25 #include "window_impl.h"
26 #include "window_scene.h"
27 #include "window_session_impl.h"
28 
29 struct Input_TouchEvent {
30     int32_t action;
31     int32_t id;
32     int32_t displayX;
33     int32_t displayY;
34     int64_t actionTime { -1 };
35     int32_t windowId { -1 };
36     int32_t displayId { -1 };
37 };
38 
39 using namespace testing;
40 using namespace testing::ext;
41 
42 namespace OHOS {
43 namespace Rosen {
44 using Mocker = SingletonMocker<StaticCall, MockStaticCall>;
45 class OHWindowTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp() override;
50     void TearDown() override;
51 
52     sptr<WindowScene> scene_ = nullptr;
53     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
54 };
55 
SetUpTestCase()56 void OHWindowTest::SetUpTestCase()
57 {
58 }
59 
TearDownTestCase()60 void OHWindowTest::TearDownTestCase()
61 {
62 #ifdef RS_ENABLE_VK
63     RSModifiersDrawThread::Destroy();
64 #endif
65 }
66 
SetUp()67 void OHWindowTest::SetUp()
68 {
69     abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
70     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
71     sptr<WindowOption> option = new WindowOption();
72     EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(option)));
73     DisplayId displayId = 0;
74     sptr<IWindowLifeCycle> listener = nullptr;
75     scene_ = sptr<WindowScene>::MakeSptr();
76     ASSERT_EQ(WMError::WM_OK, scene_->Init(displayId, abilityContext_, listener));
77 }
78 
TearDown()79 void OHWindowTest::TearDown()
80 {
81     scene_->GoDestroy();
82     scene_ = nullptr;
83     abilityContext_ = nullptr;
84 }
85 
86 namespace {
87 /**
88  * @tc.name: ShowWindow01
89  * @tc.desc: return OK when show window
90  * @tc.type: FUNC
91  */
92 HWTEST_F(OHWindowTest, ShowWindow01, TestSize.Level1)
93 {
94     ASSERT_NE(nullptr, scene_);
95     ASSERT_NE(nullptr, scene_->GetMainWindow());
96     auto ret = OH_WindowManager_ShowWindow(scene_->GetMainWindow()->GetWindowId());
97     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::OK), ret);
98 }
99 
100 /**
101  * @tc.name: IsWindowShown01
102  * @tc.desc: return OK when window is shown
103  * @tc.type: FUNC
104  */
105 HWTEST_F(OHWindowTest, IsWindowShowing01, TestSize.Level1)
106 {
107     ASSERT_NE(nullptr, scene_);
108     ASSERT_NE(nullptr, scene_->GetMainWindow());
109     bool isShow;
110     auto ret = OH_WindowManager_IsWindowShown(scene_->GetMainWindow()->GetWindowId(), &isShow);
111     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::OK), ret);
112 }
113 
114 /**
115  * @tc.name: OH_WindowManager_GetAllWindowLayoutInfoList
116  * @tc.desc: OH_WindowManager_GetAllWindowLayoutInfoList test
117  * @tc.type: FUNC
118  */
119 HWTEST_F(OHWindowTest, OH_WindowManager_GetAllWindowLayoutInfoList, TestSize.Level0)
120 {
121     int64_t displayId = -1;
122     WindowManager_Rect** windowLayoutInfo = nullptr;
123     size_t* windowLayoutInfoSize = nullptr;
124     auto ret = OH_WindowManager_GetAllWindowLayoutInfoList(displayId, windowLayoutInfo, windowLayoutInfoSize);
125     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM), ret);
126     displayId = 0;
127     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM), ret);
128     windowLayoutInfo = (WindowManager_Rect**)malloc(sizeof(WindowManager_Rect**));
129     windowLayoutInfoSize = (size_t*)malloc(sizeof(size_t*));
130     ret = OH_WindowManager_GetAllWindowLayoutInfoList(displayId, windowLayoutInfo, windowLayoutInfoSize);
131     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::OK), ret);
132     OH_WindowManager_ReleaseAllWindowLayoutInfoList(*windowLayoutInfo);
133     *windowLayoutInfo = NULL;
134     free(windowLayoutInfo);
135     windowLayoutInfo = NULL;
136     free(windowLayoutInfoSize);
137     windowLayoutInfoSize = NULL;
138 }
139 
140 /**
141  * @tc.name: OH_WindowManager_InjectTouchEvent
142  * @tc.desc: OH_WindowManager_InjectTouchEvent test
143  * @tc.type: FUNC
144  */
145 HWTEST_F(OHWindowTest, OH_WindowManager_InjectTouchEvent, TestSize.Level0)
146 {
147     int32_t windowId = -1;
148     int32_t windowX = 0;
149     int32_t windowY = 0;
150     auto ret = OH_WindowManager_InjectTouchEvent(windowId, nullptr, windowX, windowY);
151     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM), ret);
152     Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
153     ASSERT_NE(nullptr, touchEvent);
154     touchEvent->actionTime = 100;
155     touchEvent->id = 1;
156     touchEvent->action = static_cast<Input_TouchEventAction>(10);
157     touchEvent->displayX = 100;
158     touchEvent->displayY = 200;
159     touchEvent->windowId = -1;
160     ret = OH_WindowManager_InjectTouchEvent(windowId, touchEvent, windowX, windowY);
161     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM), ret);
162     windowId = 1;
163     ret = OH_WindowManager_InjectTouchEvent(windowId, touchEvent, windowX, windowY);
164     EXPECT_EQ(OH_Input_GetTouchEventWindowId(touchEvent), windowId);
165     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::OK), ret);
166     OH_Input_SetTouchEventWindowId(touchEvent, 2);
167     ret = OH_WindowManager_InjectTouchEvent(windowId, touchEvent, windowX, windowY);
168     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM), ret);
169     OH_Input_SetTouchEventWindowId(touchEvent, windowId);
170     ret = OH_WindowManager_InjectTouchEvent(windowId, touchEvent, windowX, windowY);
171     EXPECT_EQ(static_cast<int32_t>(WindowManager_ErrorCode::OK), ret);
172     OH_Input_DestroyTouchEvent(&touchEvent);
173 }
174 }
175 } // namespace Rosen
176 } // namespace OHOS