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 #include "rs_animation_base_test.h"
17 
18 #include "rs_animation_test_utils.h"
19 
20 #include "event_handler.h"
21 #include "common/rs_color.h"
22 #include "transaction/rs_transaction.h"
23 #include "wm/window_option.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 using namespace ANIMATIONTEST;
28 sptr<Window> RSAnimationBaseTest::window = nullptr;
29 std::shared_ptr<RSSurfaceNode> RSAnimationBaseTest::animationSurfaceNode = nullptr;
30 std::shared_ptr<RSNode> RSAnimationBaseTest::rootNode = nullptr;
31 std::shared_ptr<RSCanvasNode> RSAnimationBaseTest::canvasNode = nullptr;
32 std::shared_ptr<RSUIDirector> RSAnimationBaseTest::rsUiDirector = nullptr;
33 
SetUpTestCase()34 void RSAnimationBaseTest::SetUpTestCase()
35 {
36     system("setenforce 0");
37     InitAnimationWindow();
38 }
39 
TearDownTestCase()40 void RSAnimationBaseTest::TearDownTestCase()
41 {
42     DestoryAnimationWindow();
43 }
44 
SetUp()45 void RSAnimationBaseTest::SetUp()
46 {
47     ResetAnimationCanvasNode();
48 }
49 
TearDown()50 void RSAnimationBaseTest::TearDown()
51 {
52     RemoveAnimationCanvasNode();
53 }
54 
InitNode(int width,int height)55 void RSAnimationBaseTest::InitNode(int width, int height)
56 {
57     rootNode = RSRootNode::Create();
58     rootNode->SetBounds(0, 0, width, height);
59     rootNode->SetFrame(0, 0, width, height);
60     rootNode->SetBackgroundColor(SK_ColorYELLOW);
61 
62     canvasNode = RSCanvasNode::Create();
63     canvasNode->SetBounds(ANIMATION_START_BOUNDS);
64     canvasNode->SetFrame(ANIMATION_START_BOUNDS);
65     canvasNode->SetBackgroundColor(SK_ColorBLUE);
66     rootNode->AddChild(canvasNode, -1);
67     rsUiDirector->SetRoot(rootNode->GetId());
68 }
69 
InitAnimationWindow()70 void RSAnimationBaseTest::InitAnimationWindow()
71 {
72     std::cout << "InitAnimationWindow start" << std::endl;
73     if (window != nullptr) {
74         return;
75     }
76     sptr<WindowOption> option = new WindowOption();
77     option->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
78     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
79     option->SetWindowRect({ 0, 0, 720, 1280 });
80     window = Window::Create("animation_ut", option);
81     while (!window) {
82         window = Window::Create("animation_ut", option);
83     }
84     window->Show();
85     sleep(1);
86     auto rect = window->GetRequestRect();
87     while (rect.width_ == 0 && rect.height_ == 0) {
88         std::cout << "animation_ut create window failed: " << rect.width_ << " " << rect.height_ << std::endl;
89         window->Hide();
90         window->Destroy();
91         window = Window::Create("animation_ut", option);
92         window->Show();
93         sleep(1);
94         rect = window->GetRequestRect();
95     }
96     std::cout << "animation_ut create window " << rect.width_ << " " << rect.height_ << std::endl;
97     animationSurfaceNode = window->GetSurfaceNode();
98     rsUiDirector = RSUIDirector::Create();
99     rsUiDirector->Init();
100     auto runner = OHOS::AppExecFwk::EventRunner::Create(true);
101     auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner);
102     rsUiDirector->SetUITaskRunner(
103         [handler](const std::function<void()>& task, uint32_t delay) { handler->PostTask(task); });
104     runner->Run();
105     RSTransaction::FlushImplicitTransaction();
106     sleep(DELAY_TIME_ONE);
107     rsUiDirector->SetRSSurfaceNode(animationSurfaceNode);
108     InitNode(rect.width_, rect.height_);
109     rsUiDirector->SendMessages();
110     sleep(DELAY_TIME_THREE);
111     std::cout << "InitAnimationWindow end " << std::endl;
112 }
113 
DestoryAnimationWindow()114 void RSAnimationBaseTest::DestoryAnimationWindow()
115 {
116     std::cout << "DestoryAnimationWindow start" << std::endl;
117     window->Hide();
118     window->Destroy();
119     window = nullptr;
120     animationSurfaceNode = nullptr;
121     rootNode = nullptr;
122     rsUiDirector->Destroy();
123     rsUiDirector = nullptr;
124     std::cout << "DestoryAnimationWindow end" << std::endl;
125 }
126 
RemoveAnimationCanvasNode()127 void RSAnimationBaseTest::RemoveAnimationCanvasNode()
128 {
129     rootNode->RemoveChild(canvasNode);
130     canvasNode = nullptr;
131 }
132 
ResetAnimationCanvasNode()133 void RSAnimationBaseTest::ResetAnimationCanvasNode()
134 {
135     canvasNode = RSCanvasNode::Create();
136     canvasNode->SetBounds(ANIMATION_START_BOUNDS);
137     canvasNode->SetFrame(ANIMATION_START_BOUNDS);
138     canvasNode->SetBackgroundColor(SK_ColorBLUE);
139     rootNode->AddChild(canvasNode, -1);
140     rsUiDirector->SendMessages();
141     sleep(DELAY_TIME_TWO);
142 }
143 
NotifyStartAnimation()144 void RSAnimationBaseTest::NotifyStartAnimation()
145 {
146     int64_t startNum = START_NUMBER;
147     bool hasRunningAnimation = true;
148     while (hasRunningAnimation) {
149         hasRunningAnimation = rsUiDirector->FlushAnimation(startNum);
150         rsUiDirector->FlushModifier();
151         rsUiDirector->SendMessages();
152         startNum += INCREASE_NUMBER;
153         usleep(DELAY_TIME_REFRESH);
154     }
155     sleep(DELAY_TIME_ONE);
156 }
157 }
158 }
159