blob: bc02524475fb8486d43133c6103730da01ae4c48 [file] [log] [blame]
Dan Field76784652020-11-05 17:28:47 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:convert';
6
7import 'package:flutter/material.dart';
Dan Field76784652020-11-05 17:28:47 -08008import 'package:flutter_test/flutter_test.dart';
Phil Quitslund61c30c42021-04-21 13:49:03 -07009import 'package:integration_test/common.dart';
10import 'package:integration_test/integration_test.dart';
Dan Field76784652020-11-05 17:28:47 -080011import 'package:vm_service/vm_service.dart' as vm;
12
13vm.Timeline _kTimelines = vm.Timeline(
14 traceEvents: <vm.TimelineEvent>[],
15 timeOriginMicros: 100,
16 timeExtentMicros: 200,
17);
18
19Future<void> main() async {
Dan Fieldd3092542021-02-01 10:19:14 -080020 Future<Map<String, dynamic>>? request;
Dan Field76784652020-11-05 17:28:47 -080021
22 group('Test Integration binding', () {
Ian Hicksonab89ce22022-02-03 14:55:15 -080023 final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
Dan Field76784652020-11-05 17:28:47 -080024
Dan Fieldd3092542021-02-01 10:19:14 -080025 FakeVM? fakeVM;
Dan Field76784652020-11-05 17:28:47 -080026
27 setUp(() {
Ian Hicksonab89ce22022-02-03 14:55:15 -080028 request = binding.callback(<String, String>{
Dan Field76784652020-11-05 17:28:47 -080029 'command': 'request_data',
30 });
Dan Field9eacc9e2021-01-26 18:24:03 -080031 fakeVM = FakeVM(
32 timeline: _kTimelines,
Dan Field76784652020-11-05 17:28:47 -080033 );
34 });
35
36 testWidgets('Run Integration app', (WidgetTester tester) async {
37 runApp(const MaterialApp(
38 home: Text('Test'),
39 ));
Ian Hicksonab89ce22022-02-03 14:55:15 -080040 expect(tester.binding, binding);
41 binding.reportData = <String, dynamic>{'answer': 42};
Kaushik Iskad5f372b2022-09-07 12:54:06 -040042 await tester.pump();
Dan Field76784652020-11-05 17:28:47 -080043 });
44
Cody Goldbergab6ce712021-07-30 16:47:20 -040045 testWidgets('hitTesting works when using setSurfaceSize', (WidgetTester tester) async {
46 int invocations = 0;
47 await tester.pumpWidget(
48 MaterialApp(
49 home: Center(
50 child: GestureDetector(
51 onTap: () {
52 invocations++;
53 },
54 child: const Text('Test'),
55 ),
56 ),
57 ),
58 );
59
60 await tester.tap(find.byType(Text));
61 await tester.pump();
62 expect(invocations, 1);
63
64 await tester.binding.setSurfaceSize(const Size(200, 300));
65 await tester.pump();
66 await tester.tap(find.byType(Text));
67 await tester.pump();
68 expect(invocations, 2);
69
70 await tester.binding.setSurfaceSize(null);
71 await tester.pump();
72 await tester.tap(find.byType(Text));
73 await tester.pump();
74 expect(invocations, 3);
75 });
76
Dan Field76784652020-11-05 17:28:47 -080077 testWidgets('setSurfaceSize works', (WidgetTester tester) async {
78 await tester.pumpWidget(const MaterialApp(home: Center(child: Text('Test'))));
79
pdblasi-googlee22e8f22023-03-14 11:19:20 -070080 final Size viewCenter = tester.view.physicalSize /
81 tester.view.devicePixelRatio /
Dan Field76784652020-11-05 17:28:47 -080082 2;
pdblasi-googlee22e8f22023-03-14 11:19:20 -070083 final double viewCenterX = viewCenter.width;
84 final double viewCenterY = viewCenter.height;
Dan Field76784652020-11-05 17:28:47 -080085
86 Offset widgetCenter = tester.getRect(find.byType(Text)).center;
pdblasi-googlee22e8f22023-03-14 11:19:20 -070087 expect(widgetCenter.dx, viewCenterX);
88 expect(widgetCenter.dy, viewCenterY);
Dan Field76784652020-11-05 17:28:47 -080089
90 await tester.binding.setSurfaceSize(const Size(200, 300));
91 await tester.pump();
92 widgetCenter = tester.getRect(find.byType(Text)).center;
93 expect(widgetCenter.dx, 100);
94 expect(widgetCenter.dy, 150);
95
96 await tester.binding.setSurfaceSize(null);
97 await tester.pump();
98 widgetCenter = tester.getRect(find.byType(Text)).center;
pdblasi-googlee22e8f22023-03-14 11:19:20 -070099 expect(widgetCenter.dx, viewCenterX);
100 expect(widgetCenter.dy, viewCenterY);
Dan Field76784652020-11-05 17:28:47 -0800101 });
102
103 testWidgets('Test traceAction', (WidgetTester tester) async {
Ian Hicksonab89ce22022-02-03 14:55:15 -0800104 await binding.enableTimeline(vmService: fakeVM);
105 await binding.traceAction(() async {});
106 expect(binding.reportData, isNotNull);
107 expect(binding.reportData!.containsKey('timeline'), true);
Dan Field76784652020-11-05 17:28:47 -0800108 expect(
Ian Hicksonab89ce22022-02-03 14:55:15 -0800109 json.encode(binding.reportData!['timeline']),
Dan Field76784652020-11-05 17:28:47 -0800110 json.encode(_kTimelines),
111 );
112 });
Jia Haof5933db2020-11-10 00:39:05 +0800113
114 group('defaultTestTimeout', () {
Ian Hicksonab89ce22022-02-03 14:55:15 -0800115 final Timeout originalTimeout = binding.defaultTestTimeout;
Jia Haof5933db2020-11-10 00:39:05 +0800116 tearDown(() {
Ian Hicksonab89ce22022-02-03 14:55:15 -0800117 binding.defaultTestTimeout = originalTimeout;
Jia Haof5933db2020-11-10 00:39:05 +0800118 });
119
120 test('can be configured', () {
121 const Timeout newTimeout = Timeout(Duration(seconds: 17));
Ian Hicksonab89ce22022-02-03 14:55:15 -0800122 binding.defaultTestTimeout = newTimeout;
123 expect(binding.defaultTestTimeout, newTimeout);
Jia Haof5933db2020-11-10 00:39:05 +0800124 });
125 });
Jia Haoc2bfb7b2020-12-10 08:33:04 +0800126
Dan Fieldc5861cf2021-08-25 10:09:03 -0700127 // TODO(jiahaog): Remove when https://github.com/flutter/flutter/issues/66006 is fixed.
128 testWidgets('root widgets are wrapped with a RepaintBoundary', (WidgetTester tester) async {
Jia Haoc2bfb7b2020-12-10 08:33:04 +0800129 await tester.pumpWidget(const Placeholder());
130
Dan Fieldc5861cf2021-08-25 10:09:03 -0700131 expect(find.byType(RepaintBoundary), findsOneWidget);
Jia Haoc2bfb7b2020-12-10 08:33:04 +0800132 });
Dan Field76784652020-11-05 17:28:47 -0800133 });
134
135 tearDownAll(() async {
Anis Alibegićc99ed372021-06-02 19:14:06 +0200136 // This part is outside the group so that `request` has been completed as
Chris Bracken3e7e0652021-04-01 10:04:03 -0700137 // part of the `tearDownAll` registered in the group during
Dan Field76784652020-11-05 17:28:47 -0800138 // `IntegrationTestWidgetsFlutterBinding` initialization.
139 final Map<String, dynamic> response =
Dan Fieldd3092542021-02-01 10:19:14 -0800140 (await request)!['response'] as Map<String, dynamic>;
Dan Field76784652020-11-05 17:28:47 -0800141 final String message = response['message'] as String;
142 final Response result = Response.fromJson(message);
Dan Fieldd3092542021-02-01 10:19:14 -0800143 assert(result.data!['answer'] == 42);
Dan Field76784652020-11-05 17:28:47 -0800144 });
145}
146
Dan Field9eacc9e2021-01-26 18:24:03 -0800147class FakeVM extends Fake implements vm.VmService {
Dan Fieldd3092542021-02-01 10:19:14 -0800148 FakeVM({required this.timeline});
Dan Field9eacc9e2021-01-26 18:24:03 -0800149
150 vm.Timeline timeline;
151
152 @override
Dan Fieldd3092542021-02-01 10:19:14 -0800153 Future<vm.Timeline> getVMTimeline({int? timeOriginMicros, int? timeExtentMicros}) async {
Dan Field9eacc9e2021-01-26 18:24:03 -0800154 return timeline;
155 }
156
157 int lastTimeStamp = 0;
158 @override
159 Future<vm.Timestamp> getVMTimelineMicros() async {
160 lastTimeStamp += 100;
161 return vm.Timestamp(timestamp: lastTimeStamp);
162 }
163
Dan Field9eacc9e2021-01-26 18:24:03 -0800164 @override
165 Future<vm.Success> setVMTimelineFlags(List<String> recordedStreams) async {
Dan Field9eacc9e2021-01-26 18:24:03 -0800166 return vm.Success();
167 }
168
169 @override
170 Future<vm.Success> clearVMTimeline() async {
171 return vm.Success();
172 }
173}