• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 The Chromium 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
5import 'dart:ui';
6
7import 'package:test/test.dart';
8
9void main() {
10  // Ahem font uses a constant ideographic/alphabetic baseline ratio.
11  const double kAhemBaselineRatio = 1.25;
12
13  test('predictably lays out a single-line paragraph', () {
14    for (double fontSize in <double>[10.0, 20.0, 30.0, 40.0]) {
15      final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
16        fontFamily: 'Ahem',
17        fontStyle: FontStyle.normal,
18        fontWeight: FontWeight.normal,
19        fontSize: fontSize,
20      ));
21      builder.addText('Test');
22      final Paragraph paragraph = builder.build();
23      paragraph.layout(const ParagraphConstraints(width: 400.0));
24
25      expect(paragraph.height, closeTo(fontSize, 0.001));
26      expect(paragraph.width, closeTo(400.0, 0.001));
27      expect(paragraph.minIntrinsicWidth, closeTo(fontSize * 4.0, 0.001));
28      expect(paragraph.maxIntrinsicWidth, closeTo(fontSize * 4.0, 0.001));
29      expect(paragraph.alphabeticBaseline, closeTo(fontSize * .8, 0.001));
30      expect(
31        paragraph.ideographicBaseline,
32        closeTo(paragraph.alphabeticBaseline * kAhemBaselineRatio, 0.001),
33      );
34    }
35  });
36
37  test('predictably lays out a multi-line paragraph', () {
38    for (double fontSize in <double>[10.0, 20.0, 30.0, 40.0]) {
39      final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
40        fontFamily: 'Ahem',
41        fontStyle: FontStyle.normal,
42        fontWeight: FontWeight.normal,
43        fontSize: fontSize,
44      ));
45      builder.addText('Test Ahem');
46      final Paragraph paragraph = builder.build();
47      paragraph.layout(ParagraphConstraints(width: fontSize * 5.0));
48
49      expect(paragraph.height, closeTo(fontSize * 2.0, 0.001)); // because it wraps
50      expect(paragraph.width, closeTo(fontSize * 5.0, 0.001));
51      expect(paragraph.minIntrinsicWidth, closeTo(fontSize * 4.0, 0.001));
52
53      // TODO(yjbanov): see https://github.com/flutter/flutter/issues/21965
54      expect(paragraph.maxIntrinsicWidth, closeTo(fontSize * 9.0, 0.001));
55      expect(paragraph.alphabeticBaseline, closeTo(fontSize * .8, 0.001));
56      expect(
57        paragraph.ideographicBaseline,
58        closeTo(paragraph.alphabeticBaseline * kAhemBaselineRatio, 0.001),
59      );
60    }
61  });
62}
63