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 "wm_common.h"
19 #include "window_test_utils.h"
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowOccupiedAreaChangeTest"};
27 }
28
29 using utils = WindowTestUtils;
30 const int WAIT_ASYNC_US = 100000; // 100000us
31
32 class TestOccupiedAreaChangeListener : public IOccupiedAreaChangeListener {
33 public:
34 OccupiedAreaType type_ = OccupiedAreaType::TYPE_INPUT;
35 Rect rect_ = { 0, 0, 0, 0 };
36 void OnSizeChange(const sptr<OccupiedAreaChangeInfo>& info) override;
37 };
38
39 class WindowOccupiedAreaChangeTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 virtual void SetUp() override;
44 virtual void TearDown() override;
45 static sptr<TestOccupiedAreaChangeListener> testOccupiedAreaChangeListener_;
46 utils::TestWindowInfo fullScreenAppInfo_;
47 utils::TestWindowInfo imeAppInfo_;
48 };
49
50 sptr<TestOccupiedAreaChangeListener> WindowOccupiedAreaChangeTest::testOccupiedAreaChangeListener_ =
51 new TestOccupiedAreaChangeListener();
52
OnSizeChange(const sptr<OccupiedAreaChangeInfo> & info)53 void TestOccupiedAreaChangeListener::OnSizeChange(const sptr<OccupiedAreaChangeInfo>& info)
54 {
55 WLOGFI("OccupiedAreaChangeInfo: [%{public}u, {%{public}u, %{public}u}]",
56 info->type_, info->rect_.width_, info->rect_.height_);
57 type_ = info->type_;
58 rect_ = info->rect_;
59 }
60
SetUpTestCase()61 void WindowOccupiedAreaChangeTest::SetUpTestCase()
62 {
63 auto display = DisplayManager::GetInstance().GetDisplayById(0);
64 ASSERT_TRUE((display != nullptr));
65 WLOGFI("GetDefaultDisplay: id %{public}" PRIu64", w %{public}d, h %{public}d, fps %{public}u",
66 display->GetId(), display->GetWidth(), display->GetHeight(), display->GetRefreshRate());
67 Rect displayRect = {0, 0, display->GetWidth(), display->GetHeight()};
68 utils::InitByDisplayRect(displayRect);
69 }
70
TearDownTestCase()71 void WindowOccupiedAreaChangeTest::TearDownTestCase()
72 {
73 }
74
SetUp()75 void WindowOccupiedAreaChangeTest::SetUp()
76 {
77 fullScreenAppInfo_ = {
78 .name = "FullWindow",
79 .rect = utils::customAppRect_,
80 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
81 .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
82 .needAvoid = false,
83 .parentLimit = false,
84 .parentName = "",
85 };
86 imeAppInfo_ = {
87 .name = "ImeWindow",
88 .rect = utils::customAppRect_,
89 .type = WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT,
90 .mode = WindowMode::WINDOW_MODE_FLOATING,
91 };
92 }
93
TearDown()94 void WindowOccupiedAreaChangeTest::TearDown()
95 {
96 }
97
98 namespace {
99 /**
100 * @tc.name: KeyboardHeightChangeTest01
101 * @tc.desc: test get keyboard height when WINDOW_TYPE_INPUT_METHOD_FLOAT show
102 * @tc.type: FUNC
103 */
104 HWTEST_F(WindowOccupiedAreaChangeTest, KeyboardHeightChangeTest01, Function | MediumTest | Level3)
105 {
106 fullScreenAppInfo_.name = "KeyboardHeightChangeTest01";
107 const sptr<Window>& window1 = utils::CreateTestWindow(fullScreenAppInfo_);
108 ASSERT_NE(nullptr, window1);
109 window1->RegisterOccupiedAreaChangeListener(testOccupiedAreaChangeListener_);
110
111 imeAppInfo_.name = "imeWindow1";
112 imeAppInfo_.rect = { 10, 200, 300, 400 };
113 const sptr<Window>& window2 = utils::CreateTestWindow(imeAppInfo_);
114 ASSERT_NE(nullptr, window2);
115
116 ASSERT_EQ(WMError::WM_OK, window1->Show());
117 ASSERT_EQ(WMError::WM_OK, window2->SetCallingWindow(window1->GetWindowId()));
118 ASSERT_EQ(WMError::WM_OK, window2->Show());
119 // Await 100ms and get callback result in listener.
120 usleep(WAIT_ASYNC_US);
121 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.posX_, window2->GetRect().posX_);
122 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.posY_, window2->GetRect().posY_);
123 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.width_, window2->GetRect().width_);
124 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.height_, window2->GetRect().height_);
125 }
126
127 /**
128 * @tc.name: KeyboardHeightChangeTest02
129 * @tc.desc: test get keyboard height when WINDOW_TYPE_INPUT_METHOD_FLOAT hide
130 * @tc.type: FUNC
131 */
132 HWTEST_F(WindowOccupiedAreaChangeTest, KeyboardHeightChangeTest02, Function | MediumTest | Level3)
133 {
134 fullScreenAppInfo_.name = "KeyboardHeightChangeTest02";
135 const sptr<Window>& window1 = utils::CreateTestWindow(fullScreenAppInfo_);
136 ASSERT_NE(nullptr, window1);
137 window1->RegisterOccupiedAreaChangeListener(testOccupiedAreaChangeListener_);
138
139 imeAppInfo_.name = "imeWindow2";
140 imeAppInfo_.rect = { 10, 200, 300, 400 };
141 const sptr<Window>& window2 = utils::CreateTestWindow(imeAppInfo_);
142 ASSERT_NE(nullptr, window2);
143
144 ASSERT_EQ(WMError::WM_OK, window1->Show());
145 ASSERT_EQ(WMError::WM_OK, window2->SetCallingWindow(window1->GetWindowId()));
146 ASSERT_EQ(WMError::WM_OK, window2->Show());
147 ASSERT_EQ(WMError::WM_OK, window2->Hide());
148 // Await 100ms and get callback result in listener.
149 usleep(WAIT_ASYNC_US);
150 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.posX_, 0);
151 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.posY_, 0);
152 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.width_, 0);
153 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.height_, 0);
154 }
155
156 /**
157 * @tc.name: KeyboardHeightChangeTest03
158 * @tc.desc: test get keyboard height when WINDOW_TYPE_INPUT_METHOD_FLOAT destroy
159 * @tc.type: FUNC
160 */
161 HWTEST_F(WindowOccupiedAreaChangeTest, KeyboardHeightChangeTest03, Function | MediumTest | Level3)
162 {
163 fullScreenAppInfo_.name = "KeyboardHeightChangeTest03";
164 const sptr<Window>& window1 = utils::CreateTestWindow(fullScreenAppInfo_);
165 ASSERT_NE(nullptr, window1);
166 window1->RegisterOccupiedAreaChangeListener(testOccupiedAreaChangeListener_);
167
168 imeAppInfo_.name = "imeWindow3";
169 imeAppInfo_.rect = { 10, 200, 300, 400 };
170 const sptr<Window>& window2 = utils::CreateTestWindow(imeAppInfo_);
171 ASSERT_NE(nullptr, window2);
172
173 ASSERT_EQ(WMError::WM_OK, window1->Show());
174 ASSERT_EQ(WMError::WM_OK, window2->SetCallingWindow(window1->GetWindowId()));
175 ASSERT_EQ(WMError::WM_OK, window2->Show());
176 ASSERT_EQ(WMError::WM_OK, window2->Destroy());
177 // Await 100ms and get callback result in listener.
178 usleep(WAIT_ASYNC_US);
179 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.posX_, 0);
180 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.posY_, 0);
181 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.width_, 0);
182 ASSERT_EQ(testOccupiedAreaChangeListener_->rect_.height_, 0);
183 }
184 }
185 } // namespace Rosen
186 } // namespace OHOS
187