blob: a0d0e4b8ccc1fbfd0bce1fb0a4fa5f7f578391d0 [file] [log] [blame]
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.12
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart';
void main() {
internalBootstrapBrowserTest(() => testMain);
}
void testMain() {
setUpAll(() {
WebExperiments.ensureInitialized();
});
// TODO(mdebbar): Add checks for the output of `toDomElement()` in all the
// tests below.
test('Builds a text-only canvas paragraph', () {
final EngineParagraphStyle style = EngineParagraphStyle(fontSize: 13.0);
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
builder.addText('Hello');
final CanvasParagraph paragraph = builder.build();
expect(paragraph.paragraphStyle, style);
expect(paragraph.toPlainText(), 'Hello');
expect(paragraph.spans, hasLength(1));
final ParagraphSpan span = paragraph.spans.single;
expect(span, isA<FlatTextSpan>());
final FlatTextSpan textSpan = span as FlatTextSpan;
expect(textSpan.textOf(paragraph), 'Hello');
expect(textSpan.style, TextStyle(fontSize: 13.0));
});
test('Builds a single-span paragraph with complex styles', () {
final EngineParagraphStyle style =
EngineParagraphStyle(fontSize: 13.0, height: 1.5);
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
builder.pushStyle(TextStyle(fontSize: 9.0));
builder.pushStyle(TextStyle(fontWeight: FontWeight.bold));
builder.pushStyle(TextStyle(fontSize: 40.0));
builder.pop();
builder
.pushStyle(TextStyle(fontStyle: FontStyle.italic, letterSpacing: 2.0));
builder.addText('Hello');
final CanvasParagraph paragraph = builder.build();
expect(paragraph.toPlainText(), 'Hello');
expect(paragraph.spans, hasLength(1));
final FlatTextSpan span = paragraph.spans.single as FlatTextSpan;
expect(span.textOf(paragraph), 'Hello');
expect(
span.style,
TextStyle(
height: 1.5,
fontSize: 9.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
letterSpacing: 2.0,
),
);
});
test('Builds a multi-span paragraph', () {
final EngineParagraphStyle style = EngineParagraphStyle(fontSize: 13.0);
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
builder.pushStyle(TextStyle(fontWeight: FontWeight.bold));
builder.addText('Hello');
builder.pop();
builder.pushStyle(TextStyle(fontStyle: FontStyle.italic));
builder.addText(' world');
final CanvasParagraph paragraph = builder.build();
expect(paragraph.toPlainText(), 'Hello world');
expect(paragraph.spans, hasLength(2));
final FlatTextSpan hello = paragraph.spans.first as FlatTextSpan;
expect(hello.textOf(paragraph), 'Hello');
expect(
hello.style,
TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.bold,
),
);
final FlatTextSpan world = paragraph.spans.last as FlatTextSpan;
expect(world.textOf(paragraph), ' world');
expect(
world.style,
TextStyle(
fontSize: 13.0,
fontStyle: FontStyle.italic,
),
);
});
test('Builds a multi-span paragraph with complex styles', () {
final EngineParagraphStyle style = EngineParagraphStyle(fontSize: 13.0);
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
builder.pushStyle(TextStyle(fontWeight: FontWeight.bold));
builder.pushStyle(TextStyle(height: 2.0));
builder.addText('Hello');
builder.pop(); // pop TextStyle(height: 2.0).
builder.pushStyle(TextStyle(fontStyle: FontStyle.italic));
builder.addText(' world');
builder.pushStyle(TextStyle(fontWeight: FontWeight.normal));
builder.addText('!');
final CanvasParagraph paragraph = builder.build();
expect(paragraph.toPlainText(), 'Hello world!');
expect(paragraph.spans, hasLength(3));
final FlatTextSpan hello = paragraph.spans[0] as FlatTextSpan;
expect(hello.textOf(paragraph), 'Hello');
expect(
hello.style,
TextStyle(fontSize: 13.0, fontWeight: FontWeight.bold, height: 2.0),
);
final FlatTextSpan world = paragraph.spans[1] as FlatTextSpan;
expect(world.textOf(paragraph), ' world');
expect(
world.style,
TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
),
);
final FlatTextSpan bang = paragraph.spans[2] as FlatTextSpan;
expect(bang.textOf(paragraph), '!');
expect(
bang.style,
TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.normal,
fontStyle: FontStyle.italic,
),
);
});
}