Files
tmail-flutter/integration_test/robots/web/web_composer_code_view_robot.dart

202 lines
7.0 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:patrol/patrol.dart';
import 'package:tmail_ui_user/features/base/model/ui_keys.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:web/web.dart' as web;
import '../../base/core_robot.dart';
import '../../utils/test_timeouts.dart';
import '../../utils/wait_for_condition.dart';
import '../abstract/abstract_composer_code_view_robot.dart';
import 'web_composer_code_view_diagnostics.dart';
import 'web_composer_controller_finder.dart';
class WebComposerCodeViewRobot extends CoreRobot
implements AbstractComposerCodeViewRobot {
WebComposerCodeViewRobot(PatrolIntegrationTester $) : super($);
static const _markerAttribute = 'data-tmail-code-view-xss';
// The editor iframe is same-origin, so `window.top` reaches the app page.
static const _payload =
'<img src="x" onerror="window.top.document.documentElement'
'.setAttribute(\'$_markerAttribute\', \'ran\')">'
'<p>visible text</p>';
static const _scriptOnlyPayload =
'<script>window.top.document.documentElement'
'.setAttribute(\'$_markerAttribute\', \'ran\')</script>';
static const _activeContentSelector = 'script, [onerror], [onload]';
// Long enough for the broken image's error event to fire.
static const _scriptGracePeriod = Duration(seconds: 2);
late final _diagnostics = WebComposerCodeViewDiagnostics(() {
final richText = findWebComposerController($)?.richTextWebController;
return 'codeViewEnabled=${richText?.codeViewEnabled} '
'toggleInFlight=${richText?.isCodeViewToggleInFlight}';
});
@override
Future<void> openEmbedCode() async {
_diagnostics.startObserving();
try {
await _toggleEmbedCode(expectCodeViewEnabled: true);
} catch (_) {
_diagnostics.stopObserving();
rethrow;
}
}
@override
Future<void> leaveEmbedCode() async {
try {
await _toggleEmbedCode(expectCodeViewEnabled: false);
} finally {
_diagnostics.stopObserving();
}
}
Future<void> _toggleEmbedCode({required bool expectCodeViewEnabled}) async {
// A toggle posted before the editor iframe registers its handler is never answered.
await waitForCondition(() {
final composer = findWebComposerController($);
return composer != null &&
!composer.screenDisplayMode.value.isNotContentVisible() &&
!composer.isEmailBodyNotReady;
});
await $(const Key(UiKeys.composerMoreButton)).tap();
await $.pumpAndSettle();
await $(find.text(AppLocalizations().embedCode)).tap();
await $.pumpAndSettle();
await waitForCondition(
() =>
findWebComposerController($)?.richTextWebController?.codeViewEnabled ==
expectCodeViewEnabled,
);
_diagnostics.dump('after toggle, expected codeViewEnabled=$expectCodeViewEnabled');
}
// Fullscreen itself trips a Flutter web semantics assertion under Patrol,
// so this replays its getText/setText round trip directly.
@override
Future<void> writeBackEditorHtml() async {
final editor = findWebComposerController($)!.richTextWebController!.editorController;
editor.setText(await editor.getText());
// setText reports back through the editor's change event.
await Future.delayed(_scriptGracePeriod);
await $.pumpAndTrySettle();
}
@override
Future<void> minimizeAndRestoreComposer() async {
await $(find.byTooltip(AppLocalizations().minimize)).tap();
await $.pumpAndSettle();
await $(find.byTooltip(AppLocalizations().show)).tap();
await $.pumpAndSettle();
await waitForCondition(() => _editorDocument() != null);
}
@override
Future<void> typeActiveContentPayload() => _typeInCodeView(_payload);
@override
Future<void> typeScriptOnlyPayload() => _typeInCodeView(_scriptOnlyPayload);
Future<void> _typeInCodeView(String payload) async {
web.document.documentElement?.removeAttribute(_markerAttribute);
_diagnostics.dump('before enterText');
// Playwright's fill() waits for a visible textarea without a client timeout.
try {
await $.platformAutomator.web
.enterText(
WebSelector(cssOrXpath: 'textarea.note-codable'),
iframeSelector: WebSelector(cssOrXpath: 'iframe'),
text: payload,
)
.timeout(TestTimeouts.medium);
} on TimeoutException {
_diagnostics.dump('enterText still pending after ${TestTimeouts.medium.inSeconds}s');
_diagnostics.stopObserving();
fail('Code view textarea never accepted text within ${TestTimeouts.medium.inSeconds} seconds');
}
// Guards against a vacuous pass: the payload must sit in the code view.
await waitForCondition(
() => _codeViewText()?.contains(_markerAttribute) == true,
);
}
@override
Future<void> pressEscape() async {
await $.platformAutomator.web.pressKey(key: 'Escape');
await $.pumpAndTrySettle();
}
@override
Future<void> expectStillInCodeView() async {
await waitForCondition(
() =>
findWebComposerController($)?.richTextWebController?.codeViewEnabled ==
true,
);
expect(
_codeViewText(),
isNotNull,
reason: 'code view textarea missing while Embed code should stay open',
);
}
@override
Future<void> expectActiveContentDidNotRun() async {
await Future.delayed(_scriptGracePeriod);
await $.pumpAndTrySettle();
expect(
web.document.documentElement?.getAttribute(_markerAttribute),
isNull,
reason: 'HTML typed in the code view ran its event handler',
);
// A browser can refuse to run the handler, so also check the editable
// itself: active markup must never reach it, whatever the exit path.
final editable = _editorDocument()?.querySelector('div.note-editable');
expect(editable, isNotNull, reason: 'the editor is missing after leaving code view');
expect(
editable?.querySelector(_activeContentSelector),
isNull,
reason: 'active markup from the code view reached the editor',
);
}
@override
Future<void> expectCachedBodyHasNoActiveContent() async {
final body = findWebComposerController($)?.textEditorWeb;
expect(body, isNotNull, reason: 'the composer has no cached body');
expect(body, contains('visible text'));
expect(
body!.toLowerCase(),
isNot(anyOf(contains('onerror'), contains('onload'), contains('<script'))),
reason: 'raw code view HTML reached the cached body',
);
}
String? _codeViewText() {
final textarea = _editorDocument()?.querySelector('textarea.note-codable');
return (textarea as web.HTMLTextAreaElement?)?.value;
}
web.Document? _editorDocument() {
final iframes = web.document.querySelectorAll('iframe');
for (var i = 0; i < iframes.length; i++) {
final iframe = iframes.item(i) as web.HTMLIFrameElement;
final document = iframe.contentDocument;
if (document?.querySelector('textarea.note-codable') != null) {
return document;
}
}
return null;
}
}