Skip to content

Commit

Permalink
[shared_preferences] driver test using package:test on device (flutte…
Browse files Browse the repository at this point in the history
…r#1285)

* Add SharedPreference example driver test
  • Loading branch information
collinjackson authored Mar 15, 2019
1 parent 228b40f commit c5bf62e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/shared_preferences/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ dependencies:
shared_preferences:
path: ../

dev_dependencies:
flutter_driver:
sdk: flutter
test: any

flutter:
uses-material-design: true
93 changes: 93 additions & 0 deletions packages/shared_preferences/example/test/shared_preferences.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'dart:async';
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
final Completer<String> completer = Completer<String>();
enableFlutterDriverExtension(handler: (_) => completer.future);
tearDownAll(() => completer.complete(null));

group('$SharedPreferences', () {
const Map<String, dynamic> kTestValues = <String, dynamic>{
'flutter.String': 'hello world',
'flutter.bool': true,
'flutter.int': 42,
'flutter.double': 3.14159,
'flutter.List': <String>['foo', 'bar'],
};

const Map<String, dynamic> kTestValues2 = <String, dynamic>{
'flutter.String': 'goodbye world',
'flutter.bool': false,
'flutter.int': 1337,
'flutter.double': 2.71828,
'flutter.List': <String>['baz', 'quox'],
};

SharedPreferences preferences;

setUp(() async {
preferences = await SharedPreferences.getInstance();
});

tearDown(() {
preferences.clear();
});

test('reading', () async {
expect(preferences.get('String'), isNull);
expect(preferences.get('bool'), isNull);
expect(preferences.get('int'), isNull);
expect(preferences.get('double'), isNull);
expect(preferences.get('List'), isNull);
expect(preferences.getString('String'), isNull);
expect(preferences.getBool('bool'), isNull);
expect(preferences.getInt('int'), isNull);
expect(preferences.getDouble('double'), isNull);
expect(preferences.getStringList('List'), isNull);
});

test('writing', () async {
await Future.wait(<Future<bool>>[
preferences.setString('String', kTestValues2['flutter.String']),
preferences.setBool('bool', kTestValues2['flutter.bool']),
preferences.setInt('int', kTestValues2['flutter.int']),
preferences.setDouble('double', kTestValues2['flutter.double']),
preferences.setStringList('List', kTestValues2['flutter.List'])
]);
expect(preferences.getString('String'), kTestValues2['flutter.String']);
expect(preferences.getBool('bool'), kTestValues2['flutter.bool']);
expect(preferences.getInt('int'), kTestValues2['flutter.int']);
expect(preferences.getDouble('double'), kTestValues2['flutter.double']);
expect(preferences.getStringList('List'), kTestValues2['flutter.List']);
});

test('removing', () async {
const String key = 'testKey';
preferences
..setString(key, kTestValues['flutter.String'])
..setBool(key, kTestValues['flutter.bool'])
..setInt(key, kTestValues['flutter.int'])
..setDouble(key, kTestValues['flutter.double'])
..setStringList(key, kTestValues['flutter.List']);
await preferences.remove(key);
expect(preferences.get('testKey'), isNull);
});

test('clearing', () async {
preferences
..setString('String', kTestValues['flutter.String'])
..setBool('bool', kTestValues['flutter.bool'])
..setInt('int', kTestValues['flutter.int'])
..setDouble('double', kTestValues['flutter.double'])
..setStringList('List', kTestValues['flutter.List']);
await preferences.clear();
expect(preferences.getString('String'), null);
expect(preferences.getBool('bool'), null);
expect(preferences.getInt('int'), null);
expect(preferences.getDouble('double'), null);
expect(preferences.getStringList('List'), null);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter_test/flutter_test.dart';

import '../lib/main.dart';

void main() {
testWidgets('SharedPreferences example widget test',
(WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('SharedPreferences Demo'), findsOneWidget);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';

void main() {
test('SharedPreferences', () async {
final FlutterDriver driver = await FlutterDriver.connect();
await driver.requestData(null, timeout: const Duration(minutes: 1));
if (driver != null) {
driver.close();
}
});
}
3 changes: 3 additions & 0 deletions packages/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
test: any

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
Expand Down

0 comments on commit c5bf62e

Please sign in to comment.