diff --git a/packages/shared_preferences/example/pubspec.yaml b/packages/shared_preferences/example/pubspec.yaml index 68f61bd084c5..866185589c8d 100644 --- a/packages/shared_preferences/example/pubspec.yaml +++ b/packages/shared_preferences/example/pubspec.yaml @@ -7,5 +7,10 @@ dependencies: shared_preferences: path: ../ +dev_dependencies: + flutter_driver: + sdk: flutter + test: any + flutter: uses-material-design: true diff --git a/packages/shared_preferences/example/test/shared_preferences.dart b/packages/shared_preferences/example/test/shared_preferences.dart new file mode 100644 index 000000000000..bf40888c43b4 --- /dev/null +++ b/packages/shared_preferences/example/test/shared_preferences.dart @@ -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 completer = Completer(); + enableFlutterDriverExtension(handler: (_) => completer.future); + tearDownAll(() => completer.complete(null)); + + group('$SharedPreferences', () { + const Map kTestValues = { + 'flutter.String': 'hello world', + 'flutter.bool': true, + 'flutter.int': 42, + 'flutter.double': 3.14159, + 'flutter.List': ['foo', 'bar'], + }; + + const Map kTestValues2 = { + 'flutter.String': 'goodbye world', + 'flutter.bool': false, + 'flutter.int': 1337, + 'flutter.double': 2.71828, + 'flutter.List': ['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(>[ + 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); + }); + }); +} diff --git a/packages/shared_preferences/example/test/shared_preferences_test.dart b/packages/shared_preferences/example/test/shared_preferences_test.dart new file mode 100644 index 000000000000..c5044730dd3f --- /dev/null +++ b/packages/shared_preferences/example/test/shared_preferences_test.dart @@ -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); + }); +} diff --git a/packages/shared_preferences/example/test_driver/shared_preferences_test.dart b/packages/shared_preferences/example/test_driver/shared_preferences_test.dart new file mode 100644 index 000000000000..110203456c7b --- /dev/null +++ b/packages/shared_preferences/example/test_driver/shared_preferences_test.dart @@ -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(); + } + }); +} diff --git a/packages/shared_preferences/pubspec.yaml b/packages/shared_preferences/pubspec.yaml index 3bbdbd731e53..548b53a4b093 100644 --- a/packages/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/pubspec.yaml @@ -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"