From 833540972fead1e05a2be8e19ec42f74afd014e4 Mon Sep 17 00:00:00 2001 From: Yegor Date: Thu, 20 Sep 2018 16:44:19 -0700 Subject: [PATCH] add a test for single- and multi-line paragraph layout (#22047) * add a test for single- and multi-line paragraph layout * use flutter_test * use closeTo for double comparison --- .../flutter/test/engine/paragraph_test.dart | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 packages/flutter/test/engine/paragraph_test.dart diff --git a/packages/flutter/test/engine/paragraph_test.dart b/packages/flutter/test/engine/paragraph_test.dart new file mode 100644 index 00000000000..695a93f2054 --- /dev/null +++ b/packages/flutter/test/engine/paragraph_test.dart @@ -0,0 +1,62 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:ui'; + +import 'package:flutter_test/flutter_test.dart'; + +void main() { + // Ahem font uses a constant ideographic/alphabetic baseline ratio. + const double kAhemBaselineRatio = 1.1662499904632568; + + test('predictably lays out a single-line paragraph', () { + for (double fontSize in [10.0, 20.0, 30.0, 40.0]) { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: fontSize, + )); + builder.addText('Test'); + final Paragraph paragraph = builder.build(); + paragraph.layout(ParagraphConstraints(width: 400.0)); + + expect(paragraph.height, closeTo(fontSize, 0.001)); + expect(paragraph.width, closeTo(400.0, 0.001)); + expect(paragraph.minIntrinsicWidth, closeTo(fontSize * 4.0, 0.001)); + expect(paragraph.maxIntrinsicWidth, closeTo(fontSize * 4.0, 0.001)); + expect(paragraph.alphabeticBaseline, closeTo(fontSize * .8, 0.001)); + expect( + paragraph.ideographicBaseline, + closeTo(paragraph.alphabeticBaseline * kAhemBaselineRatio, 0.001), + ); + } + }); + + test('predictably lays out a multi-line paragraph', () { + for (double fontSize in [10.0, 20.0, 30.0, 40.0]) { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: fontSize, + )); + builder.addText('Test Ahem'); + final Paragraph paragraph = builder.build(); + paragraph.layout(ParagraphConstraints(width: fontSize * 5.0)); + + expect(paragraph.height, closeTo(fontSize * 2.0, 0.001)); // because it wraps + expect(paragraph.width, closeTo(fontSize * 5.0, 0.001)); + expect(paragraph.minIntrinsicWidth, closeTo(fontSize * 4.0, 0.001)); + + // TODO(yjbanov): see https://github.com/flutter/flutter/issues/21965 + expect(paragraph.maxIntrinsicWidth, closeTo(fontSize * 8.0, 0.001)); + expect(paragraph.alphabeticBaseline, closeTo(fontSize * .8, 0.001)); + expect( + paragraph.ideographicBaseline, + closeTo(paragraph.alphabeticBaseline * kAhemBaselineRatio, 0.001), + ); + } + }); +}