Skip to content

Commit

Permalink
Basic tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kipruto <[email protected]>
  • Loading branch information
kelvinkipruto committed Jan 30, 2024
1 parent ce02eb0 commit aebb7d7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 64 deletions.
8 changes: 3 additions & 5 deletions example/integration_test/plugin_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// For more information about Flutter integration tests, please see
// https://docs.flutter.dev/cookbook/testing/integration/introduction


import 'package:apps_list/app_info.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

Expand All @@ -17,9 +17,7 @@ void main() {

testWidgets('getPlatformVersion test', (WidgetTester tester) async {
final AppsList plugin = AppsList();
final String? version = await plugin.getPlatformVersion();
// The version string depends on the host platform running the test, so
// just assert that some non-empty string is returned.
expect(version?.isNotEmpty, true);
final List<AppInfoByProfile> apps = await plugin.getInstalledApps();
expect(apps, isNotNull);
});
}
51 changes: 15 additions & 36 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _appsListPlugin = AppsList();
List<AppInfoByProfile> _apps = [];

Expand All @@ -27,27 +26,17 @@ class _MyAppState extends State<MyApp> {
initPlatformState();
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
List<AppInfoByProfile> apps = [];
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await _appsListPlugin.getPlatformVersion() ??
'Unknown platform version';
apps = await _appsListPlugin.getInstalledApps();
} on PlatformException {
platformVersion = 'Failed to get platform version.';
throw Exception("Failed to get apps");
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
_platformVersion = platformVersion;
_apps = apps;
});
}
Expand All @@ -57,31 +46,21 @@ class _MyAppState extends State<MyApp> {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
title: const Text('App List Example'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text('Running on: $_platformVersion\n'),
),
Expanded(
child: ListView.builder(
itemCount: _apps.length,
itemBuilder: (context, index) {
final application = _apps[index];
return ListTile(
leading: Image.memory(application.icon),
title: Text(application.name),
subtitle: Text(
"${application.packageName} ${application.serialNumber} ${application.profile}"),
onTap: () => _appsListPlugin.launchApp(
application.packageName, application.serialNumber),
);
},
),
)
],
body: ListView.builder(
itemCount: _apps.length,
itemBuilder: (context, index) {
final application = _apps[index];
return ListTile(
leading: Image.memory(application.icon),
title: Text(application.name),
subtitle: Text(
"${application.packageName} ${application.serialNumber} ${application.profile}"),
onTap: () => _appsListPlugin.launchApp(
application.packageName, application.serialNumber),
);
},
),
),
);
Expand Down
4 changes: 0 additions & 4 deletions lib/apps_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import 'package:apps_list/app_info.dart';
import 'apps_list_platform_interface.dart';

class AppsList {
Future<String?> getPlatformVersion() {
return AppsListPlatform.instance.getPlatformVersion();
}

Future<void> launchApp(String packageName, int serialNumber) async {
return AppsListPlatform.instance.launchApp(packageName, serialNumber);
}
Expand Down
11 changes: 3 additions & 8 deletions lib/apps_list_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ class MethodChannelAppsList extends AppsListPlatform {
@visibleForTesting
final methodChannel = const MethodChannel('apps_list');

@override
Future<String?> getPlatformVersion() async {
final version =
await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}

// launch app
@override
Future<void> launchApp(String packageName, int serialNumber) async {
try {
await methodChannel.invokeMethod('launchApp', {
Expand All @@ -29,8 +23,9 @@ class MethodChannelAppsList extends AppsListPlatform {
}
}

// get installed apps
@override
Future<List<AppInfoByProfile>> getInstalledApps() async {
// Call relevant Kotlin method
try {
List<Object?> apps = await methodChannel.invokeMethod('getInstalledApps');
List<AppInfoByProfile> appInfoList =
Expand Down
4 changes: 0 additions & 4 deletions lib/apps_list_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ abstract class AppsListPlatform extends PlatformInterface {
_instance = instance;
}

Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}

Future<void> launchApp(String packageName, int serialNumber) async {
throw UnimplementedError('launchApp() has not been implemented.');
}
Expand Down
10 changes: 6 additions & 4 deletions test/apps_list_method_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ void main() {
const MethodChannel channel = MethodChannel('apps_list');

setUp(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
channel,
(MethodCall methodCall) async {
return '42';
Expand All @@ -18,10 +19,11 @@ void main() {
});

tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(channel, null);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
});

test('getPlatformVersion', () async {
expect(await platform.getPlatformVersion(), '42');
test('getAppsList', () async {
expect(await platform.getInstalledApps(), isNotNull);
});
}
16 changes: 13 additions & 3 deletions test/apps_list_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:apps_list/app_info.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:apps_list/apps_list.dart';
import 'package:apps_list/apps_list_platform_interface.dart';
Expand All @@ -7,9 +8,17 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart';
class MockAppsListPlatform
with MockPlatformInterfaceMixin
implements AppsListPlatform {
Future<String?> getPlatformVersion() => Future.value('42');

@override
Future<String?> getPlatformVersion() => Future.value('42');
Future<List<AppInfoByProfile>> getInstalledApps() {
return Future.value([]);
}

@override
Future<void> launchApp(String packageName, int serialNumber) {
return Future.value();
}
}

void main() {
Expand All @@ -19,11 +28,12 @@ void main() {
expect(initialPlatform, isInstanceOf<MethodChannelAppsList>());
});

test('getPlatformVersion', () async {
test('getAppsList', () async {
AppsList appsListPlugin = AppsList();
MockAppsListPlatform fakePlatform = MockAppsListPlatform();
AppsListPlatform.instance = fakePlatform;

expect(await appsListPlugin.getPlatformVersion(), '42');
List<AppInfoByProfile> apps = await appsListPlugin.getInstalledApps();
expect(apps, isNotNull);
});
}

0 comments on commit aebb7d7

Please sign in to comment.