binding_test.dart 5.36 KB
Newer Older
1 2 3 4 5 6 7 8
// 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 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
9 10
import 'package:integration_test/common.dart';
import 'package:integration_test/integration_test.dart';
11 12 13 14 15 16 17 18 19
import 'package:vm_service/vm_service.dart' as vm;

vm.Timeline _kTimelines = vm.Timeline(
  traceEvents: <vm.TimelineEvent>[],
  timeOriginMicros: 100,
  timeExtentMicros: 200,
);

Future<void> main() async {
20
  Future<Map<String, dynamic>>? request;
21 22

  group('Test Integration binding', () {
23
    final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
24

25
    FakeVM? fakeVM;
26 27

    setUp(() {
28
      request = binding.callback(<String, String>{
29 30
        'command': 'request_data',
      });
31 32
      fakeVM = FakeVM(
        timeline: _kTimelines,
33 34 35 36 37 38 39
      );
    });

    testWidgets('Run Integration app', (WidgetTester tester) async {
      runApp(const MaterialApp(
        home: Text('Test'),
      ));
40 41
      expect(tester.binding, binding);
      binding.reportData = <String, dynamic>{'answer': 42};
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
    testWidgets('hitTesting works when using setSurfaceSize', (WidgetTester tester) async {
      int invocations = 0;
      await tester.pumpWidget(
        MaterialApp(
          home: Center(
            child: GestureDetector(
              onTap: () {
                invocations++;
              },
              child: const Text('Test'),
            ),
          ),
        ),
      );

      await tester.tap(find.byType(Text));
      await tester.pump();
      expect(invocations, 1);

      await tester.binding.setSurfaceSize(const Size(200, 300));
      await tester.pump();
      await tester.tap(find.byType(Text));
      await tester.pump();
      expect(invocations, 2);

      await tester.binding.setSurfaceSize(null);
      await tester.pump();
      await tester.tap(find.byType(Text));
      await tester.pump();
      expect(invocations, 3);
    });

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
    testWidgets('setSurfaceSize works', (WidgetTester tester) async {
      await tester.pumpWidget(const MaterialApp(home: Center(child: Text('Test'))));

      final Size windowCenter = tester.binding.window.physicalSize /
          tester.binding.window.devicePixelRatio /
          2;
      final double windowCenterX = windowCenter.width;
      final double windowCenterY = windowCenter.height;

      Offset widgetCenter = tester.getRect(find.byType(Text)).center;
      expect(widgetCenter.dx, windowCenterX);
      expect(widgetCenter.dy, windowCenterY);

      await tester.binding.setSurfaceSize(const Size(200, 300));
      await tester.pump();
      widgetCenter = tester.getRect(find.byType(Text)).center;
      expect(widgetCenter.dx, 100);
      expect(widgetCenter.dy, 150);

      await tester.binding.setSurfaceSize(null);
      await tester.pump();
      widgetCenter = tester.getRect(find.byType(Text)).center;
      expect(widgetCenter.dx, windowCenterX);
      expect(widgetCenter.dy, windowCenterY);
    });

    testWidgets('Test traceAction', (WidgetTester tester) async {
103 104 105 106
      await binding.enableTimeline(vmService: fakeVM);
      await binding.traceAction(() async {});
      expect(binding.reportData, isNotNull);
      expect(binding.reportData!.containsKey('timeline'), true);
107
      expect(
108
        json.encode(binding.reportData!['timeline']),
109 110 111
        json.encode(_kTimelines),
      );
    });
112 113

    group('defaultTestTimeout', () {
114
      final Timeout originalTimeout = binding.defaultTestTimeout;
115
      tearDown(() {
116
        binding.defaultTestTimeout = originalTimeout;
117 118 119 120
      });

      test('can be configured', () {
        const Timeout newTimeout = Timeout(Duration(seconds: 17));
121 122
        binding.defaultTestTimeout = newTimeout;
        expect(binding.defaultTestTimeout, newTimeout);
123 124
      });
    });
125

126 127
    // TODO(jiahaog): Remove when https://github.com/flutter/flutter/issues/66006 is fixed.
    testWidgets('root widgets are wrapped with a RepaintBoundary', (WidgetTester tester) async {
128 129
      await tester.pumpWidget(const Placeholder());

130
      expect(find.byType(RepaintBoundary), findsOneWidget);
131
    });
132 133 134
  });

  tearDownAll(() async {
135
    // This part is outside the group so that `request` has been completed as
136
    // part of the `tearDownAll` registered in the group during
137 138
    // `IntegrationTestWidgetsFlutterBinding` initialization.
    final Map<String, dynamic> response =
139
        (await request)!['response'] as Map<String, dynamic>;
140 141
    final String message = response['message'] as String;
    final Response result = Response.fromJson(message);
142
    assert(result.data!['answer'] == 42);
143 144 145
  });
}

146
class FakeVM extends Fake implements vm.VmService {
147
  FakeVM({required this.timeline});
148 149 150 151

  vm.Timeline timeline;

  @override
152
  Future<vm.Timeline> getVMTimeline({int? timeOriginMicros, int? timeExtentMicros}) async {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    return timeline;
  }

  int lastTimeStamp = 0;
  @override
  Future<vm.Timestamp> getVMTimelineMicros() async {
    lastTimeStamp += 100;
    return vm.Timestamp(timestamp: lastTimeStamp);
  }

  List<String> recordedStreams = <String>[];
  @override
  Future<vm.Success> setVMTimelineFlags(List<String> recordedStreams) async {
    recordedStreams = recordedStreams;
    return vm.Success();
  }

  @override
  Future<vm.Success> clearVMTimeline() async {
    return vm.Success();
  }
}