|
| 1 | +// Copyright 2023, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:firebase_ui_auth/firebase_ui_auth.dart'; |
| 6 | +import 'package:flutter/gestures.dart'; |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter/rendering.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +import '../../test_utils.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + group("$LoginView", () { |
| 15 | + testWidgets( |
| 16 | + 'rebuilds the footer when the $AuthAction changes', |
| 17 | + (tester) async { |
| 18 | + const signInKey = ValueKey("Sign in"); |
| 19 | + const registerKey = ValueKey("Register"); |
| 20 | + |
| 21 | + const widgetSignIn = SizedBox(key: signInKey); |
| 22 | + const widgetRegister = SizedBox(key: registerKey); |
| 23 | + |
| 24 | + final view = TestMaterialApp( |
| 25 | + child: LoginView( |
| 26 | + providers: [EmailAuthProvider()], |
| 27 | + auth: MockAuth(), |
| 28 | + action: AuthAction.signIn, |
| 29 | + showAuthActionSwitch: true, |
| 30 | + footerBuilder: (context, action) { |
| 31 | + switch (action) { |
| 32 | + case AuthAction.signIn: |
| 33 | + return widgetSignIn; |
| 34 | + case AuthAction.signUp: |
| 35 | + return widgetRegister; |
| 36 | + default: |
| 37 | + return const SizedBox(); |
| 38 | + } |
| 39 | + }, |
| 40 | + ), |
| 41 | + ); |
| 42 | + await tester.pumpWidget(view); |
| 43 | + |
| 44 | + expect(find.byKey(signInKey), findsOneWidget); |
| 45 | + expect(find.byKey(registerKey), findsNothing); |
| 46 | + |
| 47 | + await _tapOnRegisterActionText(tester); |
| 48 | + |
| 49 | + expect(find.byKey(signInKey), findsNothing); |
| 50 | + expect(find.byKey(registerKey), findsOneWidget); |
| 51 | + }, |
| 52 | + ); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +/// Taps on the Register text in the action switch of the [LoginView]. |
| 57 | +/// |
| 58 | +/// We have to do this because the [WidgetTester] API does not enable the search |
| 59 | +/// for a particular TextSpan. |
| 60 | +/// See https://stackoverflow.com/a/60247474/19586032 for more details. |
| 61 | +Future<void> _tapOnRegisterActionText(WidgetTester tester) async { |
| 62 | + final actionSwitchRichText = find |
| 63 | + .byWidgetPredicate( |
| 64 | + (widget) => |
| 65 | + widget is RichText && |
| 66 | + widget.text.toPlainText().contains("Register"), |
| 67 | + ) |
| 68 | + .evaluate() |
| 69 | + .first |
| 70 | + .renderObject! as RenderParagraph; |
| 71 | + |
| 72 | + actionSwitchRichText.text.visitChildren((InlineSpan span) { |
| 73 | + if (span is! TextSpan) return true; |
| 74 | + |
| 75 | + span.visitChildren((InlineSpan span) { |
| 76 | + if (span is! TextSpan) return true; |
| 77 | + |
| 78 | + if (span.text != 'Register') return true; |
| 79 | + |
| 80 | + (span.recognizer! as TapGestureRecognizer).onTap!(); |
| 81 | + return false; |
| 82 | + }); |
| 83 | + |
| 84 | + return true; |
| 85 | + }); |
| 86 | + |
| 87 | + await tester.pumpAndSettle(); |
| 88 | +} |
0 commit comments