Skip to content

Commit 5516791

Browse files
authored
[shared_preferences_web] Migrate tests to integration_test (flutter#4185)
1 parent 2727efc commit 5516791

File tree

11 files changed

+128
-8
lines changed

11 files changed

+128
-8
lines changed

packages/shared_preferences/shared_preferences_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Move tests to `example` directory, so they run as integration_tests with `flutter drive`.
4+
15
## 2.0.0
26

37
* Migrate to null-safety.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Testing
2+
3+
This package uses `package:integration_test` to run its tests in a web browser.
4+
5+
See [Plugin Tests > Web Tests](https://github.com/flutter/flutter/wiki/Plugin-Tests#web-tests)
6+
in the Flutter wiki for instructions to setup and run the tests in this package.
7+
8+
Check [flutter.dev > Integration testing](https://flutter.dev/docs/testing/integration-tests)
9+
for more info.

packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart renamed to packages/shared_preferences/shared_preferences_web/example/integration_test/shared_preferences_web_test.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
@TestOn('chrome')
65
import 'dart:convert' show json;
76
import 'dart:html' as html;
87

98
import 'package:flutter_test/flutter_test.dart';
9+
import 'package:integration_test/integration_test.dart';
1010
import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
1111
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
1212
import 'package:shared_preferences_web/shared_preferences_web.dart';
@@ -20,12 +20,14 @@ const Map<String, dynamic> kTestValues = <String, dynamic>{
2020
};
2121

2222
void main() {
23+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
24+
2325
group('SharedPreferencesPlugin', () {
2426
setUp(() {
2527
html.window.localStorage.clear();
2628
});
2729

28-
test('registers itself', () {
30+
testWidgets('registers itself', (WidgetTester tester) async {
2931
SharedPreferencesStorePlatform.instance =
3032
MethodChannelSharedPreferencesStore();
3133
expect(SharedPreferencesStorePlatform.instance,
@@ -35,7 +37,7 @@ void main() {
3537
isA<SharedPreferencesPlugin>());
3638
});
3739

38-
test('getAll', () async {
40+
testWidgets('getAll', (WidgetTester tester) async {
3941
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
4042
expect(await store.getAll(), isEmpty);
4143

@@ -46,7 +48,7 @@ void main() {
4648
expect(allData['flutter.testKey'], 'test value');
4749
});
4850

49-
test('remove', () async {
51+
testWidgets('remove', (WidgetTester tester) async {
5052
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
5153
html.window.localStorage['flutter.testKey'] = '"test value"';
5254
expect(html.window.localStorage['flutter.testKey'], isNotNull);
@@ -58,7 +60,7 @@ void main() {
5860
);
5961
});
6062

61-
test('setValue', () async {
63+
testWidgets('setValue', (WidgetTester tester) async {
6264
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
6365
for (String key in kTestValues.keys) {
6466
final dynamic value = kTestValues[key];
@@ -79,7 +81,7 @@ void main() {
7981
);
8082
});
8183

82-
test('clear', () async {
84+
testWidgets('clear', (WidgetTester tester) async {
8385
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
8486
html.window.localStorage['flutter.testKey1'] = '"test value"';
8587
html.window.localStorage['flutter.testKey2'] = '42';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2013 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+
5+
import 'package:flutter/material.dart';
6+
7+
void main() {
8+
runApp(MyApp());
9+
}
10+
11+
/// App for testing
12+
class MyApp extends StatefulWidget {
13+
@override
14+
_MyAppState createState() => _MyAppState();
15+
}
16+
17+
class _MyAppState extends State<MyApp> {
18+
@override
19+
Widget build(BuildContext context) {
20+
return Directionality(
21+
textDirection: TextDirection.ltr,
22+
child: Text('Testing... Look at the console output for results!'),
23+
);
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: shared_preferences_web_integration_tests
2+
publish_to: none
3+
4+
environment:
5+
sdk: ">=2.12.0 <3.0.0"
6+
flutter: ">=2.2.0"
7+
8+
dependencies:
9+
shared_preferences_web:
10+
path: ../
11+
flutter:
12+
sdk: flutter
13+
14+
dev_dependencies:
15+
js: ^0.6.3
16+
flutter_test:
17+
sdk: flutter
18+
flutter_driver:
19+
sdk: flutter
20+
integration_test:
21+
sdk: flutter
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/bash
2+
# Copyright 2013 The Flutter Authors. All rights reserved.
3+
# Use of this source code is governed by a BSD-style license that can be
4+
# found in the LICENSE file.
5+
6+
if pgrep -lf chromedriver > /dev/null; then
7+
echo "chromedriver is running."
8+
9+
if [ $# -eq 0 ]; then
10+
echo "No target specified, running all tests..."
11+
find integration_test/ -iname *_test.dart | xargs -n1 -i -t flutter drive -d web-server --web-port=7357 --browser-name=chrome --driver=test_driver/integration_test.dart --target='{}'
12+
else
13+
echo "Running test target: $1..."
14+
set -x
15+
flutter drive -d web-server --web-port=7357 --browser-name=chrome --driver=test_driver/integration_test.dart --target=$1
16+
fi
17+
18+
else
19+
echo "chromedriver is not running."
20+
echo "Please, check the README.md for instructions on how to use run_test.sh"
21+
fi
22+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2013 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+
5+
import 'package:integration_test/integration_test_driver.dart';
6+
7+
Future<void> main() => integrationDriver();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<!-- Copyright 2013 The Flutter Authors. All rights reserved.
3+
Use of this source code is governed by a BSD-style license that can be
4+
found in the LICENSE file. -->
5+
<html>
6+
<head>
7+
<meta charset="UTF-8">
8+
<title>example</title>
9+
</head>
10+
<body>
11+
<script src="main.dart.js" type="application/javascript"></script>
12+
</body>
13+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## test
2+
3+
This package uses integration tests for testing.
4+
5+
See `example/README.md` for more info.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013 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+
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
void main() {
8+
test('Tell the user where to find the real tests', () {
9+
print('---');
10+
print('This package uses integration_test for its tests.');
11+
print('See `example/README.md` for more info.');
12+
print('---');
13+
});
14+
}

0 commit comments

Comments
 (0)