-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ConnectivityIntegration
for web (#1765)
- Loading branch information
Showing
9 changed files
with
206 additions
and
24 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
36 changes: 36 additions & 0 deletions
36
flutter/lib/src/integrations/connectivity/connectivity_integration.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,36 @@ | ||
import 'package:meta/meta.dart'; | ||
import '../../../sentry_flutter.dart'; | ||
import 'connectivity_provider.dart'; | ||
|
||
class ConnectivityIntegration extends Integration<SentryFlutterOptions> { | ||
Hub? _hub; | ||
ConnectivityProvider? _connectivityProvider; | ||
|
||
@override | ||
void call(Hub hub, SentryFlutterOptions options) { | ||
_hub = hub; | ||
_connectivityProvider = ConnectivityProvider(); | ||
_connectivityProvider?.listen((connectivity) { | ||
addBreadcrumb(connectivity); | ||
}); | ||
options.sdk.addIntegration('connectivityIntegration'); | ||
} | ||
|
||
@override | ||
void close() { | ||
_hub = null; | ||
_connectivityProvider?.cancel(); | ||
} | ||
|
||
@internal | ||
@visibleForTesting | ||
void addBreadcrumb(String connectivity) { | ||
_hub?.addBreadcrumb( | ||
Breadcrumb( | ||
category: 'device.connectivity', | ||
level: SentryLevel.info, | ||
type: 'connectivity', | ||
data: {'connectivity': connectivity}), | ||
); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
flutter/lib/src/integrations/connectivity/connectivity_provider.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,9 @@ | ||
import 'noop_connectivity_provider.dart' | ||
if (dart.library.html) 'web_connectivity_provider.dart'; | ||
|
||
abstract class ConnectivityProvider { | ||
factory ConnectivityProvider() => connectivityProvider(); | ||
|
||
void listen(void Function(String connectivity) onChange); | ||
void cancel(); | ||
} |
17 changes: 17 additions & 0 deletions
17
flutter/lib/src/integrations/connectivity/noop_connectivity_provider.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,17 @@ | ||
import 'connectivity_provider.dart'; | ||
|
||
ConnectivityProvider connectivityProvider() { | ||
return NoOpConnectivityProvider(); | ||
} | ||
|
||
class NoOpConnectivityProvider implements ConnectivityProvider { | ||
@override | ||
void listen(void Function(String connectivity) onChange) { | ||
// NoOp | ||
} | ||
|
||
@override | ||
void cancel() { | ||
// NoOp | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
flutter/lib/src/integrations/connectivity/web_connectivity_provider.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,32 @@ | ||
import 'dart:async'; | ||
import 'dart:html' as html; | ||
|
||
import 'connectivity_provider.dart'; | ||
|
||
ConnectivityProvider connectivityProvider() { | ||
return WebConnectivityProvider(); | ||
} | ||
|
||
class WebConnectivityProvider implements ConnectivityProvider { | ||
StreamSubscription<html.Event>? _onOnlineSub; | ||
StreamSubscription<html.Event>? _onOfflineSub; | ||
|
||
@override | ||
void listen(void Function(String connectivity) onChange) { | ||
_onOnlineSub = html.window.onOnline.listen((_) { | ||
onChange('wifi'); | ||
}); | ||
_onOfflineSub = html.window.onOffline.listen((_) { | ||
onChange('none'); | ||
}); | ||
} | ||
|
||
@override | ||
void cancel() { | ||
_onOnlineSub?.cancel(); | ||
_onOnlineSub = null; | ||
|
||
_onOfflineSub?.cancel(); | ||
_onOfflineSub = null; | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
flutter/test/integrations/connectivity_integration_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,56 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:sentry/sentry.dart'; | ||
import 'package:sentry_flutter/src/integrations/connectivity/connectivity_integration.dart'; | ||
import 'package:sentry_flutter/src/sentry_flutter_options.dart'; | ||
|
||
import '../mocks.dart'; | ||
import '../mocks.mocks.dart'; | ||
|
||
void main() { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
|
||
late Fixture fixture; | ||
|
||
setUp(() { | ||
fixture = Fixture(); | ||
}); | ||
|
||
verifyBreadcrumb(Breadcrumb crumb, String connectivityData) { | ||
expect(crumb.category, 'device.connectivity'); | ||
expect(crumb.type, 'connectivity'); | ||
expect(crumb.level, SentryLevel.info); | ||
expect(crumb.data?['connectivity'], connectivityData); | ||
} | ||
|
||
test('adds integration', () { | ||
final sut = fixture.getSut(); | ||
sut(fixture.hub, fixture.options); | ||
|
||
expect(fixture.options.sdk.integrations.contains('connectivityIntegration'), | ||
true); | ||
}); | ||
|
||
test('$ConnectivityIntegration: addsBreadcrumb', () { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb('wifi'); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'wifi'); | ||
}); | ||
} | ||
|
||
class Fixture { | ||
final hub = MockHub(); | ||
final options = SentryFlutterOptions(dsn: fakeDsn); | ||
|
||
ConnectivityIntegration getSut() { | ||
return ConnectivityIntegration(); | ||
} | ||
} |
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
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