Skip to content

Commit

Permalink
fix(share_plus): breaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SirusCodes committed Oct 28, 2022
1 parent 364d35c commit 167c604
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
6 changes: 4 additions & 2 deletions packages/share_plus/share_plus/lib/src/share_plus_linux.dart
Expand Up @@ -9,11 +9,13 @@ import 'package:url_launcher_platform_interface/url_launcher_platform_interface.

/// The Linux implementation of SharePlatform.
class SharePlusLinuxPlugin extends SharePlatform {
final urlLauncher = UrlLauncherLinux();
SharePlusLinuxPlugin(this.urlLauncher);

final UrlLauncherPlatform urlLauncher;

/// Register this dart class as the platform implementation for linux
static void registerWith() {
SharePlatform.instance = SharePlusLinuxPlugin();
SharePlatform.instance = SharePlusLinuxPlugin(UrlLauncherLinux());
}

/// Share text.
Expand Down
10 changes: 6 additions & 4 deletions packages/share_plus/share_plus/lib/src/share_plus_web.dart
Expand Up @@ -10,18 +10,20 @@ import 'package:url_launcher_web/url_launcher_web.dart';

/// The web implementation of [SharePlatform].
class SharePlusWebPlugin extends SharePlatform {
final urlLauncher = UrlLauncherPlugin();
final UrlLauncherPlatform urlLauncher;

/// Registers this class as the default instance of [SharePlatform].
static void registerWith(Registrar registrar) {
SharePlatform.instance = SharePlusWebPlugin();
SharePlatform.instance = SharePlusWebPlugin(UrlLauncherPlugin());
}

final html.Navigator _navigator;

/// A constructor that allows tests to override the window object used by the plugin.
SharePlusWebPlugin({@visibleForTesting html.Navigator? debugNavigator})
: _navigator = debugNavigator ?? html.window.navigator;
SharePlusWebPlugin(
this.urlLauncher, {
@visibleForTesting html.Navigator? debugNavigator,
}) : _navigator = debugNavigator ?? html.window.navigator;

/// Share text
@override
Expand Down
Expand Up @@ -11,13 +11,15 @@ import 'package:url_launcher_windows/url_launcher_windows.dart';
/// The fallback Windows implementation of [SharePlatform], for older Windows versions.
///
class SharePlusWindowsPlugin extends SharePlatform {
final urlLauncher = UrlLauncherWindows();
SharePlusWindowsPlugin(this.urlLauncher);

final UrlLauncherPlatform urlLauncher;

/// If the modern Share UI i.e. `DataTransferManager` is not available, then use this Dart class instead of platform specific implementation.
///
static void registerWith() {
if (!VersionHelper.instance.isWindows10RS5OrGreater) {
SharePlatform.instance = SharePlusWindowsPlugin();
SharePlatform.instance = SharePlusWindowsPlugin(UrlLauncherWindows());
}
}

Expand Down
Expand Up @@ -11,29 +11,26 @@ void main() {
});
test('url encoding is correct for &', () async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await SharePlusLinuxPlugin().share('foo&bar', subject: 'bar&foo');
await SharePlusLinuxPlugin(mock).share('foo&bar', subject: 'bar&foo');

expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar');
});

// see https://github.com/dart-lang/sdk/issues/43838#issuecomment-823551891
test('url encoding is correct for spaces', () async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await SharePlusLinuxPlugin().share('foo bar', subject: 'bar foo');
await SharePlusLinuxPlugin(mock).share('foo bar', subject: 'bar foo');

expect(mock.url, 'mailto:?subject=bar%20foo&body=foo%20bar');
});

test('throws when url_launcher can\'t launch uri', () async {
final mock = MockUrlLauncherPlatform();
mock.canLaunchMockValue = false;
UrlLauncherPlatform.instance = mock;

expect(() async => await SharePlusLinuxPlugin().share('foo bar'),
expect(() async => await SharePlusLinuxPlugin(mock).share('foo bar'),
throwsException);
});
}
Expand Down
Expand Up @@ -31,9 +31,8 @@ void main() {
'url encoding is correct for &',
() async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await SharePlusWindowsPlugin().share('foo&bar', subject: 'bar&foo');
await SharePlusWindowsPlugin(mock).share('foo&bar', subject: 'bar&foo');

expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar');
},
Expand All @@ -45,9 +44,8 @@ void main() {
'url encoding is correct for spaces',
() async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await SharePlusWindowsPlugin().share('foo bar', subject: 'bar foo');
await SharePlusWindowsPlugin(mock).share('foo bar', subject: 'bar foo');

expect(mock.url, 'mailto:?subject=bar%20foo&body=foo%20bar');
},
Expand All @@ -59,9 +57,8 @@ void main() {
() async {
final mock = MockUrlLauncherPlatform();
mock.canLaunchMockValue = false;
UrlLauncherPlatform.instance = mock;

expect(() async => await SharePlusWindowsPlugin().share('foo bar'),
expect(() async => await SharePlusWindowsPlugin(mock).share('foo bar'),
throwsException);
},
skip: VersionHelper.instance.isWindows10RS5OrGreater,
Expand Down

0 comments on commit 167c604

Please sign in to comment.