-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Make VPN toggle actions match displayed VPN state #8887
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
Open
atavism
wants to merge
2
commits into
main
Choose a base branch
from
atavism/issue-3636
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−22
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,215 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:fpdart/fpdart.dart'; | ||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
| import 'package:lantern/core/common/common.dart'; | ||
| import 'package:lantern/core/models/lantern_status.dart'; | ||
| import 'package:lantern/features/vpn/provider/server_location_notifier.dart'; | ||
| import 'package:lantern/features/vpn/provider/vpn_notifier.dart'; | ||
| import 'package:lantern/features/vpn/provider/vpn_status_notifier.dart'; | ||
| import 'package:lantern/lantern/lantern_service.dart'; | ||
| import 'package:lantern/lantern/lantern_service_notifier.dart'; | ||
|
|
||
| class _FakeLanternService implements LanternService { | ||
| final statusController = StreamController<LanternStatus>.broadcast(); | ||
|
|
||
| Either<Failure, bool> isConnectedResult = right(false); | ||
| Completer<Either<Failure, bool>>? isConnectedCompleter; | ||
| Object? isConnectedException; | ||
| int isVPNConnectedCalls = 0; | ||
| int startVPNCalls = 0; | ||
| int stopVPNCalls = 0; | ||
|
|
||
| @override | ||
| Future<Either<Failure, bool>> isVPNConnected() async { | ||
| isVPNConnectedCalls += 1; | ||
| if (isConnectedException != null) { | ||
| throw isConnectedException!; | ||
| } | ||
| final completer = isConnectedCompleter; | ||
| if (completer != null) { | ||
| return completer.future; | ||
| } | ||
| return isConnectedResult; | ||
| } | ||
|
|
||
| @override | ||
| Stream<LanternStatus> watchVPNStatus() => statusController.stream; | ||
|
|
||
| @override | ||
| Future<Either<Failure, String>> startVPN() async { | ||
| startVPNCalls += 1; | ||
| return right('ok'); | ||
| } | ||
|
|
||
| @override | ||
| Future<Either<Failure, String>> stopVPN() async { | ||
| stopVPNCalls += 1; | ||
| return right('ok'); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> checkVpnConflict() async => false; | ||
|
|
||
| @override | ||
| Future<bool> isTagAvailable(String tag) async => true; | ||
|
|
||
| Future<void> dispose() => statusController.close(); | ||
|
|
||
| @override | ||
| dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | ||
| } | ||
|
|
||
| ProviderContainer _container(_FakeLanternService service) { | ||
| return ProviderContainer( | ||
| overrides: [ | ||
| lanternServiceProvider.overrideWithValue(service), | ||
| serverLocationProvider.overrideWithValue(initialServerLocation()), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| void _disposeContainerAndService( | ||
| ProviderContainer container, | ||
| _FakeLanternService service, | ||
| ) { | ||
| addTearDown(() async { | ||
| container.dispose(); | ||
| await service.dispose(); | ||
| }); | ||
| } | ||
|
|
||
| Future<void> _pumpProviderQueue() async { | ||
| await Future<void>.delayed(Duration.zero); | ||
| await Future<void>.delayed(Duration.zero); | ||
| } | ||
|
|
||
| void main() { | ||
| group('VpnNotifier', () { | ||
| test('hydrates initial connected state from core', () async { | ||
| final service = _FakeLanternService()..isConnectedResult = right(true); | ||
| final container = _container(service); | ||
| _disposeContainerAndService(container, service); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.disconnected); | ||
|
|
||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(service.isVPNConnectedCalls, 1); | ||
| expect(container.read(vpnProvider), VPNStatus.connected); | ||
| }); | ||
|
|
||
| test('stream status wins over slower initial hydration', () async { | ||
| final service = _FakeLanternService() | ||
| ..isConnectedCompleter = Completer<Either<Failure, bool>>(); | ||
| final container = _container(service); | ||
| _disposeContainerAndService(container, service); | ||
| final statusSub = container.listen<AsyncValue<LanternStatus>>( | ||
| vPNStatusProvider, | ||
| (previous, next) {}, | ||
| fireImmediately: true, | ||
| ); | ||
| addTearDown(statusSub.close); | ||
|
|
||
| container.read(vpnProvider); | ||
| await _pumpProviderQueue(); | ||
| service.statusController.add( | ||
| LanternStatus(status: VPNStatus.disconnected), | ||
| ); | ||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.disconnected); | ||
|
|
||
| service.isConnectedCompleter!.complete(right(true)); | ||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.disconnected); | ||
| }); | ||
|
|
||
| test('thrown hydration failures become VPNStatus.error', () async { | ||
| final service = _FakeLanternService() | ||
| ..isConnectedException = StateError('hydration failed'); | ||
| final container = _container(service); | ||
| _disposeContainerAndService(container, service); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.disconnected); | ||
|
|
||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(service.isVPNConnectedCalls, 1); | ||
| expect(container.read(vpnProvider), VPNStatus.error); | ||
| }); | ||
|
|
||
| test( | ||
| 'starts VPN from an error state because the switch displays off', | ||
| () async { | ||
| final service = _FakeLanternService(); | ||
| final container = _container(service); | ||
| _disposeContainerAndService(container, service); | ||
| final statusSub = container.listen<AsyncValue<LanternStatus>>( | ||
| vPNStatusProvider, | ||
| (previous, next) {}, | ||
| fireImmediately: true, | ||
| ); | ||
| addTearDown(statusSub.close); | ||
|
|
||
| container.read(vpnProvider); | ||
| await _pumpProviderQueue(); | ||
| service.statusController.add( | ||
| LanternStatus(status: VPNStatus.error, error: 'connect failed'), | ||
| ); | ||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.error); | ||
|
|
||
| final result = await container | ||
| .read(vpnProvider.notifier) | ||
| .onVPNStateChange(); | ||
|
|
||
| expect(result.isRight(), isTrue); | ||
| expect(service.startVPNCalls, 1); | ||
| expect(service.stopVPNCalls, 0); | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| 'status stream errors become VPNStatus.error instead of crashing', | ||
| () async { | ||
| final service = _FakeLanternService(); | ||
| final container = _container(service); | ||
| _disposeContainerAndService(container, service); | ||
| final statusSub = container.listen<AsyncValue<LanternStatus>>( | ||
| vPNStatusProvider, | ||
| (previous, next) {}, | ||
| fireImmediately: true, | ||
| ); | ||
| addTearDown(statusSub.close); | ||
|
|
||
| container.read(vpnProvider); | ||
| await _pumpProviderQueue(); | ||
| service.statusController.addError(StateError('status stream failed')); | ||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.error); | ||
| final status = container.read(vPNStatusProvider); | ||
| expect(status.hasValue, isTrue); | ||
| expect(status.value?.status, VPNStatus.error); | ||
| expect(status.value?.error, contains('status stream failed')); | ||
|
|
||
| service.statusController.add( | ||
| LanternStatus( | ||
| status: VPNStatus.disconnected, | ||
| origin: VPNStatusOrigin.settingsMutation, | ||
| ), | ||
| ); | ||
| await _pumpProviderQueue(); | ||
|
|
||
| expect(container.read(vpnProvider), VPNStatus.disconnected); | ||
| final recoveredStatus = container.read(vPNStatusProvider); | ||
| expect(recoveredStatus.hasValue, isTrue); | ||
| expect(recoveredStatus.value?.status, VPNStatus.disconnected); | ||
| }, | ||
| ); | ||
| }); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.