1 /* 2 * Copyright (c) 2020-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 "dfx/ui_view_bounds.h" 17 18 #if ENABLE_DEBUG 19 #include <climits> 20 #include <gtest/gtest.h> 21 22 #include "common/graphic_startup.h" 23 #include "common/task_manager.h" 24 #include "components/root_view.h" 25 #include "components/ui_view.h" 26 #include "components/ui_view_group.h" 27 #if ENABLE_WINDOW 28 #include "window/window.h" 29 #endif 30 31 using namespace testing::ext; 32 namespace OHOS { 33 static uint16_t g_drawCount = 0; 34 class ViewBoundsTest : public testing::Test { 35 public: ViewBoundsTest()36 ViewBoundsTest() {} ~ViewBoundsTest()37 virtual ~ViewBoundsTest() {} 38 SetUpTestCase()39 static void SetUpTestCase() 40 { 41 GraphicStartUp::Init(); 42 } 43 TearDownTestCase()44 static void TearDownTestCase() {} 45 46 #if ENABLE_WINDOW CreateDefaultWindow(RootView * rootView,int x,int y)47 static void CreateDefaultWindow(RootView* rootView, int x, int y) 48 { 49 WindowConfig config = {}; 50 config.rect = rootView->GetRect(); 51 config.rect.SetPosition(x, y); 52 Window* window = Window::CreateWindow(config); 53 if (window != nullptr) { 54 window->BindRootView(rootView); 55 } 56 } 57 DestoryWindow(RootView * rootView)58 static void DestoryWindow(RootView* rootView) 59 { 60 Window::DestoryWindow(rootView->GetBoundWindow()); 61 } 62 #endif // ENABLE_WINDOW 63 }; 64 65 class UIBoundsTestView : public UIView { 66 public: UIBoundsTestView()67 UIBoundsTestView() {} ~UIBoundsTestView()68 virtual ~UIBoundsTestView() {} 69 OnPostDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)70 void OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override 71 { 72 g_drawCount++; 73 } 74 }; 75 76 class UIBoundsViewGroup : public UIViewGroup { 77 public: UIBoundsViewGroup()78 UIBoundsViewGroup() {} ~UIBoundsViewGroup()79 virtual ~UIBoundsViewGroup() {} 80 OnPostDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)81 void OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override 82 { 83 g_drawCount++; 84 } 85 }; 86 87 /** 88 * @tc.name: ViewBoundsSetState001 89 * @tc.desc: Verity if view bounds state equal 90 * @tc.type: FUNC 91 * @tc.require: AR000FQNFN 92 */ 93 HWTEST_F(ViewBoundsTest, ViewBoundsSetState001, TestSize.Level0) 94 { 95 UIViewBounds::GetInstance()->SetShowState(true); 96 bool state = UIViewBounds::GetInstance()->GetShowState(); 97 EXPECT_EQ(state, true); 98 99 UIViewBounds::GetInstance()->SetShowState(false); 100 state = UIViewBounds::GetInstance()->GetShowState(); 101 EXPECT_EQ(state, false); 102 } 103 104 /** 105 * @tc.name: ViewBoundsSetState002 106 * @tc.desc: Verity if view bounds state equal when calling multi times 107 * @tc.type: FUNC 108 * @tc.require: AR000FQNFN 109 */ 110 HWTEST_F(ViewBoundsTest, ViewBoundsSetState002, TestSize.Level1) 111 { 112 uint16_t count = 10000; // 10000: call times 113 for (uint16_t i = 0; i < count; i++) { 114 UIViewBounds::GetInstance()->SetShowState(true); 115 } 116 bool state = UIViewBounds::GetInstance()->GetShowState(); 117 EXPECT_EQ(state, true); 118 119 for (uint16_t i = 0; i < count; i++) { 120 UIViewBounds::GetInstance()->SetShowState(false); 121 } 122 state = UIViewBounds::GetInstance()->GetShowState(); 123 EXPECT_EQ(state, false); 124 125 for (uint16_t i = 0; i < count; i++) { 126 UIViewBounds::GetInstance()->SetShowState(true); 127 UIViewBounds::GetInstance()->SetShowState(false); 128 UIViewBounds::GetInstance()->SetShowState(true); 129 } 130 state = UIViewBounds::GetInstance()->GetShowState(); 131 EXPECT_EQ(state, true); 132 133 for (uint16_t i = 0; i < count; i++) { 134 UIViewBounds::GetInstance()->SetShowState(false); 135 UIViewBounds::GetInstance()->SetShowState(true); 136 UIViewBounds::GetInstance()->SetShowState(false); 137 } 138 state = UIViewBounds::GetInstance()->GetShowState(); 139 EXPECT_EQ(state, false); 140 } 141 142 /** 143 * @tc.name: ViewBoundsOnPostDraw001 144 * @tc.desc: Test if trigger redraw when change view bounds state with sigle window 145 * @tc.type: FUNC 146 * @tc.require: AR000FQNFN 147 */ 148 #if ENABLE_WINDOW 149 HWTEST_F(ViewBoundsTest, ViewBoundsOnPostDraw001, TestSize.Level0) 150 { 151 RootView* rootView = RootView::GetWindowRootView(); 152 rootView->SetWidth(600); // 600: width 153 rootView->SetHeight(500); // 500: height 154 rootView->SetPosition(0, 0); 155 UIBoundsTestView* view1 = new UIBoundsTestView(); 156 UIBoundsViewGroup* vg1 = new UIBoundsViewGroup(); 157 rootView->Add(vg1); 158 vg1->Add(view1); 159 rootView->Invalidate(); 160 ViewBoundsTest::CreateDefaultWindow(rootView, 0, 0); 161 // clear invalidate area 162 TaskManager::GetInstance()->TaskHandler(); 163 164 // init show state false 165 UIViewBounds::GetInstance()->SetShowState(false); 166 167 usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task 168 g_drawCount = 0; 169 UIViewBounds::GetInstance()->SetShowState(true); 170 TaskManager::GetInstance()->TaskHandler(); 171 EXPECT_EQ(g_drawCount, 2); // 2: redraw view count 172 173 usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task 174 g_drawCount = 0; 175 UIViewBounds::GetInstance()->SetShowState(false); 176 TaskManager::GetInstance()->TaskHandler(); 177 EXPECT_EQ(g_drawCount, 2); // 2: redraw view count 178 179 rootView->RemoveAll(); 180 delete view1; 181 delete vg1; 182 ViewBoundsTest::DestoryWindow(rootView); 183 RootView::DestoryWindowRootView(rootView); 184 } 185 186 /** 187 * @tc.name: ViewBoundsOnPostDraw001 188 * @tc.desc: Test if trigger redraw when change view bounds state with multi window 189 * @tc.type: FUNC 190 * @tc.require: AR000FQNFN 191 */ 192 HWTEST_F(ViewBoundsTest, ViewBoundsOnPostDraw002, TestSize.Level0) 193 { 194 RootView* rootView1 = RootView::GetWindowRootView(); 195 rootView1->SetWidth(600); // 600: width 196 rootView1->SetHeight(500); // 500: height 197 rootView1->SetPosition(0, 0); 198 UIBoundsTestView* view1 = new UIBoundsTestView(); 199 UIBoundsViewGroup* vg1 = new UIBoundsViewGroup(); 200 rootView1->Add(vg1); 201 vg1->Add(view1); 202 rootView1->Invalidate(); 203 ViewBoundsTest::CreateDefaultWindow(rootView1, 0, 0); 204 205 RootView* rootView2 = RootView::GetWindowRootView(); 206 rootView2->SetWidth(600); // 600: width 207 rootView2->SetHeight(500); // 500: height 208 rootView2->SetPosition(0, 0); 209 UIBoundsTestView* view2 = new UIBoundsTestView(); 210 UIBoundsViewGroup* vg2 = new UIBoundsViewGroup(); 211 rootView2->Add(vg2); 212 vg2->Add(view2); 213 rootView2->Invalidate(); 214 ViewBoundsTest::CreateDefaultWindow(rootView2, 0, 0); 215 // clear invalidate area 216 TaskManager::GetInstance()->TaskHandler(); 217 218 usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task 219 g_drawCount = 0; 220 UIViewBounds::GetInstance()->SetShowState(true); 221 TaskManager::GetInstance()->TaskHandler(); 222 EXPECT_EQ(g_drawCount, 4); // 4: redraw view count 223 224 usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task 225 g_drawCount = 0; 226 UIViewBounds::GetInstance()->SetShowState(false); 227 TaskManager::GetInstance()->TaskHandler(); 228 EXPECT_EQ(g_drawCount, 4); // 4: redraw view count 229 230 rootView1->RemoveAll(); 231 delete view1; 232 delete vg1; 233 ViewBoundsTest::DestoryWindow(rootView1); 234 RootView::DestoryWindowRootView(rootView1); 235 rootView2->RemoveAll(); 236 delete view2; 237 delete vg2; 238 ViewBoundsTest::DestoryWindow(rootView2); 239 RootView::DestoryWindowRootView(rootView2); 240 } 241 #endif // ENABLE_WINDOW 242 } // namespace OHOS 243 #endif // ENABLE_DEBUG