Skip to content

Commit 3928671

Browse files
author
Ali Abdelfattah
authored
Merge pull request #170 from Instabug/fix/android-start-api
[MOB-4959] Fix Instabug.start API when called on Android
2 parents 0bd815c + e3e339a commit 3928671

6 files changed

+73
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
.pub/
3131
pubspec.lock
3232
build/
33+
coverage/
3334

3435
# Android related
3536
**/android/**/gradle-wrapper.jar

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## master
22

33
* Change all `void ... async` methods to `Future<void> ... async` so that callers can use `await`.
4+
* Fixes a crash when Instabug.start API is called on Android.
45

56
## v9.1.9 (2021-05-11)
67

lib/Instabug.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:typed_data';
66
import 'dart:ui';
77

88
import 'package:flutter/services.dart';
9+
import 'package:instabug_flutter/utils/platform_manager.dart';
910

1011
enum InvocationEvent {
1112
shake,
@@ -102,11 +103,13 @@ class Instabug {
102103
/// the SDK's UI.
103104
static Future<void> start(
104105
String token, List<InvocationEvent> invocationEvents) async {
105-
final List<String> invocationEventsStrings =
106-
invocationEvents.map((e) => e.toString()).toList(growable: false);
107-
final List<dynamic> params = <dynamic>[token, invocationEventsStrings];
108-
await _channel.invokeMethod<Object>(
109-
'startWithToken:invocationEvents:', params);
106+
if (PlatformManager.instance.isIOS()) {
107+
final List<String> invocationEventsStrings =
108+
invocationEvents.map((e) => e.toString()).toList(growable: false);
109+
final List<dynamic> params = <dynamic>[token, invocationEventsStrings];
110+
await _channel.invokeMethod<Object>(
111+
'startWithToken:invocationEvents:', params);
112+
}
110113
}
111114

112115
/// Shows the welcome message in a specific mode.

lib/utils/platform_manager.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import 'dart:io';
3+
4+
class PlatformManager {
5+
static PlatformManager _platform = PlatformManager();
6+
7+
static PlatformManager get instance => _platform;
8+
9+
static void setPlatformInstance(PlatformManager platform) {
10+
_platform = platform;
11+
}
12+
13+
bool isAndroid() => Platform.isAndroid;
14+
bool isIOS() => Platform.isIOS;
15+
}

test/instabug_flutter_test.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ import 'package:instabug_flutter/Replies.dart';
1616
import 'package:instabug_flutter/Surveys.dart';
1717
import 'package:instabug_flutter/models/crash_data.dart';
1818
import 'package:instabug_flutter/models/exception_data.dart';
19+
import 'package:instabug_flutter/utils/platform_manager.dart';
20+
import 'package:mockito/annotations.dart';
21+
import 'package:mockito/mockito.dart';
1922
import 'package:stack_trace/stack_trace.dart';
2023

24+
import 'instabug_flutter_test.mocks.dart';
25+
26+
@GenerateMocks([
27+
PlatformManager,
28+
])
2129
void main() {
2230
WidgetsFlutterBinding.ensureInitialized();
2331
final List<MethodCall> log = <MethodCall>[];
@@ -32,6 +40,7 @@ void main() {
3240
const Map<String, String> userAttributePair = <String, String>{
3341
'gender': 'female'
3442
};
43+
late MockPlatformManager mockPlatform;
3544

3645
setUpAll(() async {
3746
const MethodChannel('instabug_flutter')
@@ -50,11 +59,19 @@ void main() {
5059
});
5160
});
5261

62+
setUp(() {
63+
mockPlatform = MockPlatformManager();
64+
PlatformManager.setPlatformInstance(mockPlatform);
65+
});
66+
5367
tearDown(() async {
5468
log.clear();
5569
});
5670

57-
test('startWithToken:invocationEvents: Test', () async {
71+
test('startWithToken:invocationEvents: should be called on iOS',
72+
() async {
73+
when(mockPlatform.isIOS()).thenAnswer((_) => true);
74+
5875
Instabug.start(appToken, invocationEvents);
5976
final List<dynamic> args = <dynamic>[
6077
appToken,

test/instabug_flutter_test.mocks.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Mocks generated by Mockito 5.0.10 from annotations
2+
// in instabug_flutter/example/ios/.symlinks/plugins/instabug_flutter/test/instabug_flutter_test.dart.
3+
// Do not manually edit this file.
4+
5+
import 'package:instabug_flutter/utils/platform_manager.dart' as _i2;
6+
import 'package:mockito/mockito.dart' as _i1;
7+
8+
// ignore_for_file: avoid_redundant_argument_values
9+
// ignore_for_file: comment_references
10+
// ignore_for_file: invalid_use_of_visible_for_testing_member
11+
// ignore_for_file: prefer_const_constructors
12+
// ignore_for_file: unnecessary_parenthesis
13+
14+
/// A class which mocks [PlatformManager].
15+
///
16+
/// See the documentation for Mockito's code generation for more information.
17+
class MockPlatformManager extends _i1.Mock implements _i2.PlatformManager {
18+
MockPlatformManager() {
19+
_i1.throwOnMissingStub(this);
20+
}
21+
22+
@override
23+
bool isAndroid() =>
24+
(super.noSuchMethod(Invocation.method(#isAndroid, []), returnValue: false)
25+
as bool);
26+
@override
27+
bool isIOS() =>
28+
(super.noSuchMethod(Invocation.method(#isIOS, []), returnValue: false)
29+
as bool);
30+
}

0 commit comments

Comments
 (0)