-
-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ConnectivityIntegration
for web
#1765
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6392c92
add connectivity integration
denrase 2b3acd0
Add connectivity integration in options
denrase fdc2d23
Merge branch 'main' into feat/connectivity
denrase 7156254
only check web apis
denrase d6edcbb
separate web and non web components
denrase bf887be
remove connectivity api as it is still experimental API
denrase e04bec3
update test
denrase 2dce393
format
denrase 307af9b
Add changelog entry
denrase 0b397c4
Merge branch 'main' into feat/connectivity
denrase 948a02b
update changelog
denrase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
flutter/lib/src/integrations/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,57 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:meta/meta.dart'; | ||
import '../../sentry_flutter.dart'; | ||
|
||
class ConnectivityIntegration extends Integration<SentryFlutterOptions> { | ||
Connectivity connectivity = Connectivity(); | ||
Hub? _hub; | ||
StreamSubscription<ConnectivityResult>? _subscription; | ||
|
||
@override | ||
void call(Hub hub, SentryFlutterOptions options) { | ||
_hub = hub; | ||
_subscription = connectivity.onConnectivityChanged.listen(addBreadcrumb); | ||
|
||
options.sdk.addIntegration('connectivityIntegration'); | ||
} | ||
|
||
@override | ||
void close() { | ||
_hub = null; | ||
_subscription?.cancel(); | ||
_subscription = null; | ||
} | ||
|
||
@internal | ||
@visibleForTesting | ||
void addBreadcrumb(ConnectivityResult result) { | ||
_hub?.addBreadcrumb( | ||
Breadcrumb( | ||
category: 'device.connectivity', | ||
level: SentryLevel.info, | ||
type: 'connectivity', | ||
data: {'connectivity': result.toSentryConnectivity()}), | ||
); | ||
} | ||
} | ||
|
||
extension on ConnectivityResult { | ||
String toSentryConnectivity() { | ||
switch (this) { | ||
case ConnectivityResult.bluetooth: | ||
case ConnectivityResult.vpn: | ||
case ConnectivityResult.wifi: | ||
return 'wifi'; | ||
case ConnectivityResult.ethernet: | ||
return 'ethernet'; | ||
case ConnectivityResult.mobile: | ||
return 'cellular'; | ||
case ConnectivityResult.none: | ||
return 'none'; | ||
case ConnectivityResult.other: | ||
return 'other'; | ||
} | ||
} | ||
} |
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
149 changes: 149 additions & 0 deletions
149
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,149 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
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_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: connectivity changed `bluetooth` adds `wifi` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.bluetooth); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'wifi'); | ||
}); | ||
|
||
test( | ||
'$ConnectivityIntegration: connectivity changed `wifi` adds `wifi` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.wifi); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'wifi'); | ||
}); | ||
|
||
test( | ||
'$ConnectivityIntegration: connectivity changed `vpn` adds `vpn` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.vpn); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'wifi'); | ||
}); | ||
|
||
test( | ||
'$ConnectivityIntegration: connectivity changed `ethernet` adds `ethernet` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.ethernet); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'ethernet'); | ||
}); | ||
|
||
test( | ||
'$ConnectivityIntegration: connectivity changed `mobile` adds `cellular` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.mobile); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'cellular'); | ||
}); | ||
|
||
test( | ||
'$ConnectivityIntegration: connectivity changed `other` adds `other` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.other); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'other'); | ||
}); | ||
|
||
test( | ||
'$ConnectivityIntegration: connectivity changed `none` adds `none` breadcrumb', | ||
() { | ||
final integration = fixture.getSut(); | ||
integration.call(fixture.hub, fixture.options); | ||
|
||
integration.addBreadcrumb(ConnectivityResult.none); | ||
|
||
final crumb = verify( | ||
fixture.hub.addBreadcrumb(captureAny), | ||
).captured.first as Breadcrumb; | ||
|
||
verifyBreadcrumb(crumb, 'none'); | ||
}); | ||
} | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda against https://develop.sentry.dev/philosophy/#dependencies-cost and as known from package_info_plus it's a quite frequent source of major updates.