File size: 3,698 Bytes
b91a53d
 
 
 
 
 
9fc1476
b91a53d
 
 
 
 
 
 
 
 
 
 
 
9fc1476
b91a53d
 
 
 
 
 
 
 
 
 
 
 
 
9fc1476
b91a53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
67
68
69
70
71
72
73
74
75
76
77
78
// 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.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_api_samples/widgets/focus_scope/focus.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('shows initial content', (WidgetTester tester) async {
    await tester.pumpWidget(const example.FocusExampleApp());
    expect(find.byType(Scaffold), findsOneWidget);
    expect(find.byType(AppBar), findsOneWidget);
    expect(find.byType(FocusScope), findsAtLeastNWidgets(1));
    expect(find.text('Press to focus'), findsOneWidget);
    expect(find.text('Focus Sample'), findsOneWidget);
    expect(find.byType(Container), findsOneWidget);

    final Container container = tester.widget<Container>(find.byType(Container));
    expect(container.color, Colors.white);
  });

  testWidgets('switches to focus mode', (WidgetTester tester) async {
    await tester.pumpWidget(const example.FocusExampleApp());
    expect(find.text('Press to focus'), findsOneWidget);
    expect(find.text("I'm in color! Press R,G,B!"), findsNothing);
    await tester.tap(find.text('Press to focus'));
    await tester.pumpAndSettle();
    expect(find.text('Press to focus'), findsNothing);
    expect(find.text("I'm in color! Press R,G,B!"), findsOneWidget);

    expect(find.byType(Container), findsOneWidget);
    final Container container = tester.widget<Container>(find.byType(Container));
    expect(container.color, Colors.white);

    await tester.tap(find.text("I'm in color! Press R,G,B!"));
    await tester.pumpAndSettle();
    expect(find.text('Press to focus'), findsOneWidget);
    expect(find.text("I'm in color! Press R,G,B!"), findsNothing);
  });

  testWidgets('changes color according to key presses', (WidgetTester tester) async {
    await tester.pumpWidget(const example.FocusExampleApp());
    expect(find.byType(Container), findsOneWidget);
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.white);
    await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.white);
    await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.white);
    await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.white);

    expect(find.text('Press to focus'), findsOneWidget);
    await tester.tap(find.text('Press to focus'));
    await tester.pumpAndSettle();
    expect(find.byType(Container), findsOneWidget);
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.white);
    await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.red);
    await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.green);
    await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.blue);

    expect(find.text("I'm in color! Press R,G,B!"), findsOneWidget);
    await tester.tap(find.text("I'm in color! Press R,G,B!"));
    await tester.pumpAndSettle();
    expect(tester.widget<Container>(find.byType(Container)).color, Colors.white);
  });
}