1 /*
2 * Copyright (c) 2021 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 "root_component_mock.h"
17
18 #include "div_component.h"
19 #include "stack_component.h"
20
21 namespace OHOS {
22 namespace ACELite {
RootComponentMock()23 RootComponentMock::RootComponentMock()
24 : rootContainer_(nullptr),
25 containerStyleMgr_(nullptr),
26 rootWidth_(DEFAULT_CONTAINER_WIDTH),
27 rootHeight_(DEFAULT_CONTAINER_HEIGHT)
28 {
29 }
30
~RootComponentMock()31 RootComponentMock::~RootComponentMock()
32 {
33 if (containerStyleMgr_ != nullptr) {
34 delete containerStyleMgr_;
35 containerStyleMgr_ = nullptr;
36 }
37
38 if (rootContainer_ != nullptr) {
39 // we do not render this container, so no need to call release
40 delete rootContainer_;
41 rootContainer_ = nullptr;
42 }
43 }
44
45 /**
46 * @brief PrepareRootContainer prepare the container, must call this after engine initialized
47 */
PrepareRootContainer(int16_t width,int16_t height,bool flex)48 void RootComponentMock::PrepareRootContainer(int16_t width, int16_t height, bool flex)
49 {
50 rootWidth_ = width;
51 rootHeight_ = height;
52
53 if (containerStyleMgr_ == nullptr) {
54 containerStyleMgr_ = new AppStyleManager();
55 }
56
57 if (rootContainer_ == nullptr) {
58 if (flex) {
59 rootContainer_ = new DivComponent(UNDEFINED, UNDEFINED, containerStyleMgr_);
60 } else {
61 rootContainer_ = new StackComponent(UNDEFINED, UNDEFINED, containerStyleMgr_);
62 }
63 }
64 }
65
66 /**
67 * @brief RenderUnderTestComponent render the given component and build view tree to the container
68 * @param component the under test component
69 */
RenderComponent(Component & component) const70 void RootComponentMock::RenderComponent(Component &component) const
71 {
72 component.Render();
73 // trigger build tree flow to apply aligned dimension to low layer
74 ConstrainedParameter parentParameter = {rootWidth_, rootHeight_};
75 Component::BuildViewTree(&component, rootContainer_, parentParameter);
76 }
77 } // namespace ACELite
78 } // namespace OHOS
79