[web] move e2e tests from flutter/engine to flutter/flutter (#86119)

* [web] move e2e tests from flutter/engine to flutter/flutter
* flutter update-packages --force-upgrade
This commit is contained in:
Yegor
2021-07-09 15:31:49 -07:00
committed by GitHub
parent 00d9f8df14
commit 78e06e716b
87 changed files with 1196 additions and 272 deletions

View File

@@ -0,0 +1,26 @@
// Copyright 2014 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 'dart:html' as html;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web_e2e_tests/common.dart';
import 'package:web_e2e_tests/image_loading_main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Image loads asset variant based on device pixel ratio',
(WidgetTester tester) async {
await app.main();
await tester.pumpAndSettle();
final List<html.ImageElement> imageElements = findElements('img').cast<html.ImageElement>();
expect(imageElements.length, 2);
expect(imageElements[0].naturalWidth, 1.5 * 64);
expect(imageElements[0].naturalHeight, 1.5 * 64);
expect(imageElements[0].width, 64);
expect(imageElements[0].height, 64);
expect(imageElements[1].width, isNot(0));
});
}

View File

@@ -0,0 +1,7 @@
// Copyright 2014 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:integration_test/integration_test_driver.dart' as test;
Future<void> main() async => test.integrationDriver();

View File

@@ -0,0 +1,56 @@
// Copyright 2014 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 'dart:html' as html;
// platformViewRegistry is exposed in the web version
import 'dart:ui' as ui show platformViewRegistry; // ignore: undefined_shown_name
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web_e2e_tests/platform_messages_main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('platform message for Clipboard.setData reply with future',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
SystemChannels.textInput.setMockMethodCallHandler(null);
// Focus on a TextFormField.
final Finder finder = find.byKey(const Key('input'));
expect(finder, findsOneWidget);
await tester.tap(find.byKey(const Key('input')));
// Focus in input, otherwise clipboard will fail with
// 'document is not focused' platform exception.
html.document.querySelector('input')?.focus();
await Clipboard.setData(const ClipboardData(text: 'sample text'));
}, skip: true); // https://github.com/flutter/flutter/issues/54296
testWidgets('Should create and dispose view embedder',
(WidgetTester tester) async {
int viewInstanceCount = 0;
platformViewsRegistry.getNextPlatformViewId();
// ignore: undefined_prefixed_name, avoid_dynamic_calls
ui.platformViewRegistry.registerViewFactory('MyView', (int viewId) {
++viewInstanceCount;
return html.DivElement();
});
app.main();
await tester.pumpAndSettle();
final Map<String, dynamic> createArgs = <String, dynamic>{
'id': 567,
'viewType': 'MyView',
};
await SystemChannels.platform_views.invokeMethod<void>('create', createArgs);
await SystemChannels.platform_views.invokeMethod<void>('dispose', 567);
expect(viewInstanceCount, 1);
});
}

View File

@@ -0,0 +1,7 @@
// Copyright 2014 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:integration_test/integration_test_driver.dart' as test;
Future<void> main() async => test.integrationDriver();

View File

@@ -0,0 +1,19 @@
// Copyright 2014 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_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web_e2e_tests/profile_diagnostics_main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('App build method exception should form valid FlutterErrorDetails',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
final dynamic appError = tester.takeException();
expect(appError, isA<TypeError>());
});
}

View File

@@ -0,0 +1,7 @@
// Copyright 2014 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:integration_test/integration_test_driver.dart' as test;
Future<void> main() async => test.integrationDriver();

View File

@@ -0,0 +1,29 @@
// Copyright 2014 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';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web_e2e_tests/scroll_wheel_main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Test mousewheel scroll by line', (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
final Finder finder = find.byKey(const Key('scroll-button'));
expect(finder, findsOneWidget);
await tester.tap(finder);
await tester.pumpAndSettle();
await tester.tap(finder);
await tester.pumpAndSettle();
await expectLater(
find.byType(app.MyApp),
matchesGoldenFile('scroll_wheel_by_line'),
);
});
}

View File

@@ -0,0 +1,9 @@
// Copyright 2014 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:web_e2e_tests/screenshot_support.dart' as test;
Future<void> main() async {
await test.runTestWithScreenshots();
}

View File

@@ -9,20 +9,12 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web_e2e_tests/common.dart';
import 'package:web_e2e_tests/text_editing_main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
/// Locate elements in the correct root of the application, whether it is
/// `document` or the new `shadowRoot` of `flt-class-pane`.
List<Node> findElements(String selector) {
final ShadowRoot? shadowRoot = document.querySelector('flt-glass-pane')?.shadowRoot;
return (shadowRoot != null) ?
shadowRoot.querySelectorAll(selector):
document.querySelectorAll(selector);
}
testWidgets('Focused text field creates a native input element',
(WidgetTester tester) async {
app.main();

View File

@@ -0,0 +1,183 @@
// Copyright 2014 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 'dart:async';
import 'dart:html' as html;
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web_e2e_tests/url_strategy_main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Can customize url strategy', (WidgetTester tester) async {
final TestUrlStrategy strategy = TestUrlStrategy.fromEntry(
const TestHistoryEntry('initial state', null, '/'),
);
setUrlStrategy(strategy);
app.appRoutes = <String, WidgetBuilder>{
'/': (BuildContext context) => Container(),
'/foo': (BuildContext context) => Container(),
};
app.main();
await tester.pumpAndSettle();
expect(strategy.getPath(), '/');
final NavigatorState navigator = app.navKey.currentState!;
navigator.pushNamed('/foo');
await tester.pump();
expect(strategy.getPath(), '/foo');
});
}
/// This URL strategy mimics the browser's history as closely as possible
/// while doing it all in memory with no interaction with the browser.
///
/// It keeps a list of history entries and event listeners in memory and
/// manipulates them in order to achieve the desired functionality.
class TestUrlStrategy extends UrlStrategy {
/// Creates a instance of [TestUrlStrategy] with an empty string as the
/// path.
factory TestUrlStrategy() => TestUrlStrategy.fromEntry(const TestHistoryEntry(null, null, ''));
/// Creates an instance of [TestUrlStrategy] and populates it with a list
/// that has [initialEntry] as the only item.
TestUrlStrategy.fromEntry(TestHistoryEntry initialEntry)
: _currentEntryIndex = 0,
history = <TestHistoryEntry>[initialEntry];
@override
String getPath() => currentEntry.url;
@override
dynamic getState() => currentEntry.state;
int _currentEntryIndex;
int get currentEntryIndex => _currentEntryIndex;
final List<TestHistoryEntry> history;
TestHistoryEntry get currentEntry {
assert(withinAppHistory);
return history[_currentEntryIndex];
}
set currentEntry(TestHistoryEntry entry) {
assert(withinAppHistory);
history[_currentEntryIndex] = entry;
}
/// Whether we are still within the history of the Flutter Web app. This
/// remains true until we go back in history beyond the entry where the app
/// started.
bool get withinAppHistory => _currentEntryIndex >= 0;
@override
void pushState(dynamic state, String title, String url) {
assert(withinAppHistory);
_currentEntryIndex++;
// When pushing a new state, we need to remove all entries that exist after
// the current entry.
//
// If the user goes A -> B -> C -> D, then goes back to B and pushes a new
// entry called E, we should end up with: A -> B -> E in the history list.
history.removeRange(_currentEntryIndex, history.length);
history.add(TestHistoryEntry(state, title, url));
}
@override
void replaceState(dynamic state, String title, String url) {
assert(withinAppHistory);
if (url == null || url == '') {
url = currentEntry.url;
}
currentEntry = TestHistoryEntry(state, title, url);
}
/// This simulates the case where a user types in a url manually. It causes
/// a new state to be pushed, and all event listeners will be invoked.
Future<void> simulateUserTypingUrl(String url) {
assert(withinAppHistory);
return _nextEventLoop(() {
pushState(null, '', url);
_firePopStateEvent();
});
}
@override
Future<void> go(int count) {
assert(withinAppHistory);
// Browsers don't move in history immediately. They do it at the next
// event loop. So let's simulate that.
return _nextEventLoop(() {
_currentEntryIndex = _currentEntryIndex + count;
if (withinAppHistory) {
_firePopStateEvent();
}
});
}
final List<html.EventListener> listeners = <html.EventListener>[];
@override
ui.VoidCallback addPopStateListener(html.EventListener fn) {
listeners.add(fn);
return () {
// Schedule a micro task here to avoid removing the listener during
// iteration in [_firePopStateEvent].
scheduleMicrotask(() => listeners.remove(fn));
};
}
/// Simulates the scheduling of a new event loop by creating a delayed future.
/// Details explained here: https://webdev.dartlang.org/articles/performance/event-loop
Future<void> _nextEventLoop(ui.VoidCallback callback) {
return Future<void>.delayed(Duration.zero).then((_) => callback());
}
/// Invokes all the attached event listeners in order of
/// attaching. This method should be called asynchronously to make it behave
/// like a real browser.
void _firePopStateEvent() {
assert(withinAppHistory);
final html.PopStateEvent event = html.PopStateEvent(
'popstate',
<String, dynamic>{'state': currentEntry.state},
);
for (int i = 0; i < listeners.length; i++) {
listeners[i](event);
}
}
@override
String prepareExternalUrl(String internalUrl) => internalUrl;
@override
String toString() {
final List<String> lines = <String>[];
for (int i = 0; i < history.length; i++) {
final TestHistoryEntry entry = history[i];
lines.add(_currentEntryIndex == i ? '* $entry' : ' $entry');
}
return '$runtimeType: [\n${lines.join('\n')}\n]';
}
}
class TestHistoryEntry {
const TestHistoryEntry(this.state, this.title, this.url);
final dynamic state;
final String? title;
final String url;
@override
String toString() {
return '$runtimeType(state:$state, title:"$title", url:"$url")';
}
}

View File

@@ -0,0 +1,9 @@
// Copyright 2014 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:web_e2e_tests/screenshot_support.dart' as test;
Future<void> main() async {
await test.runTestWithScreenshots();
}