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 "ui_test.h"
17 
18 #include "common/screen.h"
19 #include "components/ui_canvas.h"
20 #include "components/ui_label.h"
21 #include "components/ui_scroll_view.h"
22 #include "components/ui_view_group.h"
23 #include "components/root_view.h"
24 #include "hal_tick.h"
25 
26 #include <stdio.h>
27 
28 using namespace OHOS;
29 class UITest : public UIView::OnClickListener
30 {
31 public:
32     static UITest *GetInstance();
33     void SetUp();
34     void Start();
35     const UIView* GetView();
36 
37 private:
UITest()38     UITest() {}
~UITest()39     ~UITest() {}
40     UIViewGroup* CreateGroup() const;
41     UILabel* CreateLabel() const;
42     UICanvas* CreateCanvas() const;
43     UIScrollView* container_ = nullptr;
44     RootView* rootView_ = nullptr;
45 };
46 
GetInstance()47 UITest *UITest::GetInstance()
48 {
49     static UITest instance;
50     return &instance;
51 }
52 
SetUp()53 void UITest::SetUp()
54 {
55     if (rootView_ == nullptr) {
56         rootView_ = RootView::GetInstance();
57         rootView_->SetPosition(0, 0);
58         rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
59     }
60 
61     if (container_ == nullptr) {
62         container_ = new UIScrollView();
63         container_->SetThrowDrag(true);
64         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
65         container_->SetHorizontalScrollState(false);
66     }
67 }
68 
CreateGroup() const69 UIViewGroup* UITest::CreateGroup() const
70 {
71     UIViewGroup* group = new UIViewGroup();
72     group->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
73     group->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
74     group->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
75     return group;
76 }
77 
CreateLabel() const78 UILabel* UITest::CreateLabel() const
79 {
80     UILabel* label = new UILabel();
81     label->SetPosition(250, 200, 150, 64);
82     return label;
83 }
84 
CreateCanvas() const85 UICanvas* UITest::CreateCanvas() const
86 {
87     UICanvas* canvas = new UICanvas();
88     canvas->SetHeight(200);
89     canvas->SetWidth(200);
90     canvas->SetPosition(48, 48);
91     canvas->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
92     return canvas;
93 }
94 
Start()95 void UITest::Start()
96 {
97     SetUp();
98 
99     if (rootView_ == nullptr) {
100         return;
101     }
102 
103     if (container_ == nullptr) {
104         return;
105     }
106     UIViewGroup* group = CreateGroup();
107     group->SetViewId("GROUP_001");
108     group->SetPosition(0, 0);
109 
110     UILabel* label = CreateLabel();
111     group->Add(label);
112     label->SetText("水平直线绘制 ");
113     UICanvas* canvas = CreateCanvas();
114     Paint paint;
115     canvas->DrawLine({50, 50}, {150, 50}, paint); // 50 : start x 50 : start y 150 : end x 50 : end y
116     group->Add(canvas);
117     container_->Add(group);
118     rootView_->Add(container_);
119     rootView_->Invalidate();
120 }
121 
UiTestStart(void)122 void UiTestStart(void)
123 {
124     UITest::GetInstance()->Start();
125 }