allow ParagraphBuilder.shouldDisableRoundingHack to actually be set to false in tests (flutter/engine#44647)

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
LongCatIsLooong 2023-08-11 16:04:57 -07:00 committed by GitHub
parent a9978d9c83
commit eb3946ff27
5 changed files with 46 additions and 54 deletions

View File

@ -3029,21 +3029,13 @@ abstract class ParagraphBuilder {
@Deprecated('''
The shouldDisableRoundingHack flag is for internal migration purposes only and should not be used.
''')
static bool get shouldDisableRoundingHack {
return const bool.fromEnvironment('SKPARAGRAPH_REMOVE_ROUNDING_HACK', defaultValue: true)
|| _roundingHackDisabledInDebugMode;
}
static bool _roundingHackDisabledInDebugMode = true;
/// Only works in debug mode. Do not call this method as it is for migration
/// purposes only and will soon be removed.
static bool get shouldDisableRoundingHack => _shouldDisableRoundingHack;
static bool _shouldDisableRoundingHack = true;
/// Do not call this method as it is for migration purposes only and will soon
/// be removed.
// ignore: use_setters_to_change_properties
static void setDisableRoundingHack(bool disableRoundingHack) {
// bool.hasEnvironment does not work in internal tests so an additional flag
// is needed for tests.
assert(() {
_roundingHackDisabledInDebugMode = disableRoundingHack;
return true;
}());
_shouldDisableRoundingHack = disableRoundingHack;
}
/// The number of placeholders currently in the paragraph.

View File

@ -686,16 +686,11 @@ abstract class ParagraphBuilder {
factory ParagraphBuilder(ParagraphStyle style) =>
engine.renderer.createParagraphBuilder(style);
static bool get shouldDisableRoundingHack {
return const bool.fromEnvironment('SKPARAGRAPH_REMOVE_ROUNDING_HACK', defaultValue: true)
|| _roundingHackDisabledInDebugMode;
}
static bool _roundingHackDisabledInDebugMode = true;
static bool get shouldDisableRoundingHack => _shouldDisableRoundingHack;
static bool _shouldDisableRoundingHack = true;
// ignore: use_setters_to_change_properties
static void setDisableRoundingHack(bool disableRoundingHack) {
assert(() {
_roundingHackDisabledInDebugMode = disableRoundingHack;
return true;
}());
_shouldDisableRoundingHack = disableRoundingHack;
}
void pushStyle(TextStyle style);

View File

@ -124,21 +124,12 @@ void testMain() {
});
});
test('applyRoundingHack works', () {
bool assertsEnabled = false;
assert(() {
assertsEnabled = true;
return true;
}());
if (!assertsEnabled){
return;
}
test('rounding hack disabled by default', () {
expect(ui.ParagraphBuilder.shouldDisableRoundingHack, isTrue);
const double fontSize = 1.25;
const String text = '12345';
assert((fontSize * text.length).truncate() != fontSize * text.length);
final bool roundingHackWasDisabled = ui.ParagraphBuilder.shouldDisableRoundingHack;
ui.ParagraphBuilder.setDisableRoundingHack(true);
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(
ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'),
);
@ -153,19 +144,32 @@ void testMain() {
case final List<ui.LineMetrics> metrics:
expect(metrics, hasLength(1));
}
ui.ParagraphBuilder.setDisableRoundingHack(roundingHackWasDisabled);
});
test('rounding hack disabled by default', () {
test('setDisableRoundinghHack to false works in tests', () {
bool assertsEnabled = false;
assert(() {
assertsEnabled = true;
return true;
}());
if (!assertsEnabled){
return;
}
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
ui.ParagraphBuilder.setDisableRoundingHack(false);
addTearDown(() => ui.ParagraphBuilder.setDisableRoundingHack(true));
}
assert(!ui.ParagraphBuilder.shouldDisableRoundingHack);
const double fontSize = 1.25;
const String text = '12345';
assert((fontSize * text.length).truncate() != fontSize * text.length);
expect(ui.ParagraphBuilder.shouldDisableRoundingHack, isTrue);
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'));
builder.addText(text);
final ui.Paragraph paragraph = builder.build()
..layout(const ui.ParagraphConstraints(width: text.length * fontSize));
expect(paragraph.computeLineMetrics().length, 1);
expect(paragraph.computeLineMetrics().length, greaterThan(1));
});
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/71520

View File

@ -778,17 +778,12 @@ Future<void> testMain() async {
test('$CanvasParagraph.width should be a whole integer when shouldDisableRoundingHack is false', () {
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
// Try applying the rounding hack if it's disabled. This may not work if
// the 'SKPARAGRAPH_REMOVE_ROUNDING_HACK' dart environment declaration
// is set to 'false'.
ui.ParagraphBuilder.setDisableRoundingHack(false);
addTearDown(() => ui.ParagraphBuilder.setDisableRoundingHack(true));
}
// The paragraph width is only rounded to a whole integer if
// shouldDisableRoundingHack is false.
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
return;
}
assert(!ui.ParagraphBuilder.shouldDisableRoundingHack);
final ui.Paragraph paragraph = plain(ahemStyle, 'abc');
paragraph.layout(const ui.ParagraphConstraints(width: 30.8));

View File

@ -234,7 +234,7 @@ void main() {
}
});
test('disableRoundingHack works in tests', () {
test('can set disableRoundingHack to false in tests', () {
bool assertsEnabled = false;
assert(() {
assertsEnabled = true;
@ -248,20 +248,20 @@ void main() {
assert((fontSize * text.length).truncate() != fontSize * text.length);
// ignore: deprecated_member_use
final bool roundingHackWasDisabled = ParagraphBuilder.shouldDisableRoundingHack;
ParagraphBuilder.setDisableRoundingHack(true);
if (roundingHackWasDisabled) {
ParagraphBuilder.setDisableRoundingHack(false);
}
// ignore: deprecated_member_use
assert(!ParagraphBuilder.shouldDisableRoundingHack);
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(fontSize: fontSize));
builder.addText(text);
final Paragraph paragraph = builder.build()
..layout(const ParagraphConstraints(width: text.length * fontSize));
expect(paragraph.computeLineMetrics().length, greaterThan(1));
expect(paragraph.maxIntrinsicWidth, text.length * fontSize);
switch (paragraph.computeLineMetrics()) {
case [LineMetrics(width: final double width)]:
expect(width, text.length * fontSize);
case final List<LineMetrics> metrics:
expect(metrics, hasLength(1));
if (roundingHackWasDisabled) {
ParagraphBuilder.setDisableRoundingHack(true);
}
ParagraphBuilder.setDisableRoundingHack(roundingHackWasDisabled);
});
test('rounding hack disabled by default', () {
@ -274,6 +274,12 @@ void main() {
builder.addText(text);
final Paragraph paragraph = builder.build()
..layout(const ParagraphConstraints(width: text.length * fontSize));
expect(paragraph.computeLineMetrics().length, 1);
expect(paragraph.maxIntrinsicWidth, text.length * fontSize);
switch (paragraph.computeLineMetrics()) {
case [LineMetrics(width: final double width)]:
expect(width, text.length * fontSize);
case final List<LineMetrics> metrics:
expect(metrics, hasLength(1));
}
});
}