text_scaler_backward_compatibility_test.dart 9.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2014 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.

// TODO(LongCatIsLooong): Remove this file once textScaleFactor is removed.
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('TextStyle', () {
13
    test('getTextStyle is backward compatible', () {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      expect(
        const TextStyle(fontSize: 14).getTextStyle(textScaleFactor: 2.0).toString(),
        contains('fontSize: 28'),
      );
    }, skip: kIsWeb); // [intended] CkTextStyle doesn't have a custom toString implementation.
  });
  group('TextPainter', () {
    test('textScaleFactor translates to textScaler', () {
      final TextPainter textPainter = TextPainter(
        text: const TextSpan(text: 'text'),
        textDirection: TextDirection.ltr,
        textScaleFactor: 42,
      );

      expect(textPainter.textScaler, const TextScaler.linear(42.0));

      // Linear TextScaler translates to textScaleFactor.
      textPainter.textScaler = const TextScaler.linear(12.0);
      expect(textPainter.textScaleFactor, 12.0);

      textPainter.textScaleFactor = 10;
      expect(textPainter.textScaler, const TextScaler.linear(10));
    });
  });

  group('MediaQuery', () {
    test('specifying both textScaler and textScalingFactor asserts', () {
      expect(
        () => MediaQueryData(textScaleFactor: 2, textScaler: const TextScaler.linear(2.0)),
        throwsAssertionError,
      );
    });

    test('copyWith is backward compatible', () {
      const MediaQueryData data = MediaQueryData(textScaler: TextScaler.linear(2.0));

      final MediaQueryData data1 = data.copyWith(textScaleFactor: 42);
      expect(data1.textScaler, const TextScaler.linear(42));
      expect(data1.textScaleFactor, 42);

      final MediaQueryData data2 = data.copyWith(textScaler: TextScaler.noScaling);
      expect(data2.textScaler, TextScaler.noScaling);
      expect(data2.textScaleFactor, 1.0);
    });

    test('copyWith specifying both textScaler and textScalingFactor asserts', () {
      const MediaQueryData data = MediaQueryData();
       expect(
        () => data.copyWith(textScaleFactor: 2, textScaler: const TextScaler.linear(2.0)),
        throwsAssertionError,
      );
    });

67
    testWidgets('MediaQuery.textScaleFactorOf overriding compatibility', (WidgetTester tester) async {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      late final double outsideTextScaleFactor;
      late final TextScaler outsideTextScaler;
      late final double insideTextScaleFactor;
      late final TextScaler insideTextScaler;

      await tester.pumpWidget(
        Builder(
          builder: (BuildContext context) {
            outsideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
            outsideTextScaler = MediaQuery.textScalerOf(context);
            return MediaQuery(
              data: const MediaQueryData(
                textScaleFactor: 4.0,
              ),
              child: Builder(
                builder: (BuildContext context) {
                  insideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
                  insideTextScaler = MediaQuery.textScalerOf(context);
                  return Container();
                },
              ),
            );
          },
        ),
      );

      // Overriding textScaleFactor should work for unmigrated widgets that are
      // still using MediaQuery.textScaleFactorOf. Also if a unmigrated widget
      // overrides MediaQuery.textScaleFactor, migrated widgets in the subtree
      // should get the correct TextScaler.
      expect(outsideTextScaleFactor, 1.0);
      expect(outsideTextScaler.textScaleFactor, 1.0);
      expect(outsideTextScaler, TextScaler.noScaling);
      expect(insideTextScaleFactor, 4.0);
      expect(insideTextScaler.textScaleFactor, 4.0);
      expect(insideTextScaler, const TextScaler.linear(4.0));
    });

106
    testWidgets('textScaleFactor overriding backward compatibility', (WidgetTester tester) async {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      late final double outsideTextScaleFactor;
      late final TextScaler outsideTextScaler;
      late final double insideTextScaleFactor;
      late final TextScaler insideTextScaler;

      await tester.pumpWidget(
        Builder(
          builder: (BuildContext context) {
            outsideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
            outsideTextScaler = MediaQuery.textScalerOf(context);
            return MediaQuery(
              data: const MediaQueryData(textScaler: TextScaler.linear(4.0)),
              child: Builder(
                builder: (BuildContext context) {
                  insideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
                  insideTextScaler = MediaQuery.textScalerOf(context);
                  return Container();
                },
              ),
            );
          },
        ),
      );

      expect(outsideTextScaleFactor, 1.0);
      expect(outsideTextScaler.textScaleFactor, 1.0);
      expect(outsideTextScaler, TextScaler.noScaling);
      expect(insideTextScaleFactor, 4.0);
      expect(insideTextScaler.textScaleFactor, 4.0);
      expect(insideTextScaler, const TextScaler.linear(4.0));
    });
  });

  group('RenderObjects backward compatibility', () {
    test('RenderEditable', () {
      final RenderEditable renderObject = RenderEditable(
        backgroundCursorColor: const Color.fromARGB(0xFF, 0xFF, 0x00, 0x00),
        textDirection: TextDirection.ltr,
        cursorColor: const Color.fromARGB(0xFF, 0xFF, 0x00, 0x00),
        offset: ViewportOffset.zero(),
        textSelectionDelegate: _FakeEditableTextState(),
        text: const TextSpan(
          text: 'test',
          style: TextStyle(height: 1.0, fontSize: 10.0),
        ),
        startHandleLayerLink: LayerLink(),
        endHandleLayerLink: LayerLink(),
        selection: const TextSelection.collapsed(offset: 0),
      );
      expect(renderObject.textScaleFactor, 1.0);

      renderObject.textScaleFactor = 3.0;
      expect(renderObject.textScaleFactor, 3.0);
      expect(renderObject.textScaler, const TextScaler.linear(3.0));

      renderObject.textScaler = const TextScaler.linear(4.0);
      expect(renderObject.textScaleFactor, 4.0);
    });

    test('RenderParagraph', () {
      final RenderParagraph renderObject = RenderParagraph(
        const TextSpan(
          text: 'test',
          style: TextStyle(height: 1.0, fontSize: 10.0),
        ),
        textDirection: TextDirection.ltr,
      );
      expect(renderObject.textScaleFactor, 1.0);

      renderObject.textScaleFactor = 3.0;
      expect(renderObject.textScaleFactor, 3.0);
      expect(renderObject.textScaler, const TextScaler.linear(3.0));

      renderObject.textScaler = const TextScaler.linear(4.0);
      expect(renderObject.textScaleFactor, 4.0);
    });
  });

  group('Widgets backward compatibility', () {
186
    testWidgets('RichText', (WidgetTester tester) async {
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      await tester.pumpWidget(
        RichText(
          textDirection: TextDirection.ltr,
          text: const TextSpan(),
          textScaleFactor: 2.0,
        ),
      );

      expect(
        tester.renderObject<RenderParagraph>(find.byType(RichText)).textScaler,
        const TextScaler.linear(2.0),
      );
      expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).textScaleFactor, 2.0);
    });

202
    testWidgets('Text', (WidgetTester tester) async {
203 204 205 206 207 208 209 210 211 212 213 214 215 216
      await tester.pumpWidget(
        const Text(
          'text',
          textDirection: TextDirection.ltr,
          textScaleFactor: 2.0,
        ),
      );

      expect(
        tester.renderObject<RenderParagraph>(find.text('text')).textScaler,
        const TextScaler.linear(2.0),
      );
    });

217
    testWidgets('EditableText', (WidgetTester tester) async {
218
      final TextEditingController controller = TextEditingController();
219
      addTearDown(controller.dispose);
220
      final FocusNode focusNode = FocusNode(debugLabel: 'EditableText Node');
221
      addTearDown(focusNode.dispose);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      const TextStyle textStyle = TextStyle();
      const Color cursorColor = Color.fromARGB(0xFF, 0xFF, 0x00, 0x00);
      await tester.pumpWidget(
        MediaQuery(
          data: const MediaQueryData(),
          child: Directionality(
            textDirection: TextDirection.rtl,
            child: EditableText(
              backgroundCursorColor: cursorColor,
              controller: controller,
              focusNode: focusNode,
              style: textStyle,
              cursorColor: cursorColor,
              textScaleFactor: 2.0,
            ),
          ),
        ),
      );

      final RenderEditable renderEditable = tester.allRenderObjects.whereType<RenderEditable>().first;
      expect(
        renderEditable.textScaler,
        const TextScaler.linear(2.0),
      );
    });
  });
}

class _FakeEditableTextState with TextSelectionDelegate {
  @override
  TextEditingValue textEditingValue = TextEditingValue.empty;

  TextSelection? selection;

  @override
  void hideToolbar([bool hideHandles = true]) { }

  @override
  void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause cause) {
    selection = value.selection;
  }

  @override
  void bringIntoView(TextPosition position) { }

  @override
  void cutSelection(SelectionChangedCause cause) { }

  @override
  Future<void> pasteText(SelectionChangedCause cause) {
    return Future<void>.value();
  }

  @override
  void selectAll(SelectionChangedCause cause) { }

  @override
  void copySelection(SelectionChangedCause cause) { }
}