snapshot of https://github.com/flutter/samples.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Main Branch CI / Test Flutter beta on ubuntu-latest (push) Has been skipped
Main Branch CI / Test Flutter stable on ubuntu-latest (push) Has been skipped
Main Branch CI / Test Flutter beta on macos-latest (push) Has been cancelled
Main Branch CI / Test Flutter stable on macos-latest (push) Has been cancelled
Some checks failed
Main Branch CI / Test Flutter beta on ubuntu-latest (push) Has been skipped
Main Branch CI / Test Flutter stable on ubuntu-latest (push) Has been skipped
Main Branch CI / Test Flutter beta on macos-latest (push) Has been cancelled
Main Branch CI / Test Flutter stable on macos-latest (push) Has been cancelled
This commit is contained in:
17
platform_channels/test/home_page_test.dart
Normal file
17
platform_channels/test/home_page_test.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
// 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_test/flutter_test.dart';
|
||||
import 'package:platform_channels/main.dart';
|
||||
|
||||
void main() {
|
||||
group('HomePage tests', () {
|
||||
testWidgets('HomePage has multiple Text widgets', (tester) async {
|
||||
await tester.pumpWidget(const MaterialApp(home: HomePage()));
|
||||
|
||||
expect(find.byType(Text), findsWidgets);
|
||||
});
|
||||
});
|
||||
}
|
||||
47
platform_channels/test/src/add_pet_details_test.dart
Normal file
47
platform_channels/test/src/add_pet_details_test.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
// 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/main.dart' as app;
|
||||
|
||||
void main() {
|
||||
group('AddPetDetails tests', () {
|
||||
var petList = <Map>[];
|
||||
|
||||
testWidgets('Enter pet details', (tester) async {
|
||||
tester.binding.defaultBinaryMessenger.setMockDecodedMessageHandler(
|
||||
const BasicMessageChannel<dynamic>(
|
||||
'jsonMessageCodecDemo',
|
||||
JSONMessageCodec(),
|
||||
),
|
||||
(dynamic message) async {
|
||||
petList.add(message as Map);
|
||||
},
|
||||
);
|
||||
var router = app.router('/petListScreen/addPetDetails');
|
||||
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
|
||||
|
||||
// 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');
|
||||
|
||||
// Navigate back to /petListScreen
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
router.routeInformationProvider.value.uri.path,
|
||||
'/petListScreen',
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
72
platform_channels/test/src/event_channel_demo_test.dart
Normal file
72
platform_channels/test/src/event_channel_demo_test.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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/event_channel_demo.dart';
|
||||
|
||||
void main() {
|
||||
group('EventChannel Demo tests', () {
|
||||
final sensorValues = [1.3556, 2.3, -0.12];
|
||||
setUpAll(() {
|
||||
// By default EventChannel uses StandardMethodCodec to communicate with
|
||||
// platform.
|
||||
const standardMethod = StandardMethodCodec();
|
||||
|
||||
// This function handles the incoming messages from the platform. It
|
||||
// calls the BinaryMessenger.setMessageHandler registered for the EventChannel
|
||||
// and add the incoming message to the StreamController used by the EventChannel
|
||||
// after decoding the message with codec used by the EventChannel.
|
||||
void emitValues(ByteData? event) {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.handlePlatformMessage('eventChannelDemo', event, (reply) {});
|
||||
}
|
||||
|
||||
// Register a mock for EventChannel. EventChannel under the hood uses
|
||||
// MethodChannel to listen and cancel the created stream.
|
||||
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMessageHandler('eventChannelDemo', (message) async {
|
||||
// Decode the message into MethodCallHandler.
|
||||
final methodCall = standardMethod.decodeMethodCall(message);
|
||||
|
||||
if (methodCall.method == 'listen') {
|
||||
// Emit new sensor values.
|
||||
emitValues(
|
||||
standardMethod.encodeSuccessEnvelope(sensorValues),
|
||||
);
|
||||
emitValues(null);
|
||||
return standardMethod.encodeSuccessEnvelope(null);
|
||||
} else if (methodCall.method == 'cancel') {
|
||||
return standardMethod.encodeSuccessEnvelope(null);
|
||||
} else {
|
||||
fail('Expected listen or cancel');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('EventChannel AccelerometerReadings Stream test', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(const MaterialApp(home: EventChannelDemo()));
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Check the values of axis. The value is rounded to 3 decimal places.
|
||||
expect(
|
||||
find.text('x axis: ${sensorValues[0].toStringAsFixed(3)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text('y axis: ${sensorValues[1].toStringAsFixed(3)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text('z axis: ${sensorValues[2].toStringAsFixed(3)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
46
platform_channels/test/src/method_channel_demo_test.dart
Normal file
46
platform_channels/test/src/method_channel_demo_test.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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/method_channel_demo.dart';
|
||||
|
||||
void main() {
|
||||
group('MethodChannelDemo tests', () {
|
||||
testWidgets('MethodChannelDemo count test', (tester) async {
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
const MethodChannel('methodChannelDemo'),
|
||||
(call) async {
|
||||
var count = call.arguments['count'] as int;
|
||||
if (call.method == 'increment') {
|
||||
return ++count;
|
||||
} else if (call.method == 'decrement') {
|
||||
return --count;
|
||||
}
|
||||
|
||||
return MissingPluginException();
|
||||
},
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(home: MethodChannelDemo()),
|
||||
);
|
||||
|
||||
// Initially the value of count should be 0.
|
||||
expect(find.text('Value of count is 0'), findsOneWidget);
|
||||
|
||||
// Tap the ElevatedButton with Icons.add to increment the value of count.
|
||||
await tester.tap(find.byIcon(Icons.add));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Value of count is 1'), findsOneWidget);
|
||||
|
||||
// Tap the ElevatedButton with Icons.remove to decrement the value of count.
|
||||
await tester.tap(find.byIcon(Icons.remove));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Value of count is 0'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
96
platform_channels/test/src/pet_list_screen_test.dart
Normal file
96
platform_channels/test/src/pet_list_screen_test.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
// 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', () {
|
||||
const basicMessageChannel = BasicMessageChannel<String?>(
|
||||
'stringCodecDemo',
|
||||
StringCodec(),
|
||||
);
|
||||
|
||||
var petList = [
|
||||
{'petType': 'Dog', 'breed': 'Pug'},
|
||||
];
|
||||
|
||||
PetListModel? petListModel;
|
||||
|
||||
setUpAll(() {
|
||||
// Mock for the pet list received from the platform.
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockDecodedMessageHandler(basicMessageChannel, (
|
||||
message,
|
||||
) async {
|
||||
petListModel = PetListModel.fromJson(message!);
|
||||
return null;
|
||||
});
|
||||
|
||||
// Mock for the index received from the Dart to delete the pet details,
|
||||
// and send the updated pet list back to Dart.
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockDecodedMessageHandler(
|
||||
const BasicMessageChannel<ByteData?>(
|
||||
'binaryCodecDemo',
|
||||
BinaryCodec(),
|
||||
),
|
||||
(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 null;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
41
platform_channels/test/src/platform_image_demo_test.dart
Normal file
41
platform_channels/test/src/platform_image_demo_test.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
// 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/platform_image_demo.dart';
|
||||
|
||||
void main() {
|
||||
group('Platform Image Demo tests', () {
|
||||
testWidgets('Platform Image test', (tester) async {
|
||||
tester.binding.defaultBinaryMessenger.setMockDecodedMessageHandler(
|
||||
const BasicMessageChannel<dynamic>(
|
||||
'platformImageDemo',
|
||||
StandardMessageCodec(),
|
||||
),
|
||||
(dynamic message) async {
|
||||
var byteData = await rootBundle.load(
|
||||
'assets/eat_new_orleans.jpg',
|
||||
);
|
||||
return byteData.buffer.asUint8List();
|
||||
},
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(home: PlatformImageDemo()),
|
||||
);
|
||||
|
||||
// Initially a PlaceHolder is displayed when imageData is null.
|
||||
expect(find.byType(Placeholder), findsOneWidget);
|
||||
expect(find.byType(Image), findsNothing);
|
||||
|
||||
// Tap on ElevatedButton to get Image.
|
||||
await tester.tap(find.byType(FilledButton));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(Placeholder), findsNothing);
|
||||
expect(find.byType(Image), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user