You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
meshcore-client/test/widgets/message_text_scale_test.dart

88 lines
2.9 KiB

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/widgets/translated_message_content.dart';
// #1277: a plain message rendered through flutter_linkify, which fixes its
// text scale at 1.0; a message with an @-mention rendered through Text.rich,
// which follows the system text size. On a phone with a non-default text
// size the two came out at different sizes.
/// A non-linear scaler, like Android 14 font scaling: +6 at every size.
class _PlusSix extends TextScaler {
const _PlusSix();
@override
double scale(double fontSize) => fontSize + 6;
@override
double get textScaleFactor => 1.5;
@override
bool operator ==(Object other) => other is _PlusSix;
@override
int get hashCode => 6;
}
const _fontSize = 16.0;
Future<void> _pump(WidgetTester tester, String text, TextScaler scaler) =>
tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: scaler),
child: Scaffold(
body: TranslatedMessageContent(
displayText: text,
style: const TextStyle(fontSize: _fontSize),
),
),
),
),
),
);
/// The size the plain-message widget will draw [_fontSize] at.
double _plainSize(WidgetTester tester) {
final linkify = find.byWidgetPredicate(
(w) => w is Linkify || w is SelectableLinkify,
);
final w = tester.widget(linkify);
final factor = w is Linkify
? w.textScaleFactor
: (w as SelectableLinkify).textScaleFactor;
return _fontSize * factor;
}
/// The size the mention-message paragraph draws [_fontSize] at.
double _mentionSize(WidgetTester tester) {
final paragraph = tester
.renderObjectList<RenderParagraph>(find.byType(RichText))
.firstWhere((p) => p.text.toPlainText().contains('hello'));
return paragraph.textScaler.scale(_fontSize);
}
void main() {
for (final scaler in [const TextScaler.linear(1.5), const _PlusSix()]) {
testWidgets('plain and mention messages scale alike ($scaler)', (
tester,
) async {
await _pump(tester, 'hello world', scaler);
final plain = _plainSize(tester);
await _pump(tester, '@[Bob] hello world', scaler);
final mention = _mentionSize(tester);
expect(mention, scaler.scale(_fontSize));
expect(plain, closeTo(mention, 0.001));
});
}
}

Powered by TurnKey Linux.