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 "window_test_utils.h"
19 #include "wm_common.h"
20 #include "scene_board_judgement.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 using Utils = WindowTestUtils;
28 class WindowInputMethodTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 Utils::TestWindowInfo inputMethodWindowInfo_;
35
36 private:
37 static constexpr uint32_t TEST_SLEEP_SECOND = 1;
38 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
39 };
40
SetUpTestCase()41 void WindowInputMethodTest::SetUpTestCase()
42 {
43 auto display = DisplayManager::GetInstance().GetDisplayById(0);
44 ASSERT_TRUE((display != nullptr));
45 Rect displayRect = {0, 0, display->GetWidth(), display->GetHeight()};
46 Utils::InitByDisplayRect(displayRect);
47 }
48
TearDownTestCase()49 void WindowInputMethodTest::TearDownTestCase()
50 {
51 }
52
SetUp()53 void WindowInputMethodTest::SetUp()
54 {
55 inputMethodWindowInfo_ = {
56 .name = "",
57 .rect = Utils::customAppRect_,
58 .type = WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT,
59 .mode = WindowMode::WINDOW_MODE_FLOATING,
60 .needAvoid = false,
61 .parentLimit = false,
62 .showWhenLocked = true,
63 .parentId = INVALID_WINDOW_ID,
64 };
65 }
66
TearDown()67 void WindowInputMethodTest::TearDown()
68 {
69 usleep(WAIT_SYNC_IN_NS);
70 }
71
72 namespace {
73 /**
74 * @tc.name: InputMethodWindow01
75 * @tc.desc: One InputMethod Floating Window
76 * @tc.type: FUNC
77 */
78 HWTEST_F(WindowInputMethodTest, InputMethodWindow01, Function | MediumTest | Level3)
79 {
80 inputMethodWindowInfo_.name = "input_method.1";
81 const sptr<Window>& window = Utils::CreateTestWindow(inputMethodWindowInfo_);
82 ASSERT_NE(window, nullptr);
83 ASSERT_EQ(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT, window->GetType());
84 window->SetWindowGravity(WindowGravity::WINDOW_GRAVITY_BOTTOM, 0);
85 ASSERT_EQ(WMError::WM_OK, window->Show());
86 ASSERT_EQ(WMError::WM_OK, window->Hide());
87 window->SetWindowGravity(WindowGravity::WINDOW_GRAVITY_FLOAT, 0);
88 ASSERT_EQ(WMError::WM_OK, window->Show());
89 ASSERT_EQ(WMError::WM_OK, window->Hide());
90 }
91
92 /**
93 * @tc.name: InputMethodWindow02
94 * @tc.desc: One InputMethod Floating Window & One KeyGuard Window
95 * @tc.type: FUNC
96 */
97 HWTEST_F(WindowInputMethodTest, InputMethodWindow02, Function | MediumTest | Level3)
98 {
99 inputMethodWindowInfo_.name = "input_method.2";
100 const sptr<Window>& inputMethodWindow = Utils::CreateTestWindow(inputMethodWindowInfo_);
101 ASSERT_NE(inputMethodWindow, nullptr);
102 inputMethodWindow->SetWindowGravity(WindowGravity::WINDOW_GRAVITY_BOTTOM, 0);
103 inputMethodWindow->Show();
104 if (Utils::customAppRect_.width_ == inputMethodWindow->GetRect().width_) {
105 ASSERT_EQ(inputMethodWindow->GetRect().width_, Utils::customAppRect_.width_);
106 }
107
108 if (inputMethodWindow->GetRect().height_ == Utils::customAppRect_.height_) {
109 ASSERT_EQ(inputMethodWindow->GetRect().height_, Utils::customAppRect_.height_);
110 }
111
112 inputMethodWindow->Hide();
113 inputMethodWindow->SetWindowGravity(WindowGravity::WINDOW_GRAVITY_FLOAT, 0);
114 inputMethodWindow->Show();
115
116 if (Utils::customAppRect_.width_ == inputMethodWindow->GetRect().width_) {
117 ASSERT_EQ(inputMethodWindow->GetRect().width_, Utils::customAppRect_.width_);
118 ASSERT_EQ(inputMethodWindow->GetRect().height_, Utils::customAppRect_.height_);
119 }
120 inputMethodWindow->Hide();
121 }
122
123 /**
124 * @tc.name: ShowKeyboard1
125 * @tc.desc: create window and show keyboard.
126 * @tc.type: FUNC
127 */
128 HWTEST_F(WindowInputMethodTest, ShowKeyboard01, Function | MediumTest | Level3)
129 {
130 WindowTestUtils::TestWindowInfo windowInfo = {
131 .name = "ShowKeyboard",
132 .rect = Utils::customAppRect_,
133 .type = WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT,
134 .mode = WindowMode::WINDOW_MODE_FLOATING,
135 .needAvoid = false,
136 .parentLimit = false,
137 .showWhenLocked = true,
138 .parentId = INVALID_WINDOW_ID,
139 };
140 const sptr<Window>& fullWindow = Utils::CreateTestWindow(windowInfo);
141 if (fullWindow == nullptr) {
142 return;
143 }
144 if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
145 sleep(TEST_SLEEP_SECOND);
146 ASSERT_EQ(WMError::WM_OK, fullWindow->ShowKeyboard(KeyboardViewMode::NON_IMMERSIVE_MODE));
147 sleep(TEST_SLEEP_SECOND);
148 ASSERT_EQ(WMError::WM_OK, fullWindow->ChangeKeyboardViewMode(KeyboardViewMode::NON_IMMERSIVE_MODE));
149 sleep(TEST_SLEEP_SECOND);
150 fullWindow->Destroy();
151 return;
152 }
153 ASSERT_EQ(WMError::WM_OK, fullWindow->ShowKeyboard(KeyboardViewMode::DARK_IMMERSIVE_MODE));
154 sleep(TEST_SLEEP_SECOND);
155
156 ASSERT_EQ(WMError::WM_OK, fullWindow->ChangeKeyboardViewMode(KeyboardViewMode::LIGHT_IMMERSIVE_MODE));
157 sleep(TEST_SLEEP_SECOND);
158
159 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM,
160 fullWindow->ChangeKeyboardViewMode(static_cast<KeyboardViewMode>(-1)));
161 sleep(TEST_SLEEP_SECOND);
162
163 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
164 sleep(TEST_SLEEP_SECOND);
165
166 ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW,
167 fullWindow->ChangeKeyboardViewMode(KeyboardViewMode::DARK_IMMERSIVE_MODE));
168 sleep(TEST_SLEEP_SECOND);
169 fullWindow->Destroy();
170 }
171 } // namespace
172 } // namespace Rosen
173 } // namespace OHOS
174