This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Support platform view creation params #42255
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b71f405
[web] Support platform view creation params
mdebbar 94c7373
Merge branch 'main' into creation_params
mdebbar c325e82
add tests for other types
mdebbar 472ccf6
move the assert message to this PR
mdebbar c3e6f16
Merge branch 'main' into creation_params
mdebbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ void main() { | |
|
||
const MethodCodec codec = StandardMethodCodec(); | ||
|
||
typedef PlatformViewFactoryCall = ({int viewId, Object? params}); | ||
|
||
void testMain() { | ||
group('PlatformViewMessageHandler', () { | ||
group('handlePlatformViewCall', () { | ||
|
@@ -109,6 +111,80 @@ void testMain() { | |
reason: | ||
'The response should be a success envelope, with null in it.'); | ||
}); | ||
|
||
test('passes creation params to the factory', () async { | ||
final List<PlatformViewFactoryCall> factoryCalls = <PlatformViewFactoryCall>[]; | ||
contentManager.registerFactory(viewType, (int viewId, {Object? params}) { | ||
factoryCalls.add((viewId: viewId, params: params)); | ||
return createDomHTMLDivElement(); | ||
}); | ||
final PlatformViewMessageHandler messageHandler = PlatformViewMessageHandler( | ||
contentManager: contentManager, | ||
); | ||
|
||
final List<Completer<ByteData?>> completers = <Completer<ByteData?>>[]; | ||
|
||
completers.add(Completer<ByteData?>()); | ||
messageHandler.handlePlatformViewCall( | ||
_getCreateMessage(viewType, 111), | ||
completers.last.complete, | ||
); | ||
|
||
completers.add(Completer<ByteData?>()); | ||
messageHandler.handlePlatformViewCall( | ||
_getCreateMessage(viewType, 222, <dynamic, dynamic>{'foo': 'bar'}), | ||
completers.last.complete, | ||
); | ||
|
||
completers.add(Completer<ByteData?>()); | ||
messageHandler.handlePlatformViewCall( | ||
_getCreateMessage(viewType, 333, 'foobar'), | ||
completers.last.complete, | ||
); | ||
|
||
completers.add(Completer<ByteData?>()); | ||
messageHandler.handlePlatformViewCall( | ||
_getCreateMessage(viewType, 444, <dynamic>[1, null, 'str']), | ||
completers.last.complete, | ||
); | ||
|
||
final List<ByteData?> responses = await Future.wait( | ||
completers.map((Completer<ByteData?> c) => c.future), | ||
); | ||
|
||
for (final ByteData? response in responses) { | ||
expect( | ||
codec.decodeEnvelope(response!), | ||
isNull, | ||
reason: 'The response should be a success envelope, with null in it.', | ||
); | ||
} | ||
|
||
expect(factoryCalls, hasLength(4)); | ||
expect(factoryCalls[0].viewId, 111); | ||
expect(factoryCalls[0].params, isNull); | ||
expect(factoryCalls[1].viewId, 222); | ||
expect(factoryCalls[1].params, <dynamic, dynamic>{'foo': 'bar'}); | ||
Comment on lines
+166
to
+167
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. Do we need an extra case where the param passed is not a map, but a string or an int, or some other simple |
||
expect(factoryCalls[2].viewId, 333); | ||
expect(factoryCalls[2].params, 'foobar'); | ||
expect(factoryCalls[3].viewId, 444); | ||
expect(factoryCalls[3].params, <dynamic>[1, null, 'str']); | ||
}); | ||
|
||
test('fails if the factory returns a non-DOM object', () async { | ||
contentManager.registerFactory(viewType, (int viewId) { | ||
// Return an object that's not a DOM element. | ||
return Object(); | ||
}); | ||
|
||
final PlatformViewMessageHandler messageHandler = | ||
PlatformViewMessageHandler(contentManager: contentManager); | ||
final ByteData? message = _getCreateMessage(viewType, viewId); | ||
|
||
expect(() { | ||
messageHandler.handlePlatformViewCall(message, (_) {}); | ||
}, throwsA(isA<TypeError>())); | ||
}); | ||
}); | ||
|
||
group('"dispose" message', () { | ||
|
@@ -162,12 +238,13 @@ class _FakePlatformViewManager extends PlatformViewManager { | |
} | ||
} | ||
|
||
ByteData? _getCreateMessage(String viewType, int viewId) { | ||
ByteData? _getCreateMessage(String viewType, int viewId, [Object? params]) { | ||
return codec.encodeMethodCall(MethodCall( | ||
'create', | ||
<String, dynamic>{ | ||
'id': viewId, | ||
'viewType': viewType, | ||
if (params != null) 'params': params, | ||
}, | ||
)); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this
assert(content is DomElement, 'Cool error message')
, rather than just the cast? That way we may be able to provide a better error message than the default one coming from Dart?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I don't think
DomElement
is a real type at runtime, so I'm not sure whatcontent is DomElement
is going to check. cc @joshualitt any ideas?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to submit this as is. I can do a follow up PR if you still feel strongly about it.