Test creation of Android AlertDialogs with a platform view context (#53980)

This commit is contained in:
Amir Hardon
2020-04-06 12:36:01 -07:00
committed by GitHub
parent 2d623278e7
commit 80bc3f4a3c
4 changed files with 169 additions and 30 deletions

View File

@@ -4,11 +4,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'motion_events_page.dart';
import 'page.dart';
import 'wm_integrations.dart';
final List<PageWidget> _allPages = <PageWidget>[
const MotionEventsPage(),
const WindowManagerIntegrationsPage(),
];
void main() {
@@ -20,17 +23,20 @@ class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _allPages.length,
itemBuilder: (_, int index) => ListTile(
title: Text(_allPages[index].title),
key: _allPages[index].tileKey,
onTap: () => _pushPage(context, _allPages[index]),
),
body: ListView(
children: _allPages.map((PageWidget p) => _buildPageListTile(context, p)).toList(),
),
);
}
Widget _buildPageListTile(BuildContext context, PageWidget page) {
return ListTile(
title: Text(page.title),
key: page.tileKey,
onTap: () { _pushPage(context, page); },
);
}
void _pushPage(BuildContext context, PageWidget page) {
Navigator.of(context).push(MaterialPageRoute<void>(
builder: (_) => Scaffold(

View File

@@ -0,0 +1,104 @@
// Copyright 2014 The Flutter Authors. 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:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'page.dart';
class WindowManagerIntegrationsPage extends PageWidget {
const WindowManagerIntegrationsPage()
: super('Window Manager Integrations Tests', const ValueKey<String>('WmIntegrationsListTile'));
@override
Widget build(BuildContext context) => WindowManagerBody();
}
class WindowManagerBody extends StatefulWidget {
@override
State<WindowManagerBody> createState() => WindowManagerBodyState();
}
enum _LastTestStatus {
pending,
success,
error
}
class WindowManagerBodyState extends State<WindowManagerBody> {
MethodChannel viewChannel;
_LastTestStatus lastTestStatus = _LastTestStatus.pending;
String lastError;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Window Manager Integrations'),
),
body: Column(
children: <Widget>[
SizedBox(
height: 300,
child: AndroidView(
viewType: 'simple_view',
onPlatformViewCreated: onPlatformViewCreated,
),
),
if (lastTestStatus != _LastTestStatus.pending) _statusWidget(),
if (viewChannel != null) RaisedButton(
key: const ValueKey<String>('ShowAlertDialog'),
child: const Text('SHOW ALERT DIALOG'),
onPressed: onShowAlertDialogPressed,
),
],
),
);
}
Widget _statusWidget() {
assert(lastTestStatus != _LastTestStatus.pending);
final String message = lastTestStatus == _LastTestStatus.success ? 'Success' : lastError;
return Container(
color: lastTestStatus == _LastTestStatus.success ? Colors.green : Colors.red,
child: Text(
message,
key: const ValueKey<String>('Status'),
style: TextStyle(
color: lastTestStatus == _LastTestStatus.error ? Colors.yellow : null,
),
),
);
}
Future<void> onShowAlertDialogPressed() async {
if (lastTestStatus != _LastTestStatus.pending) {
setState(() {
lastTestStatus = _LastTestStatus.pending;
});
}
try {
await viewChannel.invokeMethod<void>('showAlertDialog');
setState(() {
lastTestStatus = _LastTestStatus.success;
});
} catch(e) {
setState(() {
lastTestStatus = _LastTestStatus.error;
lastError = '$e';
});
}
}
void onPlatformViewCreated(int id) {
setState(() {
viewChannel = MethodChannel('simple_view/$id');
});
}
}