1 /*
2 * Copyright (c) 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.h"
19 #include "wm_common.h"
20 #include "common_test_utils.h"
21 #include "window_test_utils.h"
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAnimationTest"};
29 }
30
31 using Utils = WindowTestUtils;
32
33 class TestAnimationTransitionController : public IAnimationTransitionController {
34 public:
TestAnimationTransitionController(sptr<Window> window)35 explicit TestAnimationTransitionController(sptr<Window> window) : window_(window) {}
36 void AnimationForShown() override;
37 void AnimationForHidden() override;
38 private:
39 sptr<Window> window_ = nullptr;
40 };
41
42 class WindowAnimationTransitionTest : public testing::Test {
43 public:
44 static void SetUpTestCase();
45 static void TearDownTestCase();
46 virtual void SetUp() override;
47 virtual void TearDown() override;
48 sptr<TestAnimationTransitionController> testAnimationTransitionListener_;
49 Utils::TestWindowInfo windowInfo_;
50 Transform trans_;
51 Transform defaultTrans_;
52 };
53
AnimationForShown()54 void TestAnimationTransitionController::AnimationForShown()
55 {
56 WLOGI("TestAnimationTransitionController AnimationForShown");
57 Transform trans;
58 window_->SetTransform(trans);
59 }
60
AnimationForHidden()61 void TestAnimationTransitionController::AnimationForHidden()
62 {
63 WLOGI("TestAnimationTransitionController AnimationForHidden");
64 Transform trans;
65 trans.pivotX_ = 1.0f;
66 trans.pivotY_ = 0.6f;
67 window_->SetTransform(trans);
68 }
69
SetUpTestCase()70 void WindowAnimationTransitionTest::SetUpTestCase()
71 {
72 }
73
TearDownTestCase()74 void WindowAnimationTransitionTest::TearDownTestCase()
75 {
76 }
77
SetUp()78 void WindowAnimationTransitionTest::SetUp()
79 {
80 CommonTestUtils::GuaranteeFloatWindowPermission("wms_window_animation_transition_test");
81 windowInfo_ = {
82 .name = "AnimationTestWindow",
83 .rect = {0, 0, 200, 200},
84 .type = WindowType::WINDOW_TYPE_FLOAT,
85 .mode = WindowMode::WINDOW_MODE_FLOATING,
86 .needAvoid = false,
87 .parentLimit = false,
88 .parentId = INVALID_WINDOW_ID,
89 };
90 trans_.pivotX_ = 1.0f;
91 trans_.pivotY_ = 0.6f;
92 }
93
TearDown()94 void WindowAnimationTransitionTest::TearDown()
95 {
96 }
97
98 namespace {
99 /**
100 * @tc.name: AnimationTransitionTest01
101 * @tc.desc: Register AnimationController and hide with custom animation
102 * @tc.type: FUNC
103 * @tc.require: issueI5NDLK
104 */
HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest01, Function | MediumTest | Level3)105 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest01, Function | MediumTest | Level3)
106 {
107 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
108 if (window == nullptr) {
109 return;
110 }
111 sptr<TestAnimationTransitionController> testAnimationTransitionListener =
112 new TestAnimationTransitionController(window);
113 window->RegisterAnimationTransitionController(testAnimationTransitionListener);
114 window->Show(0, true);
115 usleep(500000); // 500000us = 0.5s
116 ASSERT_EQ(WMError::WM_OK, window->Hide(0, true));
117 usleep(500000); // 500000us = 0.5s
118 ASSERT_TRUE(trans_ == window->GetTransform());
119 ASSERT_EQ(WMError::WM_OK, window->Destroy());
120 }
121
122 /**
123 * @tc.name: AnimationTransitionTest02
124 * @tc.desc: Register AnimationController and show without animation
125 * @tc.type: FUNC
126 * @tc.require: issueI5NDLK
127 */
HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest02, Function | MediumTest | Level3)128 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest02, Function | MediumTest | Level3)
129 {
130 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
131 if (window == nullptr) {
132 return;
133 }
134 sptr<TestAnimationTransitionController> testAnimationTransitionListener =
135 new TestAnimationTransitionController(window);
136 window->RegisterAnimationTransitionController(testAnimationTransitionListener);
137 window->Show(0, true);
138 usleep(500000); // 500000us = 0.5s
139 ASSERT_TRUE(defaultTrans_ == window->GetTransform());
140 ASSERT_EQ(WMError::WM_OK, window->Destroy());
141 }
142
143 /**
144 * @tc.name: AnimationTransitionTest03
145 * @tc.desc: hide with default animation without register animationController
146 * @tc.type: FUNC
147 * @tc.require: issueI5NDLK
148 */
HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest03, Function | MediumTest | Level3)149 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest03, Function | MediumTest | Level3)
150 {
151 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
152 if (window == nullptr) {
153 return;
154 }
155 ASSERT_EQ(WMError::WM_OK, window->Hide(0, true));
156 usleep(500000); // 500000us = 0.5s
157 ASSERT_TRUE(defaultTrans_ == window->GetTransform());
158 ASSERT_EQ(WMError::WM_OK, window->Destroy());
159 }
160
161 /**
162 * @tc.name: AnimationTransitionTest04
163 * @tc.desc: hide without custom animation and register animationController
164 * @tc.type: FUNC
165 * @tc.require: issueI5NDLK
166 */
HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest04, Function | MediumTest | Level3)167 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest04, Function | MediumTest | Level3)
168 {
169 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
170 if (window == nullptr) {
171 return;
172 }
173 sptr<TestAnimationTransitionController> testAnimationTransitionListener =
174 new TestAnimationTransitionController(window);
175 window->RegisterAnimationTransitionController(testAnimationTransitionListener);
176 window->Show(0, true);
177 usleep(500000); // 500000us = 0.5s
178 ASSERT_EQ(WMError::WM_OK, window->Hide());
179 ASSERT_TRUE(defaultTrans_ == window->GetTransform());
180 ASSERT_EQ(WMError::WM_OK, window->Destroy());
181 }
182 }
183 } // namespace Rosen
184 } // namespace OHOS
185