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 */
105 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest01, Function | MediumTest | Level3)
106 {
107 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
108 ASSERT_NE(window, nullptr);
109 sptr<TestAnimationTransitionController> testAnimationTransitionListener =
110 new TestAnimationTransitionController(window);
111 window->RegisterAnimationTransitionController(testAnimationTransitionListener);
112 window->Show(0, true);
113 usleep(500000); // 500000us = 0.5s
114 ASSERT_EQ(WMError::WM_OK, window->Hide(0, true));
115 usleep(500000); // 500000us = 0.5s
116 ASSERT_TRUE(trans_ == window->GetTransform());
117 ASSERT_EQ(WMError::WM_OK, window->Destroy());
118 }
119
120 /**
121 * @tc.name: AnimationTransitionTest02
122 * @tc.desc: Register AnimationController and show without animation
123 * @tc.type: FUNC
124 * @tc.require: issueI5NDLK
125 */
126 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest02, Function | MediumTest | Level3)
127 {
128 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
129 ASSERT_NE(window, nullptr);
130 sptr<TestAnimationTransitionController> testAnimationTransitionListener =
131 new TestAnimationTransitionController(window);
132 window->RegisterAnimationTransitionController(testAnimationTransitionListener);
133 window->Show(0, true);
134 usleep(500000); // 500000us = 0.5s
135 ASSERT_TRUE(defaultTrans_ == window->GetTransform());
136 ASSERT_EQ(WMError::WM_OK, window->Destroy());
137 }
138
139 /**
140 * @tc.name: AnimationTransitionTest03
141 * @tc.desc: hide with default animation without register animationController
142 * @tc.type: FUNC
143 * @tc.require: issueI5NDLK
144 */
145 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest03, Function | MediumTest | Level3)
146 {
147 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
148 ASSERT_NE(window, nullptr);
149 ASSERT_EQ(WMError::WM_OK, window->Hide(0, true));
150 usleep(500000); // 500000us = 0.5s
151 ASSERT_TRUE(defaultTrans_ == window->GetTransform());
152 ASSERT_EQ(WMError::WM_OK, window->Destroy());
153 }
154
155 /**
156 * @tc.name: AnimationTransitionTest04
157 * @tc.desc: hide without custom animation and register animationController
158 * @tc.type: FUNC
159 * @tc.require: issueI5NDLK
160 */
161 HWTEST_F(WindowAnimationTransitionTest, AnimationTransitionTest04, Function | MediumTest | Level3)
162 {
163 const sptr<Window>& window = Utils::CreateTestWindow(windowInfo_);
164 ASSERT_NE(window, nullptr);
165 sptr<TestAnimationTransitionController> testAnimationTransitionListener =
166 new TestAnimationTransitionController(window);
167 window->RegisterAnimationTransitionController(testAnimationTransitionListener);
168 window->Show(0, true);
169 usleep(500000); // 500000us = 0.5s
170 ASSERT_EQ(WMError::WM_OK, window->Hide());
171 ASSERT_TRUE(defaultTrans_ == window->GetTransform());
172 ASSERT_EQ(WMError::WM_OK, window->Destroy());
173 }
174 }
175 } // namespace Rosen
176 } // namespace OHOS
177