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