Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.1.4

- (Null safety) Remove dependency on `package:platform_detect`
- Port unit tests to run with `flutter drive`

# 0.1.3+2

- Fix a typo in a test name and fix some style inconsistencies.
Expand Down
31 changes: 21 additions & 10 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@ import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:meta/meta.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

import 'package:platform_detect/platform_detect.dart' show browser;

const _safariTargetTopSchemes = {
'mailto',
'tel',
'sms',
};
String _getUrlScheme(String url) => Uri.tryParse(url)?.scheme;

bool _isSafariTargetTopScheme(String url) =>
_safariTargetTopSchemes.contains(_getUrlScheme(url));

// Taken from package:platform_detect.
bool _navigatorIsSafari(html.Navigator navigator) {
// An web view running in an iOS app does not have a 'Version/X.X.X' string in the appVersion
final vendor = navigator.vendor;
final appVersion = navigator.appVersion;
return vendor != null &&
vendor.contains('Apple') &&
appVersion != null &&
appVersion.contains('Version');
}

/// The web implementation of [UrlLauncherPlatform].
///
/// This class implements the `package:url_launcher` functionality for the web.
class UrlLauncherPlugin extends UrlLauncherPlatform {
html.Window _window;
bool _isSafari = false;

// The set of schemes that can be handled by the plugin
static final _supportedSchemes = {
Expand All @@ -26,19 +40,16 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
}.union(_safariTargetTopSchemes);

/// A constructor that allows tests to override the window object used by the plugin.
UrlLauncherPlugin({@visibleForTesting html.Window window})
: _window = window ?? html.window;
UrlLauncherPlugin({@visibleForTesting html.Window debugWindow})
: _window = debugWindow ?? html.window {
_isSafari = _navigatorIsSafari(_window.navigator);
}

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

String _getUrlScheme(String url) => Uri.tryParse(url)?.scheme;

bool _isSafariTargetTopScheme(String url) =>
_safariTargetTopSchemes.contains(_getUrlScheme(url));

/// Opens the given [url] in the specified [webOnlyWindowName].
///
/// Returns the newly created window.
Expand All @@ -47,7 +58,7 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
// We need to open mailto, tel and sms urls on the _top window context on safari browsers.
// See https://github.com/flutter/flutter/issues/51461 for reference.
final target = webOnlyWindowName ??
((browser.isSafari && _isSafariTargetTopScheme(url)) ? '_top' : '');
((_isSafari && _isSafariTargetTopScheme(url)) ? '_top' : '');
return _window.open(url, target);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/u
# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.1.3+2
version: 0.1.4

flutter:
plugin:
Expand All @@ -15,7 +15,6 @@ flutter:

dependencies:
url_launcher_platform_interface: ^1.0.8
platform_detect: ^1.4.0
flutter:
sdk: flutter
flutter_web_plugins:
Expand All @@ -28,6 +27,8 @@ dev_dependencies:
url_launcher: ^5.2.5
pedantic: ^1.8.0
mockito: ^4.1.1
integration_test:
path: ../../integration_test

environment:
sdk: ">=2.2.0 <3.0.0"
Expand Down
17 changes: 17 additions & 0 deletions packages/url_launcher/url_launcher_web/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Running browser_tests

Make sure you have updated to the latest Flutter master.

1. Check what version of Chrome is running on the machine you're running tests on.

2. Download and install driver for that version from here:
* <https://chromedriver.chromium.org/downloads>

3. Start the driver using `chromedriver --port=4444`

4. Change into the `test` directory of your clone.

5. Run tests: `flutter drive -d web-server --browser-name=chrome --target=test_driver/TEST_NAME_integration.dart`, or (in Linux):

* Single: `./run_test test_driver/TEST_NAME_integration.dart`
* All: `./run_test`
22 changes: 22 additions & 0 deletions packages/url_launcher/url_launcher_web/test/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

/// App for testing
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Text('Testing... Look at the console output for results!');
}
}
22 changes: 22 additions & 0 deletions packages/url_launcher/url_launcher_web/test/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: regular_integration_tests
publish_to: none

environment:
sdk: ">=2.2.2 <3.0.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_driver:
sdk: flutter
flutter_test:
sdk: flutter
http: ^0.12.2
mockito: ^4.1.1
url_launcher_web:
path: ../
integration_test:
path: ../../../integration_test

17 changes: 17 additions & 0 deletions packages/url_launcher/url_launcher_web/test/run_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/bash
if pgrep -lf chromedriver > /dev/null; then
echo "chromedriver is running."

if [ $# -eq 0 ]; then
echo "No target specified, running all tests..."
find test_driver/ -iname *_integration.dart | xargs -n1 -i -t flutter drive -d web-server --web-port=7357 --browser-name=chrome --target='{}'
else
echo "Running test target: $1..."
set -x
flutter drive -d web-server --web-port=7357 --browser-name=chrome --target=$1
fi

else
echo "chromedriver is not running."
fi

Loading