Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
7b580fd5
Unverified
Commit
7b580fd5
authored
Apr 25, 2022
by
Sai Sandeep Mutyala
Committed by
GitHub
Apr 25, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable TestDefaultBinaryMessenger to intercept all platform channels. (#100049)
parent
46cb9ede
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
1 deletion
+51
-1
test_default_binary_messenger.dart
...s/flutter_test/lib/src/test_default_binary_messenger.dart
+12
-1
test_default_binary_messenger_test.dart
...flutter_test/test/test_default_binary_messenger_test.dart
+39
-0
No files found.
packages/flutter_test/lib/src/test_default_binary_messenger.dart
View file @
7b580fd5
...
...
@@ -8,6 +8,11 @@ import 'dart:ui' as ui;
import
'package:fake_async/fake_async.dart'
;
import
'package:flutter/services.dart'
;
/// A function which takes the name of the method channel, it's handler,
/// platform message and asynchronously returns an encoded response.
typedef
AllMessagesHandler
=
Future
<
ByteData
?>?
Function
(
String
channel
,
MessageHandler
?
handler
,
ByteData
?
message
);
/// A [BinaryMessenger] subclass that is used as the default binary messenger
/// under testing environment.
///
...
...
@@ -116,11 +121,17 @@ class TestDefaultBinaryMessenger extends BinaryMessenger {
// can implement the [checkMockMessageHandler] method.
final
Map
<
String
,
Object
>
_outboundHandlerIdentities
=
<
String
,
Object
>{};
/// Handler that intercepts and responds to outgoing messages, pretending
/// to be the platform, for all channels.
AllMessagesHandler
?
allMessagesHandler
;
@override
Future
<
ByteData
?>?
send
(
String
channel
,
ByteData
?
message
)
{
final
Future
<
ByteData
?>?
resultFuture
;
final
MessageHandler
?
handler
=
_outboundHandlers
[
channel
];
if
(
handler
!=
null
)
{
if
(
allMessagesHandler
!=
null
)
{
resultFuture
=
allMessagesHandler
!(
channel
,
handler
,
message
);
}
else
if
(
handler
!=
null
)
{
resultFuture
=
handler
(
message
);
}
else
{
resultFuture
=
delegate
.
send
(
channel
,
message
);
...
...
packages/flutter_test/test/test_default_binary_messenger_test.dart
View file @
7b580fd5
...
...
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'dart:typed_data'
;
import
'dart:ui'
as
ui
;
import
'package:flutter/services.dart'
;
...
...
@@ -26,6 +27,19 @@ class TestDelegate extends BinaryMessenger {
void
setMessageHandler
(
String
channel
,
MessageHandler
?
handler
)
=>
throw
UnimplementedError
();
}
class
WorkingTestDelegate
extends
BinaryMessenger
{
@override
Future
<
ByteData
?>?
send
(
String
channel
,
ByteData
?
message
)
async
{
return
ByteData
.
sublistView
(
Uint8List
.
fromList
(<
int
>[
1
,
2
,
3
]));
}
// Rest of the API isn't needed for this test.
@override
Future
<
void
>
handlePlatformMessage
(
String
channel
,
ByteData
?
data
,
ui
.
PlatformMessageResponseCallback
?
callback
)
=>
throw
UnimplementedError
();
@override
void
setMessageHandler
(
String
channel
,
MessageHandler
?
handler
)
=>
throw
UnimplementedError
();
}
void
main
(
)
{
testWidgets
(
'Caught exceptions are caught by the test framework'
,
(
WidgetTester
tester
)
async
{
final
BinaryMessenger
delegate
=
TestDelegate
();
...
...
@@ -39,4 +53,29 @@ void main() {
expect
(
error
,
const
RecognizableTestException
());
}
});
testWidgets
(
'Mock MessageHandler is set correctly'
,
(
WidgetTester
tester
)
async
{
final
TestDefaultBinaryMessenger
binaryMessenger
=
TestDefaultBinaryMessenger
(
WorkingTestDelegate
());
binaryMessenger
.
setMockMessageHandler
(
''
,
(
ByteData
?
message
)
async
=>
ByteData
.
sublistView
(
Uint8List
.
fromList
(<
int
>[
2
,
3
,
4
])));
final
ByteData
?
result
=
await
binaryMessenger
.
send
(
''
,
null
);
expect
(
result
?.
buffer
.
asUint8List
(),
Uint8List
.
fromList
(<
int
>[
2
,
3
,
4
]));
});
testWidgets
(
'Mock AllMessagesHandler is set correctly'
,
(
WidgetTester
tester
)
async
{
final
TestDefaultBinaryMessenger
binaryMessenger
=
TestDefaultBinaryMessenger
(
WorkingTestDelegate
());
binaryMessenger
.
allMessagesHandler
=
(
String
channel
,
MessageHandler
?
handler
,
ByteData
?
message
)
async
=>
ByteData
.
sublistView
(
Uint8List
.
fromList
(<
int
>[
2
,
3
,
4
]));
final
ByteData
?
result
=
await
binaryMessenger
.
send
(
''
,
null
);
expect
(
result
?.
buffer
.
asUint8List
(),
Uint8List
.
fromList
(<
int
>[
2
,
3
,
4
]));
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment