widgets_test.dart 22.2 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 The Chromium Authors. All rights reserved.rint
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
8
import 'package:flutter_localizations/flutter_localizations.dart';
9 10 11 12 13 14 15 16

class TestLocalizations {
  TestLocalizations(this.locale, this.prefix);

  final Locale locale;
  final String prefix;

  static Future<TestLocalizations> loadSync(Locale locale, String prefix) {
17
    return SynchronousFuture<TestLocalizations>(TestLocalizations(locale, prefix));
18 19 20
  }

  static Future<TestLocalizations> loadAsync(Locale locale, String prefix) {
21 22
    return Future<TestLocalizations>.delayed(const Duration(milliseconds: 100))
      .then((_) => TestLocalizations(locale, prefix));
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  }

  static TestLocalizations of(BuildContext context) {
    return Localizations.of<TestLocalizations>(context, TestLocalizations);
  }

  String get message => '${prefix ?? ""}$locale';
}

class SyncTestLocalizationsDelegate extends LocalizationsDelegate<TestLocalizations> {
  SyncTestLocalizationsDelegate([this.prefix]);

  final String prefix; // Changing this value triggers a rebuild
  final List<bool> shouldReloadValues = <bool>[];

38 39 40
  @override
  bool isSupported(Locale locale) => true;

41 42 43 44 45 46 47 48
  @override
  Future<TestLocalizations> load(Locale locale) => TestLocalizations.loadSync(locale, prefix);

  @override
  bool shouldReload(SyncTestLocalizationsDelegate old) {
    shouldReloadValues.add(prefix != old.prefix);
    return prefix != old.prefix;
  }
49 50 51

  @override
  String toString() => '$runtimeType($prefix)';
52 53 54 55 56 57 58 59
}

class AsyncTestLocalizationsDelegate extends LocalizationsDelegate<TestLocalizations> {
  AsyncTestLocalizationsDelegate([this.prefix]);

  final String prefix; // Changing this value triggers a rebuild
  final List<bool> shouldReloadValues = <bool>[];

60 61 62
  @override
  bool isSupported(Locale locale) => true;

63 64 65 66 67 68 69 70
  @override
  Future<TestLocalizations> load(Locale locale) => TestLocalizations.loadAsync(locale, prefix);

  @override
  bool shouldReload(AsyncTestLocalizationsDelegate old) {
    shouldReloadValues.add(prefix != old.prefix);
    return prefix != old.prefix;
  }
71 72 73

  @override
  String toString() => '$runtimeType($prefix)';
74 75 76 77 78 79 80 81
}

class MoreLocalizations {
  MoreLocalizations(this.locale);

  final Locale locale;

  static Future<MoreLocalizations> loadSync(Locale locale) {
82
    return SynchronousFuture<MoreLocalizations>(MoreLocalizations(locale));
83 84 85
  }

  static Future<MoreLocalizations> loadAsync(Locale locale) {
86 87
    return Future<MoreLocalizations>.delayed(const Duration(milliseconds: 100))
      .then((_) => MoreLocalizations(locale));
88 89 90 91 92 93 94 95 96 97 98 99 100
  }

  static MoreLocalizations of(BuildContext context) {
    return Localizations.of<MoreLocalizations>(context, MoreLocalizations);
  }

  String get message => '$locale';
}

class SyncMoreLocalizationsDelegate extends LocalizationsDelegate<MoreLocalizations> {
  @override
  Future<MoreLocalizations> load(Locale locale) => MoreLocalizations.loadSync(locale);

101 102 103
  @override
  bool isSupported(Locale locale) => true;

104 105 106 107 108 109 110 111
  @override
  bool shouldReload(SyncMoreLocalizationsDelegate old) => false;
}

class AsyncMoreLocalizationsDelegate extends LocalizationsDelegate<MoreLocalizations> {
  @override
  Future<MoreLocalizations> load(Locale locale) => MoreLocalizations.loadAsync(locale);

112 113 114
  @override
  bool isSupported(Locale locale) => true;

115 116 117 118
  @override
  bool shouldReload(AsyncMoreLocalizationsDelegate old) => false;
}

119 120 121 122 123 124 125 126
class OnlyRTLDefaultWidgetsLocalizations extends DefaultWidgetsLocalizations {
  @override
  TextDirection get textDirection => TextDirection.rtl;
}

class OnlyRTLDefaultWidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
  const OnlyRTLDefaultWidgetsLocalizationsDelegate();

127 128 129
  @override
  bool isSupported(Locale locale) => true;

130 131
  @override
  Future<WidgetsLocalizations> load(Locale locale) {
132
    return SynchronousFuture<WidgetsLocalizations>(OnlyRTLDefaultWidgetsLocalizations());
133 134 135 136 137 138
  }

  @override
  bool shouldReload(OnlyRTLDefaultWidgetsLocalizationsDelegate old) => false;
}

139 140 141 142
Widget buildFrame({
  Locale locale,
  Iterable<LocalizationsDelegate<dynamic>> delegates,
  WidgetBuilder buildContent,
143
  LocaleResolutionCallback localeResolutionCallback,
144
  List<Locale> supportedLocales = const <Locale>[
145 146
    Locale('en', 'US'),
    Locale('en', 'GB'),
147
  ],
148
}) {
149
  return WidgetsApp(
150 151 152
    color: const Color(0xFFFFFFFF),
    locale: locale,
    localizationsDelegates: delegates,
153 154
    localeResolutionCallback: localeResolutionCallback,
    supportedLocales: supportedLocales,
155
    onGenerateRoute: (RouteSettings settings) {
156
      return PageRouteBuilder<void>(
157 158 159 160 161 162 163 164
        pageBuilder: (BuildContext context, Animation<double> _, Animation<double> __) {
          return buildContent(context);
        }
      );
    },
  );
}

165 166 167 168
class SyncLoadTest extends StatefulWidget {
  const SyncLoadTest();

  @override
169
  SyncLoadTestState createState() => SyncLoadTestState();
170 171 172 173 174
}

class SyncLoadTestState extends State<SyncLoadTest> {
  @override
  Widget build(BuildContext context) {
175
    return Text(
176 177 178 179 180 181
      TestLocalizations.of(context).message,
      textDirection: TextDirection.rtl,
    );
  }
}

182 183 184 185 186 187 188 189
void main() {
  testWidgets('Localizations.localeFor in a WidgetsApp with system locale', (WidgetTester tester) async {
    BuildContext pageContext;

    await tester.pumpWidget(
      buildFrame(
        buildContent: (BuildContext context) {
          pageContext = context;
Ian Hickson's avatar
Ian Hickson committed
190
          return const Text('Hello World', textDirection: TextDirection.ltr);
191 192 193 194 195 196 197 198 199 200 201 202 203 204
        }
      )
    );

    await tester.binding.setLocale('en', 'GB');
    await tester.pump();
    expect(Localizations.localeOf(pageContext), const Locale('en', 'GB'));

    await tester.binding.setLocale('en', 'US');
    await tester.pump();
    expect(Localizations.localeOf(pageContext), const Locale('en', 'US'));
  });

  testWidgets('Localizations.localeFor in a WidgetsApp with an explicit locale', (WidgetTester tester) async {
205
    const Locale locale = Locale('en', 'US');
206 207 208 209 210 211 212
    BuildContext pageContext;

    await tester.pumpWidget(
      buildFrame(
        locale: locale,
        buildContent: (BuildContext context) {
          pageContext = context;
213
          return const Text('Hello World');
214 215 216 217 218 219 220 221 222 223 224 225 226 227
        },
      )
    );

    expect(Localizations.localeOf(pageContext), locale);

    await tester.binding.setLocale('en', 'GB');
    await tester.pump();

    // The WidgetApp's explicit locale overrides the system's locale.
    expect(Localizations.localeOf(pageContext), locale);
  });

  testWidgets('Synchronously loaded localizations in a WidgetsApp', (WidgetTester tester) async {
228
    final List<LocalizationsDelegate<dynamic>> delegates = <LocalizationsDelegate<dynamic>>[
229
      SyncTestLocalizationsDelegate(),
230
      DefaultWidgetsLocalizations.delegate,
231 232
    ];

233
    Future<void> pumpTest(Locale locale) async {
234
      await tester.pumpWidget(Localizations(
235 236 237 238 239
        locale: locale,
        delegates: delegates,
        child: const SyncLoadTest(),
      ));
    }
240

241
    await pumpTest(const Locale('en', 'US'));
242
    expect(find.text('en_US'), findsOneWidget);
243

244
    await pumpTest(const Locale('en', 'GB'));
245 246 247
    await tester.pump();
    expect(find.text('en_GB'), findsOneWidget);

248
    await pumpTest(const Locale('en', 'US'));
249 250 251 252 253 254 255 256
    await tester.pump();
    expect(find.text('en_US'), findsOneWidget);
  });

  testWidgets('Asynchronously loaded localizations in a WidgetsApp', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
257
          AsyncTestLocalizationsDelegate(),
258 259
        ],
        buildContent: (BuildContext context) {
260
          return Text(TestLocalizations.of(context).message);
261 262 263 264
        }
      )
    );
    await tester.pump(const Duration(milliseconds: 50)); // TestLocalizations.loadAsync() takes 100ms
265
    expect(find.text('en_US'), findsNothing); // TestLocalizations hasn't been loaded yet
266 267 268

    await tester.pump(const Duration(milliseconds: 50)); // TestLocalizations.loadAsync() completes
    await tester.pumpAndSettle();
269
    expect(find.text('en_US'), findsOneWidget); // default test locale is US english
270

271
    await tester.binding.setLocale('en', 'GB');
272 273
    await tester.pump(const Duration(milliseconds: 100));
    await tester.pumpAndSettle();
274
    expect(find.text('en_GB'), findsOneWidget);
275

276
    await tester.binding.setLocale('en', 'US');
277 278 279
    await tester.pump(const Duration(milliseconds: 50));
    // TestLocalizations.loadAsync() hasn't completed yet so the old text
    // localization is still displayed
280
    expect(find.text('en_GB'), findsOneWidget);
281 282
    await tester.pump(const Duration(milliseconds: 50)); // finish the async load
    await tester.pumpAndSettle();
283
    expect(find.text('en_US'), findsOneWidget);
284 285 286 287 288 289
  });

  testWidgets('Localizations with multiple sync delegates', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
290 291
          SyncTestLocalizationsDelegate(),
          SyncMoreLocalizationsDelegate(),
292 293 294
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
295
          return Column(
296
            children: <Widget>[
297 298
              Text('A: ${TestLocalizations.of(context).message}'),
              Text('B: ${MoreLocalizations.of(context).message}'),
299 300 301 302 303 304
            ],
          );
        }
      )
    );

Josh Soref's avatar
Josh Soref committed
305
    // All localizations were loaded synchronously
306 307 308 309 310 311 312 313
    expect(find.text('A: en_US'), findsOneWidget);
    expect(find.text('B: en_US'), findsOneWidget);
  });

  testWidgets('Localizations with multiple delegates', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
314 315
          SyncTestLocalizationsDelegate(),
          AsyncMoreLocalizationsDelegate(), // No resources until this completes
316 317 318
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
319
          return Column(
320
            children: <Widget>[
321 322
              Text('A: ${TestLocalizations.of(context).message}'),
              Text('B: ${MoreLocalizations.of(context).message}'),
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            ],
          );
        }
      )
    );

    await tester.pump(const Duration(milliseconds: 50));
    expect(find.text('A: en_US'), findsNothing); // MoreLocalizations.load() hasn't completed yet
    expect(find.text('B: en_US'), findsNothing);

    await tester.pump(const Duration(milliseconds: 50));
    await tester.pumpAndSettle();

    expect(find.text('A: en_US'), findsOneWidget);
    expect(find.text('B: en_US'), findsOneWidget);
  });

Josh Soref's avatar
Josh Soref committed
340
  testWidgets('Multiple Localizations', (WidgetTester tester) async {
341 342 343
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
344
          SyncTestLocalizationsDelegate(),
345 346 347
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
348
          return Column(
349
            children: <Widget>[
350 351
              Text('A: ${TestLocalizations.of(context).message}'),
              Localizations(
352 353
                locale: const Locale('en', 'GB'),
                delegates: <LocalizationsDelegate<dynamic>>[
354
                  SyncTestLocalizationsDelegate(),
355
                  DefaultWidgetsLocalizations.delegate,
356 357
                ],
                // Create a new context within the en_GB Localization
358
                child: Builder(
359
                  builder: (BuildContext context) {
360
                    return Text('B: ${TestLocalizations.of(context).message}');
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
                  },
                ),
              ),
            ],
          );
        }
      )
    );

    expect(find.text('A: en_US'), findsOneWidget);
    expect(find.text('B: en_GB'), findsOneWidget);
  });

  // If both the locale and the length and type of a Localizations delegate list
  // stays the same BUT one of its delegate.shouldReload() methods returns true,
  // then the dependent widgets should rebuild.
  testWidgets('Localizations sync delegate shouldReload returns true', (WidgetTester tester) async {
378
    final SyncTestLocalizationsDelegate originalDelegate = SyncTestLocalizationsDelegate();
379 380 381 382
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
          originalDelegate,
383
          SyncMoreLocalizationsDelegate(),
384 385 386
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
387
          return Column(
388
            children: <Widget>[
389 390
              Text('A: ${TestLocalizations.of(context).message}'),
              Text('B: ${MoreLocalizations.of(context).message}'),
391 392 393 394 395 396 397 398 399 400 401 402
            ],
          );
        }
      )
    );

    await tester.pumpAndSettle();
    expect(find.text('A: en_US'), findsOneWidget);
    expect(find.text('B: en_US'), findsOneWidget);
    expect(originalDelegate.shouldReloadValues, <bool>[]);


403
    final SyncTestLocalizationsDelegate modifiedDelegate = SyncTestLocalizationsDelegate('---');
404 405 406 407
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
          modifiedDelegate,
408
          SyncMoreLocalizationsDelegate(),
409 410 411
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
412
          return Column(
413
            children: <Widget>[
414 415
              Text('A: ${TestLocalizations.of(context).message}'),
              Text('B: ${MoreLocalizations.of(context).message}'),
416 417 418 419 420 421 422 423 424 425
            ],
          );
        }
      )
    );

    await tester.pumpAndSettle();
    expect(find.text('A: ---en_US'), findsOneWidget);
    expect(find.text('B: en_US'), findsOneWidget);
    expect(modifiedDelegate.shouldReloadValues, <bool>[true]);
426
    expect(originalDelegate.shouldReloadValues, <bool>[]);
427 428 429 430 431 432
  });

  testWidgets('Localizations async delegate shouldReload returns true', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
433 434
          AsyncTestLocalizationsDelegate(),
          AsyncMoreLocalizationsDelegate(),
435 436 437
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
438
          return Column(
439
            children: <Widget>[
440 441
              Text('A: ${TestLocalizations.of(context).message}'),
              Text('B: ${MoreLocalizations.of(context).message}'),
442 443 444 445 446 447 448 449 450 451
            ],
          );
        }
      )
    );

    await tester.pumpAndSettle();
    expect(find.text('A: en_US'), findsOneWidget);
    expect(find.text('B: en_US'), findsOneWidget);

452
    final AsyncTestLocalizationsDelegate modifiedDelegate = AsyncTestLocalizationsDelegate('---');
453 454 455 456
    await tester.pumpWidget(
      buildFrame(
        delegates: <LocalizationsDelegate<dynamic>>[
          modifiedDelegate,
457
          AsyncMoreLocalizationsDelegate(),
458 459 460
        ],
        locale: const Locale('en', 'US'),
        buildContent: (BuildContext context) {
461
          return Column(
462
            children: <Widget>[
463 464
              Text('A: ${TestLocalizations.of(context).message}'),
              Text('B: ${MoreLocalizations.of(context).message}'),
465 466 467 468 469 470 471 472 473 474 475 476
            ],
          );
        }
      )
    );

    await tester.pumpAndSettle();
    expect(find.text('A: ---en_US'), findsOneWidget);
    expect(find.text('B: en_US'), findsOneWidget);
    expect(modifiedDelegate.shouldReloadValues, <bool>[true]);
  });

477 478 479 480 481
  testWidgets('Directionality tracks system locale', (WidgetTester tester) async {
    BuildContext pageContext;

    await tester.pumpWidget(
      buildFrame(
482 483 484
        delegates: const <LocalizationsDelegate<dynamic>>[
          GlobalWidgetsLocalizations.delegate,
        ],
485
        supportedLocales: const <Locale>[
486 487
          Locale('en', 'GB'),
          Locale('ar', 'EG'),
488
        ],
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
        buildContent: (BuildContext context) {
          pageContext = context;
          return const Text('Hello World');
        }
      )
    );

    await tester.binding.setLocale('en', 'GB');
    await tester.pump();
    expect(Directionality.of(pageContext), TextDirection.ltr);

    await tester.binding.setLocale('ar', 'EG');
    await tester.pump();
    expect(Directionality.of(pageContext), TextDirection.rtl);
  });
504 505 506 507 508 509 510 511

  testWidgets('localeResolutionCallback override', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        localeResolutionCallback: (Locale newLocale, Iterable<Locale> supportedLocales) {
          return const Locale('foo', 'BAR');
        },
        buildContent: (BuildContext context) {
512
          return Text(Localizations.localeOf(context).toString());
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
        }
      )
    );

    await tester.pumpAndSettle();
    expect(find.text('foo_BAR'), findsOneWidget);

    await tester.binding.setLocale('en', 'GB');
    await tester.pumpAndSettle();
    expect(find.text('foo_BAR'), findsOneWidget);
  });


  testWidgets('supportedLocales and defaultLocaleChangeHandler', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        supportedLocales: const <Locale>[
530 531 532
          Locale('zh', 'CN'),
          Locale('en', 'GB'),
          Locale('en', 'CA'),
533 534
        ],
        buildContent: (BuildContext context) {
535
          return Text(Localizations.localeOf(context).toString());
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        }
      )
    );

    // Startup time. Default test locale is const Locale('', ''), so
    // no supported matches. Use the first locale.
    await tester.pumpAndSettle();
    expect(find.text('zh_CN'), findsOneWidget);

    // defaultLocaleChangedHandler prefers exact supported locale match
    await tester.binding.setLocale('en', 'CA');
    await tester.pumpAndSettle();
    expect(find.text('en_CA'), findsOneWidget);

    // defaultLocaleChangedHandler chooses 1st matching supported locale.languageCode
    await tester.binding.setLocale('en', 'US');
    await tester.pumpAndSettle();
    expect(find.text('en_GB'), findsOneWidget);

    // defaultLocaleChangedHandler: no matching supported locale, so use the 1st one
    await tester.binding.setLocale('da', 'DA');
    await tester.pumpAndSettle();
    expect(find.text('zh_CN'), findsOneWidget);
  });
560

561 562 563 564 565
  testWidgets('Localizations.override widget tracks parent\'s locale and delegates', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        // Accept whatever locale we're given
        localeResolutionCallback: (Locale locale, Iterable<Locale> supportedLocales) => locale,
566 567 568
        delegates: const <LocalizationsDelegate<dynamic>>[
          GlobalWidgetsLocalizations.delegate,
        ],
569
        buildContent: (BuildContext context) {
570
          return Localizations.override(
571
            context: context,
572
            child: Builder(
573 574 575
              builder: (BuildContext context) {
                final Locale locale = Localizations.localeOf(context);
                final TextDirection direction = WidgetsLocalizations.of(context).textDirection;
576
                return Text('$locale $direction');
577 578 579 580 581 582
              },
            ),
          );
        }
      )
    );
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    // Initial WidgetTester locale is new Locale('', '')
    await tester.pumpAndSettle();
    expect(find.text('_ TextDirection.ltr'), findsOneWidget);

    await tester.binding.setLocale('en', 'CA');
    await tester.pumpAndSettle();
    expect(find.text('en_CA TextDirection.ltr'), findsOneWidget);

    await tester.binding.setLocale('ar', 'EG');
    await tester.pumpAndSettle();
    expect(find.text('ar_EG TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('da', 'DA');
    await tester.pumpAndSettle();
    expect(find.text('da_DA TextDirection.ltr'), findsOneWidget);
  });

  testWidgets('Localizations.override widget overrides parent\'s DefaultWidgetLocalizations', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        // Accept whatever locale we're given
        localeResolutionCallback: (Locale locale, Iterable<Locale> supportedLocales) => locale,
        buildContent: (BuildContext context) {
607
          return Localizations.override(
608
            context: context,
609
            delegates: const <OnlyRTLDefaultWidgetsLocalizationsDelegate>[
610
              // Override: no matter what the locale, textDirection is always RTL.
611
              OnlyRTLDefaultWidgetsLocalizationsDelegate(),
612
            ],
613
            child: Builder(
614 615 616
              builder: (BuildContext context) {
                final Locale locale = Localizations.localeOf(context);
                final TextDirection direction = WidgetsLocalizations.of(context).textDirection;
617
                return Text('$locale $direction');
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
              },
            ),
          );
        }
      )
    );

    // Initial WidgetTester locale is new Locale('', '')
    await tester.pumpAndSettle();
    expect(find.text('_ TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('en', 'CA');
    await tester.pumpAndSettle();
    expect(find.text('en_CA TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('ar', 'EG');
    await tester.pumpAndSettle();
    expect(find.text('ar_EG TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('da', 'DA');
    await tester.pumpAndSettle();
    expect(find.text('da_DA TextDirection.rtl'), findsOneWidget);
  });

  testWidgets('WidgetsApp overrides DefaultWidgetLocalizations', (WidgetTester tester) async {
    await tester.pumpWidget(
      buildFrame(
        // Accept whatever locale we're given
        localeResolutionCallback: (Locale locale, Iterable<Locale> supportedLocales) => locale,
        delegates: <OnlyRTLDefaultWidgetsLocalizationsDelegate>[
          const OnlyRTLDefaultWidgetsLocalizationsDelegate(),
        ],
        buildContent: (BuildContext context) {
          final Locale locale = Localizations.localeOf(context);
          final TextDirection direction = WidgetsLocalizations.of(context).textDirection;
653
          return Text('$locale $direction');
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
        }
      )
    );

    // Initial WidgetTester locale is new Locale('', '')
    await tester.pumpAndSettle();
    expect(find.text('_ TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('en', 'CA');
    await tester.pumpAndSettle();
    expect(find.text('en_CA TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('ar', 'EG');
    await tester.pumpAndSettle();
    expect(find.text('ar_EG TextDirection.rtl'), findsOneWidget);

    await tester.binding.setLocale('da', 'DA');
    await tester.pumpAndSettle();
    expect(find.text('da_DA TextDirection.rtl'), findsOneWidget);
  });
674 675

}