|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// @dart = 2.12 |
| 6 | +import 'package:test/bootstrap/browser.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | +import 'package:ui/src/engine.dart'; |
| 9 | +import 'package:ui/ui.dart' as ui; |
| 10 | + |
| 11 | +import 'common.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + internalBootstrapBrowserTest(() => testMain); |
| 15 | +} |
| 16 | + |
| 17 | +void testMain() { |
| 18 | + group('CanvasKit', () { |
| 19 | + setUpCanvasKitTest(); |
| 20 | + |
| 21 | + test('Surface allocates canvases efficiently', () { |
| 22 | + final Surface surface = Surface(HtmlViewEmbedder()); |
| 23 | + final CkSurface original = surface.acquireRenderSurface(ui.Size(9, 19)); |
| 24 | + |
| 25 | + // Expect exact requested dimensions. |
| 26 | + expect(original.width(), 9); |
| 27 | + expect(original.height(), 19); |
| 28 | + |
| 29 | + // Shrinking reuses the existing surface straight-up. |
| 30 | + final CkSurface shrunk = surface.acquireRenderSurface(ui.Size(5, 15)); |
| 31 | + expect(shrunk, same(original)); |
| 32 | + |
| 33 | + // The first increase will allocate a new surface, but will overallocate |
| 34 | + // by 40% to accommodate future increases. |
| 35 | + final CkSurface firstIncrease = surface.acquireRenderSurface(ui.Size(10, 20)); |
| 36 | + expect(firstIncrease, isNot(same(original))); |
| 37 | + |
| 38 | + // Expect overallocated dimensions |
| 39 | + expect(firstIncrease.width(), 14); |
| 40 | + expect(firstIncrease.height(), 28); |
| 41 | + |
| 42 | + // Subsequent increases within 40% reuse the old surface. |
| 43 | + final CkSurface secondIncrease = surface.acquireRenderSurface(ui.Size(11, 22)); |
| 44 | + expect(secondIncrease, same(firstIncrease)); |
| 45 | + |
| 46 | + // Increases beyond the 40% limit will cause a new allocation. |
| 47 | + final CkSurface huge = surface.acquireRenderSurface(ui.Size(20, 40)); |
| 48 | + expect(huge, isNot(same(firstIncrease))); |
| 49 | + |
| 50 | + // Also over-allocated |
| 51 | + expect(huge.width(), 28); |
| 52 | + expect(huge.height(), 56); |
| 53 | + |
| 54 | + // Shrink again. Reuse the last allocated surface. |
| 55 | + final CkSurface shrunk2 = surface.acquireRenderSurface(ui.Size(5, 15)); |
| 56 | + expect(shrunk2, same(huge)); |
| 57 | + }); |
| 58 | + }, skip: isIosSafari); |
| 59 | +} |
0 commit comments