TF-4738 Fix response view while drive picker loading display prevent drive picker (#4740)

This commit is contained in:
Dat Vu authored and GitHub committed 2026-07-31 09:17:17 +07:00
1 parent 3e9ce7613e
commit bf804ee25e
10 files changed
+545 -79

No files matched your search

+2
View File
@@ -15,6 +15,8 @@ env:
# "core"). Add more lines here as needed.
CHROME_TEST_PATHS: |
core/test/data/network/download/download_manager_test.dart
workplace/test/presentation/mixin/web_window_message_mixin_web_test.dart
workplace/test/presentation/view/drive_intent_web_view_modal_web_test.dart
jobs:
analyze-test:
@@ -42,8 +42,7 @@ mixin DriveIntentMessageHandlerMixin<T extends StatefulWidget> on State<T> {
void startLoading(DriveIntentLoader intentLoader) {
// Starts the deadline immediately so a platform view that never becomes
// ready (e.g. WebView/iframe init silently failing) still times out.
_readyTimeoutTimer?.cancel();
_readyTimeoutTimer = Timer(readyTimeout, _handleReadyTimeout);
_restartReadyTimeout();
unawaited(_loadIntent(intentLoader));
}
@@ -69,8 +68,22 @@ mixin DriveIntentMessageHandlerMixin<T extends StatefulWidget> on State<T> {
_finish(DrivePickOutcomeFailed(error));
}
void notifyPlatformViewReady() {
void _restartReadyTimeout() {
_readyTimeoutTimer?.cancel();
_readyTimeoutTimer = Timer(readyTimeout, _handleReadyTimeout);
}
void notifyPlatformViewReady({bool viewRecreated = false}) {
_platformViewReady = true;
if (viewRecreated && _phase == _Phase.loadingPage) {
final intent = _resolvedIntent;
if (intent != null) {
// A replacement iframe needs the resolved URL and a fresh deadline.
_restartReadyTimeout();
unawaited(_applyIntent(intent));
}
return;
}
_tryApplyIntent();
}
@@ -32,7 +32,20 @@ class DriveIntentSkeletonLoader extends StatelessWidget {
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(6)),
),
child: mobile ? _buildMobile(context) : _buildWeb(),
// Table layout has no close slot (#4738).
child: mobile
? _buildMobile(context)
: Stack(
children: [
Positioned.fill(child: _buildWeb()),
if (closeButton != null)
Positioned(
top: 12,
right: 17,
child: closeButton!,
),
],
),
);
}
@@ -35,42 +35,51 @@ class DriveIntentWebViewModalShell extends StatelessWidget {
Widget build(BuildContext context) {
final closeButton = TMailButtonWidget.fromIcon(
icon: closeIconPath,
width: 40,
height: 40,
iconSize: 24,
iconColor: const Color(0xFF424244).withValues(alpha: 0.64),
padding: const EdgeInsets.all(12),
padding: const EdgeInsets.all(8),
backgroundColor: Colors.transparent,
hoverColor: Colors.transparent,
hoverColor: const Color(0x14424244),
onTapActionCallback: onClose,
);
return PointerInterceptor(
child: GestureDetector(
onTap: onBarrierTap ?? onClose,
behavior: HitTestBehavior.opaque,
child: Dialog(
backgroundColor: Colors.white,
insetPadding: insetPadding,
shape: shape,
constraints: constraints,
alignment: alignment,
child: GestureDetector(
onTap: () {},
behavior: HitTestBehavior.opaque,
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (haveCloseButton)
Padding(
padding: const EdgeInsetsGeometry.directional(end: 17),
child: closeButton,
),
Expanded(child: child),
],
return GestureDetector(
onTap: onBarrierTap ?? onClose,
behavior: HitTestBehavior.opaque,
child: Dialog(
backgroundColor: Colors.white,
insetPadding: insetPadding,
shape: shape,
constraints: constraints,
alignment: alignment,
child: GestureDetector(
onTap: () {},
behavior: HitTestBehavior.opaque,
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (haveCloseButton)
Padding(
padding: const EdgeInsetsGeometry.directional(end: 17),
child: closeButton,
),
// Keep the iframe mounted across responsive changes (#4738).
Expanded(
key: const ValueKey('drive-shell-content'),
child: child,
),
],
),
if (loadingWidget != null)
Positioned.fill(
child: PointerInterceptor(
child: loadingWidget!(closeButton),
),
),
if (loadingWidget != null)
Positioned.fill(child: loadingWidget!(closeButton)),
],
),
],
),
),
),
@@ -2,6 +2,7 @@ import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:core/presentation/views/button/tmail_button_widget.dart';
import 'package:core/presentation/views/html_viewer/html_iframe_widget.dart';
import 'package:flutter/material.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
import 'package:universal_html/html.dart' as html;
import 'package:workplace/data/model/workplace_intent_request.dart';
import 'package:workplace/domain/entity/workplace_intent.dart';
@@ -38,13 +39,14 @@ class _DriveIntentWebViewModalState extends State<DriveIntentWebViewModal>
}
Widget _loadingWidget({
required bool show,
required bool wideScreen,
TMailButtonWidget? closeButton,
}) {
if (!show) return const SizedBox();
if (wideScreen) {
return DriveIntentSkeletonLoader.table(imageAssets: widget.imageAssets);
return DriveIntentSkeletonLoader.table(
imageAssets: widget.imageAssets,
closeButton: closeButton,
);
}
return DriveIntentSkeletonLoader.list(
imageAssets: widget.imageAssets,
@@ -57,6 +59,7 @@ class _DriveIntentWebViewModalState extends State<DriveIntentWebViewModal>
super.initState();
// The modal owns its listener: bound to its own lifetime, not the opener's
// (a context menu tile pops its route on tap and would tear it down early).
// Registered before the iframe can post its first ready message.
startWindowMessageListener(_forwardMessage);
startLoading(widget.intentLoader);
}
@@ -107,28 +110,37 @@ class _DriveIntentWebViewModalState extends State<DriveIntentWebViewModal>
onBarrierTap: () {
if (!showSkeleton) cancel();
},
loadingWidget: (closeButton) => _loadingWidget(
show: showSkeleton,
wideScreen: isWideScreen,
closeButton: closeButton,
),
loadingWidget: showSkeleton
? (closeButton) => _loadingWidget(
wideScreen: isWideScreen,
closeButton: closeButton,
)
: null,
child: HtmlIframeWidget(
key: const ValueKey('drive-intent-webview'),
onIframeCreated: (iframe) {
final viewRecreated = _iframeElement != null &&
!identical(_iframeElement, iframe);
_iframeElement = iframe;
notifyPlatformViewReady();
notifyPlatformViewReady(viewRecreated: viewRecreated);
},
),
);
return isWideScreen
? shell
: SafeArea(
top: true,
left: false,
right: false,
bottom: false,
child: shell,
);
// Keep the iframe ancestry stable across resize (#4738).
return Stack(
children: [
Positioned.fill(
child: PointerInterceptor(child: const SizedBox.expand()),
),
SafeArea(
top: !isWideScreen,
left: false,
right: false,
bottom: false,
child: shell,
),
],
);
}
@override
@@ -237,6 +237,31 @@ void _applyOrderingTests() {
expect(state.outcomes, isEmpty);
state.cancelReadyTimeoutForTesting();
});
testWidgets('recreated platform view reloads the intent while loading',
(tester) async {
final state = await _buildAndApply(tester);
state.notifyPlatformViewReady(viewRecreated: true);
expect(state.loadCalls, hasLength(2));
expect(state.outcomes, isEmpty);
state.cancelReadyTimeoutForTesting();
});
testWidgets('recreated platform view does not reload an interactive picker',
(tester) async {
final state = await _buildAndApply(tester);
state.onMessage(
raw: _encode({'type': 'intent-$_intentId:readyToUse'}),
origin: _origin,
);
state.notifyPlatformViewReady(viewRecreated: true);
expect(state.loadCalls, hasLength(1));
expect(state.outcomes, isEmpty);
});
}
void _intentLoadFailureTests() {
@@ -440,27 +465,38 @@ class _TimeoutTestState extends State<_TimeoutTestWidget>
Widget build(BuildContext context) => const SizedBox.shrink();
}
void _readyTimeoutTests() {
Future<_TimeoutTestState> _buildTimeoutState(
WidgetTester tester, {
bool notifyReady = true,
}) async {
await tester.pumpWidget(const MaterialApp(home: _TimeoutTestWidget()));
final state = tester.state<_TimeoutTestState>(find.byType(_TimeoutTestWidget));
state.startLoading(() => Future.value(_intent()));
await tester.pump();
if (notifyReady) state.notifyPlatformViewReady();
return state;
}
void _expectTimeoutFailure(_TimeoutTestState state) {
expect(state.outcomes, hasLength(1));
expect(state.outcomes.single, isA<DrivePickOutcomeFailed>());
expect(
(state.outcomes.single as DrivePickOutcomeFailed).error,
isA<DriveIntentTimeoutException>(),
);
}
void _readyTimeoutFiringTests() {
testWidgets('fires Failed(DriveIntentTimeoutException) if ready never arrives', (tester) async {
await tester.pumpWidget(const MaterialApp(home: _TimeoutTestWidget()));
final state = tester.state<_TimeoutTestState>(find.byType(_TimeoutTestWidget));
state.startLoading(() => Future.value(_intent()));
await tester.pump();
state.notifyPlatformViewReady();
final state = await _buildTimeoutState(tester);
await tester.pump(const Duration(milliseconds: 60));
expect(state.outcomes, hasLength(1));
expect(state.outcomes.single, isA<DrivePickOutcomeFailed>());
expect((state.outcomes.single as DrivePickOutcomeFailed).error, isA<DriveIntentTimeoutException>());
_expectTimeoutFailure(state);
});
testWidgets('cancelled if readyToUse arrives before timeout', (tester) async {
await tester.pumpWidget(const MaterialApp(home: _TimeoutTestWidget()));
final state = tester.state<_TimeoutTestState>(find.byType(_TimeoutTestWidget));
state.startLoading(() => Future.value(_intent()));
await tester.pump();
state.notifyPlatformViewReady();
final state = await _buildTimeoutState(tester);
state.onMessage(raw: _encode({'type': 'intent-$_intentId:ready'}), origin: _origin);
state.onMessage(raw: _encode({'type': 'intent-$_intentId:readyToUse'}), origin: _origin);
@@ -470,31 +506,55 @@ void _readyTimeoutTests() {
});
testWidgets('fires Failed(DriveIntentTimeoutException) if readyToUse never arrives, even after ready', (tester) async {
await tester.pumpWidget(const MaterialApp(home: _TimeoutTestWidget()));
final state = tester.state<_TimeoutTestState>(find.byType(_TimeoutTestWidget));
state.startLoading(() => Future.value(_intent()));
await tester.pump();
state.notifyPlatformViewReady();
final state = await _buildTimeoutState(tester);
state.onMessage(raw: _encode({'type': 'intent-$_intentId:ready'}), origin: _origin);
await tester.pump(const Duration(milliseconds: 60));
expect(state.outcomes, hasLength(1));
expect((state.outcomes.single as DrivePickOutcomeFailed).error, isA<DriveIntentTimeoutException>());
_expectTimeoutFailure(state);
});
testWidgets('fires Failed(DriveIntentTimeoutException) if platform view never becomes ready', (tester) async {
await tester.pumpWidget(const MaterialApp(home: _TimeoutTestWidget()));
final state = tester.state<_TimeoutTestState>(find.byType(_TimeoutTestWidget));
state.startLoading(() => Future.value(_intent()));
final state = await _buildTimeoutState(tester, notifyReady: false);
await tester.pump(const Duration(milliseconds: 60));
expect(state.outcomes, hasLength(1));
expect((state.outcomes.single as DrivePickOutcomeFailed).error, isA<DriveIntentTimeoutException>());
_expectTimeoutFailure(state);
});
}
void _recreatedViewTimeoutTests() {
testWidgets('recreated platform view restarts the ready timeout', (tester) async {
final state = await _buildTimeoutState(tester);
await tester.pump(const Duration(milliseconds: 40));
state.notifyPlatformViewReady(viewRecreated: true);
await tester.pump(const Duration(milliseconds: 20));
expect(state.outcomes, isEmpty);
await tester.pump(const Duration(milliseconds: 40));
expect(state.outcomes, hasLength(1));
});
testWidgets('recreated platform view becomes interactive before its new timeout', (tester) async {
final state = await _buildTimeoutState(tester);
await tester.pump(const Duration(milliseconds: 40));
state.notifyPlatformViewReady(viewRecreated: true);
state.onMessage(raw: _encode({'type': 'intent-$_intentId:ready'}), origin: _origin);
state.onMessage(raw: _encode({'type': 'intent-$_intentId:readyToUse'}), origin: _origin);
expect(state.showSkeleton, isFalse);
await tester.pump(const Duration(milliseconds: 60));
expect(state.outcomes, isEmpty);
});
}
void _readyTimeoutTests() {
_readyTimeoutFiringTests();
_recreatedViewTimeoutTests();
}
void main() {
group('DriveIntentMessageHandlerMixin', () {
group('handshake messages', _handshakeMessageTests);
@@ -0,0 +1,83 @@
@TestOn('chrome')
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:universal_html/html.dart' as html;
import 'package:workplace/presentation/mixin/web_window_message_mixin.dart';
class _MessageListenerWidget extends StatefulWidget {
final void Function(String data, String? origin) onMessage;
const _MessageListenerWidget({required this.onMessage});
@override
State<_MessageListenerWidget> createState() => _MessageListenerWidgetState();
}
class _MessageListenerWidgetState extends State<_MessageListenerWidget>
with WebWindowMessageMixin<_MessageListenerWidget> {
@override
void initState() {
super.initState();
startWindowMessageListener(widget.onMessage);
}
@override
void dispose() {
stopWindowMessageListener();
super.dispose();
}
@override
Widget build(BuildContext context) => const SizedBox();
}
void main() {
testWidgets('forwards browser MessageEvents', (tester) async {
String? data;
String? origin;
await tester.pumpWidget(MaterialApp(
home: _MessageListenerWidget(
onMessage: (message, messageOrigin) {
data = message;
origin = messageOrigin;
},
),
));
html.window.dispatchEvent(
html.MessageEvent(
'message',
data: jsonEncode({'type': 'intent-test:readyToUse'}),
origin: Uri.base.origin,
),
);
await tester.pump();
expect(data, jsonEncode({'type': 'intent-test:readyToUse'}));
expect(origin, Uri.base.origin);
});
testWidgets('stops forwarding messages after disposal', (tester) async {
var callCount = 0;
await tester.pumpWidget(MaterialApp(
home: _MessageListenerWidget(
onMessage: (_, __) => callCount++,
),
));
await tester.pumpWidget(const SizedBox());
html.window.dispatchEvent(
html.MessageEvent(
'message',
data: jsonEncode({'type': 'intent-test:readyToUse'}),
origin: Uri.base.origin,
),
);
await tester.pump();
expect(callCount, 0);
});
}
@@ -0,0 +1,60 @@
import 'package:core/presentation/views/button/tmail_button_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:workplace/presentation/model/drive_intent_image_assets.dart';
import 'package:workplace/presentation/view/drive_intent_skeleton_loader.dart';
const _closeButtonKey = ValueKey('close-button');
const _skeletonKey = ValueKey('desktop-skeleton');
const _imageAssets = DriveIntentImageAssets(
driveLogo: 'assets/images/twake-drive-logo.svg',
closeIcon: 'assets/images/ic_close.svg',
searchIcon: 'assets/images/ic_search_bar.svg',
);
void main() {
testWidgets('desktop loading close button matches the design', (tester) async {
var closeCalls = 0;
final closeButton = TMailButtonWidget.fromIcon(
key: _closeButtonKey,
icon: _imageAssets.closeIcon,
width: 40,
height: 40,
iconSize: 24,
padding: const EdgeInsets.all(8),
backgroundColor: Colors.transparent,
onTapActionCallback: () => closeCalls++,
);
await tester.pumpWidget(
MaterialApp(
home: Center(
child: SizedBox(
key: _skeletonKey,
width: 800,
height: 500,
child: DriveIntentSkeletonLoader.table(
imageAssets: _imageAssets,
closeButton: closeButton,
),
),
),
),
);
final closeButtonFinder = find.byKey(_closeButtonKey);
final closeContainerFinder = find.descendant(
of: closeButtonFinder,
matching: find.byType(Container),
);
final skeletonRect = tester.getRect(find.byKey(_skeletonKey));
final closeRect = tester.getRect(closeContainerFinder);
expect(closeRect.size, const Size(40, 40));
expect(closeRect.top, skeletonRect.top + 12);
expect(closeRect.right, skeletonRect.right - 17);
await tester.tap(closeContainerFinder);
expect(closeCalls, 1);
});
}
@@ -0,0 +1,141 @@
import 'package:core/presentation/views/button/tmail_button_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
import 'package:workplace/presentation/view/drive_intent_web_view_modal_shell.dart';
const _closeIcon = 'assets/close.svg';
Widget _shell({
required bool haveCloseButton,
required BoxConstraints constraints,
required EdgeInsets insetPadding,
Widget Function(TMailButtonWidget closeButton)? loadingWidget,
VoidCallback? onClose,
}) {
return MaterialApp(
home: _modalShell(
haveCloseButton: haveCloseButton,
constraints: constraints,
insetPadding: insetPadding,
loadingWidget: loadingWidget,
onClose: onClose,
),
);
}
Widget _modalShell({
required bool haveCloseButton,
required BoxConstraints constraints,
required EdgeInsets insetPadding,
Widget Function(TMailButtonWidget closeButton)? loadingWidget,
VoidCallback? onClose,
}) {
return DriveIntentWebViewModalShell(
closeIconPath: _closeIcon,
onClose: onClose ?? () {},
haveCloseButton: haveCloseButton,
constraints: constraints,
insetPadding: insetPadding,
loadingWidget: loadingWidget,
child: const SizedBox.shrink(),
);
}
void main() {
group('DriveIntentWebViewModalShell', () {
_testLoadingOverlayIsClosable();
_testLoadingOverlayCloseButtonCallback();
_testCloseButtonHoverFeedback();
_testNoPointerInterceptorWithoutLoadingOverlay();
});
}
void _testLoadingOverlayIsClosable() {
testWidgets(
'loading overlay is always closable — receives a close button',
(tester) async {
TMailButtonWidget? received;
await tester.pumpWidget(
_shell(
haveCloseButton: false,
constraints: const BoxConstraints(maxWidth: 800, maxHeight: 677),
insetPadding: const EdgeInsets.all(24),
loadingWidget: (closeButton) {
received = closeButton;
return closeButton;
},
),
);
expect(received, isNotNull);
expect(find.byWidget(received!), findsOneWidget);
expect(
find.ancestor(
of: find.byType(PointerInterceptor),
matching: find.byType(Positioned),
),
findsOneWidget,
);
},
);
}
void _testLoadingOverlayCloseButtonCallback() {
testWidgets('loading overlay close button is wired to onClose', (
tester,
) async {
var closed = 0;
TMailButtonWidget? received;
await tester.pumpWidget(
_shell(
haveCloseButton: false,
constraints: const BoxConstraints(maxWidth: 800, maxHeight: 677),
insetPadding: const EdgeInsets.all(24),
onClose: () => closed++,
loadingWidget: (closeButton) {
received = closeButton;
return closeButton;
},
),
);
// Assert callback wiring without SVG hit-testing.
received!.onTapActionCallback!();
expect(closed, 1);
});
}
void _testCloseButtonHoverFeedback() {
testWidgets('close button keeps Material hover feedback', (tester) async {
await tester.pumpWidget(
_shell(
haveCloseButton: true,
constraints: const BoxConstraints(maxWidth: 800, maxHeight: 677),
insetPadding: const EdgeInsets.all(24),
),
);
final button = tester.widget<TMailButtonWidget>(
find.byType(TMailButtonWidget),
);
expect(button.hoverColor, const Color(0x14424244));
});
}
void _testNoPointerInterceptorWithoutLoadingOverlay() {
testWidgets('does not intercept pointers without a loading overlay', (
tester,
) async {
await tester.pumpWidget(
_shell(
haveCloseButton: false,
constraints: const BoxConstraints(maxWidth: 800, maxHeight: 677),
insetPadding: const EdgeInsets.all(24),
),
);
expect(find.byType(PointerInterceptor), findsNothing);
});
}
@@ -0,0 +1,73 @@
@TestOn('chrome')
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:workplace/data/model/workplace_intent_request.dart';
import 'package:workplace/domain/entity/workplace_intent.dart';
import 'package:workplace/presentation/model/drive_intent_image_assets.dart';
import 'package:workplace/presentation/view/drive_intent_web_view_modal.dart';
const _imageAssets = DriveIntentImageAssets(
driveLogo: 'assets/images/twake-drive-logo.svg',
closeIcon: 'assets/images/ic_close.svg',
searchIcon: 'assets/images/ic_search_bar.svg',
);
const _filePickerConfig = WorkplaceFilePickerConfigRequest(
sharingLink: WorkplaceActionConfigRequest(),
downloadLink: null,
);
Widget _modal(Future<WorkplaceIntent> intent) {
return MaterialApp(
home: DriveIntentWebViewModal(
intentLoader: () => intent,
filePickerConfig: _filePickerConfig,
imageAssets: _imageAssets,
),
);
}
void main() {
testWidgets(
'preserves the iframe platform view in both resize directions (#4738)',
(tester) async {
// The intent never resolves: the modal stays in its loading phase, which
// is exactly the window #4738 regresses in. The iframe lives in the shell
// regardless of the skeleton, so it is mounted throughout.
final intent = Completer<WorkplaceIntent>();
addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});
tester.view.devicePixelRatio = 1;
tester.view.physicalSize = const Size(1280, 800);
await tester.pumpWidget(_modal(intent.future));
await tester.pump();
// The flutter_test web embedder does not attach the real <iframe> to the
// live DOM, so recreation is asserted at the framework level: a stable
// HtmlElementView Element means the platform view — and its iframe — was
// never torn down. findsOneWidget additionally rules out an orphaned view
// leaking alongside a fresh one.
final platformView = find.byType(HtmlElementView);
expect(platformView, findsOneWidget);
final platformViewElement = tester.element(platformView);
// Desktop -> mobile crosses the responsive breakpoint (1280 >= 1200 desktop,
// 375 < 900 mobile), the transition that used to remount the iframe.
tester.view.physicalSize = const Size(375, 800);
await tester.pump();
expect(platformView, findsOneWidget);
expect(identical(platformViewElement, tester.element(platformView)), isTrue);
// Mobile -> desktop: the reverse transition must be just as stable.
tester.view.physicalSize = const Size(1280, 800);
await tester.pump();
expect(platformView, findsOneWidget);
expect(identical(platformViewElement, tester.element(platformView)), isTrue);
},
);
}