-
Notifications
You must be signed in to change notification settings - Fork 76
fix: add support for non-root base-href #75
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
8892812
31526ae
606ade6
b4114a9
e948054
9e1fd5b
39f6598
f2542bd
b8d0b58
bd3e7a7
4900aaf
ac4e069
a7ddb22
897dca4
f5d5bc4
8f38c22
bb3d4ca
be5d5f5
bb5d60d
12cd725
8f1ed7a
3886996
27f6d07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
holzgeist marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import 'dart:js_interop'; | ||
| import 'dart:ui_web' as ui_web; | ||
|
|
||
| import 'package:web/web.dart'; | ||
| import 'package:web/web.dart' as web; | ||
diegotori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// This is an implementation of the `import_js_library` plugin that is used | ||
| /// until that plugin is migrated to null safety. | ||
|
|
@@ -18,19 +19,23 @@ Future<void> importJsLibrary( | |
| } | ||
|
|
||
| String _libraryUrl(String url, String pluginName) { | ||
| // Added suggested changes as per | ||
| // https://github.com/fluttercommunity/wakelock_plus/issues/19#issuecomment-2301963609 | ||
| if (url.startsWith('./')) { | ||
| url = url.replaceFirst('./', ''); | ||
| return './assets/packages/$pluginName/$url'; | ||
| } | ||
|
|
||
| if (url.startsWith('assets/')) { | ||
| return './assets/packages/$pluginName/$url'; | ||
| } else { | ||
| return url; | ||
| return ui_web.assetManager.getAssetUrl( | ||
| 'packages/$pluginName/$url', | ||
| ); | ||
| } | ||
|
|
||
| return url; | ||
| } | ||
|
|
||
| HTMLScriptElement _createScriptTag(String library) { | ||
| final script = document.createElement('script') as HTMLScriptElement | ||
| web.HTMLScriptElement _createScriptTag(String library) { | ||
| final script = web.document.createElement('script') as web.HTMLScriptElement | ||
| ..type = 'text/javascript' | ||
| ..charset = 'utf-8' | ||
| ..async = true | ||
|
|
@@ -42,32 +47,44 @@ HTMLScriptElement _createScriptTag(String library) { | |
| /// Future that resolves when all load. | ||
| Future<void> _importJSLibraries(List<String> libraries) { | ||
| final loading = <Future<void>>[]; | ||
| final head = document.head; | ||
| final head = web.document.head; | ||
|
|
||
| for (final library in libraries) { | ||
| if (!_isImported(library)) { | ||
| final scriptTag = _createScriptTag(library); | ||
| head!.appendChild(scriptTag); | ||
| loading.add(scriptTag.onLoad.first); | ||
| scriptTag.onError.listen((event) { | ||
| final scriptElement = event.srcElement is web.HTMLScriptElement | ||
| ? event.srcElement as web.HTMLScriptElement | ||
| : null; | ||
| if (scriptElement != null) { | ||
| loading.add( | ||
| Future.error( | ||
| Exception('Error loading: ${scriptElement.src}'), | ||
| ), | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return Future.wait(loading); | ||
| } | ||
|
|
||
| bool _isImported(String url) { | ||
| final head = document.head!; | ||
| final head = web.document.head!; | ||
| return _isLoaded(head, url); | ||
| } | ||
|
|
||
| bool _isLoaded(HTMLHeadElement head, String url) { | ||
| bool _isLoaded(web.HTMLHeadElement head, String url) { | ||
| if (url.startsWith('./')) { | ||
| url = url.replaceFirst('./', ''); | ||
| } | ||
| for (int i = 0; i < head.children.length; i++) { | ||
| final element = head.children.item(i)!; | ||
| if (element.instanceOfString('HTMLScriptElement')) { | ||
| if ((element as HTMLScriptElement).src.endsWith(url)) { | ||
| if ((element as web.HTMLScriptElement).src.endsWith(url)) { | ||
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,14 @@ import 'package:wakelock_plus/src/wakelock_plus_web_plugin.dart'; | |
| import 'package:wakelock_plus/wakelock_plus.dart'; | ||
| import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart'; | ||
|
|
||
| /// | ||
| /// Run these tests with: | ||
| /// flutter run -d chrome test/wakelock_plus_web_plugin_test.dart | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is better than nothing, but super non-standard! If you want to run tests like this, I'd suggest you try to migrate them to https://github.com/flutter/packages/tree/main/packages/video_player/video_player_web/example <- this "example" actually contains the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ditman we already have those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, we also have some I'd recommend using either
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ditman Looks like even after porting this test to use @TestOn('browser')
library;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:wakelock_plus/src/wakelock_plus_web_plugin.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
// ignore: depend_on_referenced_packages
import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart';
///
/// Run these tests with:
/// flutter run -d chrome test/wakelock_plus_web_plugin_test.dart
///
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('$WakelockPlusWebPlugin', () {
setUpAll(() async {
WakelockPlusPlatformInterface.instance = WakelockPlusWebPlugin();
});
tearDown(() async {
await WakelockPlus.disable();
});
testWidgets('$WakelockPlusWebPlugin set as default instance',
(tester) async {
expect(
WakelockPlusPlatformInterface.instance, isA<WakelockPlusWebPlugin>());
});
testWidgets('initially disabled', (tester) async {
expect(WakelockPlus.enabled, completion(isFalse));
});
testWidgets('enable', (tester) async {
await WakelockPlus.enable();
expect(WakelockPlus.enabled, completion(isTrue));
});
testWidgets('enable more than once', (tester) async {
await WakelockPlus.enable();
await WakelockPlus.enable();
await WakelockPlus.enable();
expect(WakelockPlus.enabled, completion(isTrue));
});
testWidgets('disable', (tester) async {
await WakelockPlus.enable();
await WakelockPlus.disable();
expect(WakelockPlus.enabled, completion(isFalse));
});
testWidgets('disable more than once', (tester) async {
await WakelockPlus.enable();
await WakelockPlus.disable();
await WakelockPlus.disable();
await WakelockPlus.disable();
expect(WakelockPlus.enabled, completion(isFalse));
});
testWidgets('toggle', (tester) async {
await WakelockPlus.toggle(enable: true);
expect(WakelockPlus.enabled, completion(isTrue));
await WakelockPlus.toggle(enable: false);
expect(WakelockPlus.enabled, completion(isFalse));
});
});
}
Run with while in the example folder: flutter drive -d web-server --web-port 7357 --browser-name chrome --driver test_driver/integration_test.dart --target integration_test/wakelock_plus_web_plugin_test.dart
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bottom line, what I'm looking for is an integration test that runs on a browser, that can also be spun up in a CI context. Not sure which one is the right approach, since the above is not working at all when using
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect the problem is that the If you inject a test app that has a button that enables/disables wakelock, and you trigger the button through the test driver, this should work, and assets would be available. I think the code here is attempting to run Wakelock in the test runner code, not the application under test? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I don't have time to try my solution right now, but I think the migration to an integration test with flutter driver might be inescapable if we want to test app assets)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what you're saying is that I should instead inject the Example App, get a lock on the button that toggles the wakelock, and then make assertions from there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's what I'm saying... What I'm not sure now is that the assets are going to be available when the example app runs in flutter drive mode, as you're saying (because your test looks all right to me). As a reference, a few examples of tests that "inject an app and then look at the DOM of the page or something else" to do integration testing:
Neither of those use assets, though. Here's an example of an integration test that uses assets: |
||
| /// | ||
| void main() { | ||
| group('$WakelockPlusWebPlugin', () { | ||
| setUpAll(() async { | ||
| // todo: the web tests do not work as the JS library import does not work. | ||
| // todo: the web tests do not work as the JS library import does not work when using flutter run test --platform chrome. | ||
| WakelockPlusPlatformInterface.instance = WakelockPlusWebPlugin(); | ||
| }); | ||
|
|
||
|
|
@@ -24,16 +28,23 @@ void main() { | |
|
|
||
| test('enable', () async { | ||
| await WakelockPlus.enable(); | ||
| // Wait a bit for web to enable the wakelock | ||
| await Future.delayed(const Duration(milliseconds: 50)); | ||
| expect(WakelockPlus.enabled, completion(isTrue)); | ||
| }); | ||
|
|
||
| test('disable', () async { | ||
| await WakelockPlus.enable(); | ||
| // Wait a bit for web to enable the wakelock | ||
| await Future.delayed(const Duration(milliseconds: 50)); | ||
| await WakelockPlus.disable(); | ||
| expect(WakelockPlus.enabled, completion(isFalse)); | ||
| }); | ||
|
|
||
| test('toggle', () async { | ||
| await WakelockPlus.toggle(enable: true); | ||
| // Wait a bit for web to enable the wakelock | ||
| await Future.delayed(const Duration(milliseconds: 50)); | ||
| expect(WakelockPlus.enabled, completion(isTrue)); | ||
|
|
||
| await WakelockPlus.toggle(enable: false); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.