mirror of
https://github.com/flutter/samples.git
synced 2026-04-01 17:23:18 +00:00
[platform_channels] adds BasicMessageChannel Demo (#484)
This commit is contained in:
37
platform_channels/test/src/add_pet_details_test.dart
Normal file
37
platform_channels/test/src/add_pet_details_test.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2020 The Flutter team. 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/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:platform_channels/src/add_pet_details.dart';
|
||||
|
||||
void main() {
|
||||
group('AddPetDetails tests', () {
|
||||
var petList = <Map>[];
|
||||
|
||||
setUpAll(() {
|
||||
BasicMessageChannel<dynamic>('jsonMessageCodecDemo', JSONMessageCodec())
|
||||
.setMockMessageHandler((dynamic message) async {
|
||||
petList.add(message as Map);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('Enter pet details', (tester) async {
|
||||
await tester.pumpWidget(MaterialApp(home: AddPetDetails()));
|
||||
|
||||
// Enter the breed of cat.
|
||||
await tester.enterText(find.byType(TextField), 'Persian');
|
||||
// Select cat from the pet type.
|
||||
await tester.tap(find.text('Cat'));
|
||||
|
||||
// Initially the list will be empty.
|
||||
expect(petList, isEmpty);
|
||||
await tester.tap(find.byIcon(Icons.add));
|
||||
|
||||
expect(petList, isNotEmpty);
|
||||
expect(petList.last['breed'], 'Persian');
|
||||
});
|
||||
});
|
||||
}
|
||||
83
platform_channels/test/src/pet_list_screen_test.dart
Normal file
83
platform_channels/test/src/pet_list_screen_test.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright 2020 The Flutter team. 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:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:platform_channels/src/pet_list_message_channel.dart';
|
||||
import 'package:platform_channels/src/pet_list_screen.dart';
|
||||
|
||||
void main() {
|
||||
group('PetListScreen tests', () {
|
||||
final basicMessageChannel =
|
||||
BasicMessageChannel('stringCodecDemo', StringCodec());
|
||||
|
||||
var petList = [
|
||||
{
|
||||
'petType': 'Dog',
|
||||
'breed': 'Pug',
|
||||
}
|
||||
];
|
||||
|
||||
PetListModel petListModel;
|
||||
|
||||
setUpAll(() {
|
||||
// Mock for the pet list received from the platform.
|
||||
basicMessageChannel.setMockMessageHandler((message) async {
|
||||
petListModel = PetListModel.fromJson(message);
|
||||
return;
|
||||
});
|
||||
|
||||
// Mock for the index received from the Dart to delete the pet details,
|
||||
// and send the updated pet list back to Dart.
|
||||
BasicMessageChannel('binaryCodecDemo', BinaryCodec())
|
||||
.setMockMessageHandler((message) async {
|
||||
// Convert the ByteData to String.
|
||||
final index = utf8.decoder.convert(message.buffer
|
||||
.asUint8List(message.offsetInBytes, message.lengthInBytes));
|
||||
|
||||
// Remove the pet details at the given index.
|
||||
petList.removeAt(int.parse(index));
|
||||
|
||||
// Send the updated petList back.
|
||||
final map = {'petList': petList};
|
||||
await basicMessageChannel.send(json.encode(map));
|
||||
return;
|
||||
});
|
||||
});
|
||||
|
||||
test('convert json message to PetListModel', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// Initially petListModel will be null.
|
||||
expect(petListModel, isNull);
|
||||
|
||||
// Send the pet list using BasicMessageChannel.
|
||||
final map = {'petList': petList};
|
||||
basicMessageChannel.send(json.encode(map));
|
||||
|
||||
// Get the details of first pet.
|
||||
final petDetails = petListModel.petList.first;
|
||||
expect(petDetails.petType, 'Dog');
|
||||
expect(petDetails.breed, 'Pug');
|
||||
});
|
||||
|
||||
testWidgets('BuildPetList test', (tester) async {
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Scaffold(
|
||||
body: BuildPetList(petListModel.petList),
|
||||
),
|
||||
));
|
||||
|
||||
expect(find.text('Pet type: Dog'), findsOneWidget);
|
||||
expect(find.text('Pet breed: Pug'), findsOneWidget);
|
||||
|
||||
// Delete the pet details.
|
||||
await tester.tap(find.byIcon(Icons.delete).first);
|
||||
expect(petListModel.petList, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user