Unverified Commit 80bc3f4a authored by Amir Hardon's avatar Amir Hardon Committed by GitHub

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

parent 2d623278
......@@ -4,35 +4,37 @@
package io.flutter.integration.platformviews;
import android.app.AlertDialog;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
public class SimplePlatformView implements PlatformView, MethodChannel.MethodCallHandler {
private final View mView;
private final MethodChannel mMethodChannel;
private final TouchPipe mTouchPipe;
private final View view;
private final MethodChannel methodChannel;
private final io.flutter.integration.platformviews.TouchPipe touchPipe;
SimplePlatformView(Context context, MethodChannel methodChannel) {
mMethodChannel = methodChannel;
mView = new View(context) {
this.methodChannel = methodChannel;
view = new View(context) {
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
};
mView.setBackgroundColor(0xff0000ff);
mMethodChannel.setMethodCallHandler(this);
mTouchPipe = new TouchPipe(mMethodChannel, mView);
view.setBackgroundColor(0xff0000ff);
this.methodChannel.setMethodCallHandler(this);
touchPipe = new io.flutter.integration.platformviews.TouchPipe(this.methodChannel, view);
}
@Override
public View getView() {
return mView;
return view;
}
@Override
......@@ -43,14 +45,28 @@ public class SimplePlatformView implements PlatformView, MethodChannel.MethodCal
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
case "pipeTouchEvents":
mTouchPipe.enable();
touchPipe.enable();
result.success(null);
return;
case "stopTouchEvents":
mTouchPipe.disable();
touchPipe.disable();
result.success(null);
return;
case "showAlertDialog":
showAlertDialog(result);
return;
}
result.notImplemented();
}
private void showAlertDialog(MethodChannel.Result result) {
Context context = view.getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView textView = new TextView(context);
textView.setText("Alert!");
builder.setView(textView);
final AlertDialog alertDialog = builder.show();
result.success(null);
}
}
......@@ -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(
......
// 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');
});
}
}
......@@ -17,16 +17,29 @@ Future<void> main() async {
driver.close();
});
group('MotionEvents tests ', () {
test('recomposition', () async {
final SerializableFinder motionEventsListTile =
find.byValueKey('MotionEventsListTile');
await driver.tap(motionEventsListTile);
await driver.waitFor(find.byValueKey('PlatformView'));
final String errorMessage = await driver.requestData('run test');
expect(errorMessage, '');
},
// TODO(amirh): enable this test https://github.com/flutter/flutter/issues/54022
skip: true);
});
test('MotionEvent recomposition', () async {
final SerializableFinder motionEventsListTile =
find.byValueKey('MotionEventsListTile');
await driver.tap(motionEventsListTile);
await driver.waitFor(find.byValueKey('PlatformView'));
final String errorMessage = await driver.requestData('run test');
expect(errorMessage, '');
},
// TODO(amirh): enable this test https://github.com/flutter/flutter/issues/54022
skip: true);
test('AlertDialog from platform view context', () async {
final SerializableFinder wmListTile =
find.byValueKey('WmIntegrationsListTile');
await driver.tap(wmListTile);
final SerializableFinder showAlertDialog = find.byValueKey('ShowAlertDialog');
await driver.waitFor(showAlertDialog);
await driver.tap(showAlertDialog);
final String status = await driver.getText(find.byValueKey('Status'));
expect(status, 'Success');
},
// TODO(amirh): enable this test when https://github.com/flutter/flutter/issues/34248 is fixed.
skip: true,
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment