forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shared_preferences] driver test using package:test on device (flutte…
…r#1285) * Add SharedPreference example driver test
- Loading branch information
1 parent
228b40f
commit c5bf62e
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/shared_preferences/example/test/shared_preferences.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/shared_preferences/example/test/shared_preferences_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/shared_preferences/example/test_driver/shared_preferences_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters