1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00

[testing_app] Migrate the sample to the integration_test package (#633)

This commit is contained in:
Abdullah Deshmukh
2021-01-31 12:17:22 +05:30
committed by GitHub
parent d16d35e0ac
commit f63c465ff4
15 changed files with 301 additions and 626 deletions

View File

@@ -0,0 +1,92 @@
// Copyright 2020 The Flutter team. 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_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:testing_app/main.dart';
void main() {
group('Testing App Driver Tests', () {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Finding an item in the list', (tester) async {
await tester.pumpWidget(TestingApp());
// Create variables for finders that are used multiple times.
final itemFinder = find.byKey(ValueKey('text_25'));
// Scroll until the item to be found appears.
await tester.scrollUntilVisible(
itemFinder,
500.0,
);
// Check if the item contains the correct text.
expect(tester.widget<Text>(itemFinder).data, 'Item 25');
});
testWidgets('Testing IconButtons', (tester) async {
await tester.pumpWidget(TestingApp());
// Create a finder for the icon.
final iconFinder = find.byKey(ValueKey('icon_0'));
// Tap on the icon.
await tester.tap(iconFinder);
await tester.pumpAndSettle(Duration(seconds: 1));
// Verify if appropriate message appears.
expect(find.text('Added to favorites.'), findsOneWidget);
// Tap on the icon again.
await tester.tap(iconFinder);
await tester.pumpAndSettle(Duration(seconds: 1));
// Verify if appropriate message appears.
expect(find.text('Removed from favorites.'), findsOneWidget);
await tester.pumpAndSettle(Duration(seconds: 1));
});
testWidgets('Verifying whether item gets added to favorites',
(tester) async {
await tester.pumpWidget(TestingApp());
// Add item to favorites.
await tester.tap(find.byKey(ValueKey('icon_5')));
await tester.pumpAndSettle(Duration(seconds: 1));
// Tap on the favorites button on the AppBar.
// The Favorites List should appear.
await tester.tap(find.text('Favorites'));
await tester.pumpAndSettle();
// Check if the added item has appeared in the list.
expect(tester.widget<Text>(find.byKey(ValueKey('favorites_text_5'))).data,
equals('Item 5'));
});
testWidgets('Testing remove button', (tester) async {
await tester.pumpWidget(TestingApp());
// Add item to favorites.
await tester.tap(find.byKey(ValueKey('icon_5')));
await tester.pumpAndSettle(Duration(seconds: 1));
// Navigate to Favorites screen.
await tester.tap(find.text('Favorites'));
await tester.pumpAndSettle();
// Tap on the remove icon.
await tester.tap(find.byKey(ValueKey('remove_icon_5')));
await tester.pumpAndSettle();
// Verify if it disappears.
expect(find.text('Item 5'), findsNothing);
// Verify if appropriate message appears.
expect(find.text('Removed from favorites.'), findsOneWidget);
});
});
}

View File

@@ -0,0 +1,23 @@
// Copyright 2020 The Flutter team. 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:integration_test/integration_test_driver.dart';
Future<void> main() {
return integrationDriver(
responseDataCallback: (data) async {
// If the tests reported any data, save it to the disk.
if (data != null) {
for (var entry in data.entries) {
print('Writing ${entry.key} to the disk.');
// Default storage destination is the 'build' directory.
await writeResponseData(
entry.value as Map<String, dynamic>,
testOutputFilename: entry.key,
);
}
}
},
);
}

View File

@@ -0,0 +1,119 @@
// Copyright 2020 The Flutter team. 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_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:testing_app/main.dart';
void main() {
group('Testing App Performance Tests', () {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized()
as IntegrationTestWidgetsFlutterBinding;
// The fullyLive frame policy simulates
// the way Flutter responds to animations.
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
testWidgets('Scrolling test', (tester) async {
await tester.pumpWidget(TestingApp());
// Create variables for finders that are used multiple times.
final listFinder = find.byType(ListView);
final scroller = tester.widget<ListView>(listFinder).controller;
// Record the performance timeline of the following operations.
await binding.traceAction(
() async {
// Record the performance summary as the app scrolls through
// the list of items.
await binding.watchPerformance(
() async {
// Quickly scroll all the way down.
await scroller.animateTo(
7000,
duration: const Duration(seconds: 1),
curve: Curves.linear,
);
await tester.pumpAndSettle();
// Quickly scroll back up all the way.
await scroller.animateTo(
-7000,
duration: const Duration(seconds: 1),
curve: Curves.linear,
);
await tester.pumpAndSettle();
},
// Send the performance summary to the driver.
reportKey: 'scrolling_summary',
);
},
// Send the timeline data to the driver.
// This timeline can be opened in the Chrome browser's tracing tools
// by navigating to chrome://tracing.
reportKey: 'scrolling_timeline',
);
});
testWidgets('Favorites operations test', (tester) async {
await tester.pumpWidget(TestingApp());
// Record the performance timeline of the following operations.
await binding.traceAction(
() async {
// Record the performance summary as operations are performed
// on the favorites list.
await binding.watchPerformance(
() async {
// Create a list of icon keys.
final iconKeys = [
'icon_0',
'icon_1',
'icon_2',
];
// Add first three items to favorites.
for (var icon in iconKeys) {
// Tap onto the icon.
await tester.tap(find.byKey(ValueKey(icon)));
await tester.pumpAndSettle();
// Verify if appropriate message appears.
expect(find.text('Added to favorites.'), findsOneWidget);
}
// Tap onto the favorites button on the AppBar.
// The Favorites List should appear.
await tester.tap(find.text('Favorites'));
await tester.pumpAndSettle();
final removeIconKeys = [
'remove_icon_0',
'remove_icon_1',
'remove_icon_2',
];
// Remove all the items from favorites.
for (final iconKey in removeIconKeys) {
// Tap onto the remove icon.
await tester.tap(find.byKey(ValueKey(iconKey)));
await tester.pumpAndSettle();
// Verify if appropriate message appears.
expect(find.text('Removed from favorites.'), findsOneWidget);
}
},
// Send the performance summary to the driver.
reportKey: 'favorites_operations_summary',
);
},
// Send the timeline data to the driver.
// This timeline can be opened in the Chrome browser's tracing tools
// by navigating to chrome://tracing.
reportKey: 'favorites_operations_timeline',
);
});
});
}

View File

@@ -0,0 +1,50 @@
// Copyright 2020 The Flutter team. 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_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:provider/provider.dart';
import 'package:testing_app/models/favorites.dart';
import 'package:testing_app/screens/favorites.dart';
Favorites favoritesList;
Widget createFavoritesScreen() => ChangeNotifierProvider<Favorites>(
create: (context) {
favoritesList = Favorites();
return favoritesList;
},
child: MaterialApp(
home: FavoritesPage(),
),
);
void main() {
group('Testing App State Management Tests', () {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Verifying add method', (tester) async {
await tester.pumpWidget(createFavoritesScreen());
// Add an item to the list.
favoritesList.add(30);
await tester.pumpAndSettle();
// Check if the new item appears in the list.
expect(find.text('Item 30'), findsOneWidget);
});
testWidgets('Verifying remove method', (tester) async {
await tester.pumpWidget(createFavoritesScreen());
// Remove an item from the list.
favoritesList.remove(30);
await tester.pumpAndSettle();
// Verify if it disappears.
expect(find.text('Item 30'), findsNothing);
});
});
}