main.dart 6.7 KB
Newer Older
1 2 3 4 5
// 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 'package:flutter/material.dart';
6
import 'package:flutter/services.dart';
7 8 9
import 'package:flutter_driver/driver_extension.dart';

void main() {
10 11 12 13 14 15
  // enableFlutterDriverExtension() will disable keyboard,
  // which is required for flutter_driver tests
  // But breaks the XCUITests
  if (const bool.fromEnvironment('ENABLE_DRIVER_EXTENSION')) {
    enableFlutterDriverExtension();
  }
16
  runApp(const MyApp());
17 18 19 20
}

/// The main app entrance of the test
class MyApp extends StatelessWidget {
21
  const MyApp({super.key});
22

23 24 25 26 27 28 29 30 31 32 33 34
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

35
/// A page with several buttons in the center.
36 37 38
///
/// On press the button, a page with platform view should be pushed into the scene.
class MyHomePage extends StatefulWidget {
39
  const MyHomePage({super.key, this.title});
40
  final String? title;
41 42

  @override
43
  State<MyHomePage> createState() => _MyHomePageState();
44 45 46 47 48 49 50
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
51
        title: Text(widget.title ?? ''),
52 53
      ),
      body: Column(children: <Widget>[
54
        TextButton(
55 56 57 58 59
          key: const ValueKey<String>('platform_view_button'),
          child: const Text('show platform view'),
          onPressed: () {
            Navigator.push(
              context,
60 61
              MaterialPageRoute<MergeThreadTestPage>(
                  builder: (BuildContext context) => const MergeThreadTestPage()),
62 63 64 65
            );
          },
        ),
        // Push this button to perform an animation, which ensure the threads are unmerged after the animation.
66
        ElevatedButton(
67 68 69 70
          key: const ValueKey<String>('unmerge_button'),
          child: const Text('Tap to unmerge threads'),
          onPressed: () {},
        ),
71 72 73 74 75 76 77 78 79 80 81
        TextButton(
          key: const ValueKey<String>('platform_view_focus_test'),
          child: const Text('platform view focus test'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute<FocusTestPage>(
                  builder: (BuildContext context) => const FocusTestPage()),
            );
          },
        ),
82 83 84 85 86 87 88 89 90 91 92
        TextButton(
          key: const ValueKey<String>('platform_view_z_order_test'),
          child: const Text('platform view z order test'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute<ZOrderTestPage>(
                  builder: (BuildContext context) => const ZOrderTestPage()),
            );
          },
        ),
93 94 95 96 97
      ]),
    );
  }
}

98 99 100
/// A page to test thread merge for platform view.
class MergeThreadTestPage extends StatelessWidget {
  const MergeThreadTestPage({super.key});
101 102

  static Key button = const ValueKey<String>('plus_button');
103 104 105 106 107

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
108
        title: const Text('Platform View Thread Merge Tests'),
109 110 111
      ),
      body: Column(
        children: <Widget>[
112 113
          const Expanded(
            child: SizedBox(
114
              width: 300,
115
              child: UiKitView(viewType: 'platform_view'),
116
            ),
117
          ),
118
          ElevatedButton(
119 120 121
            key: button,
            child: const Text('button'),
            onPressed: (){},
122
          ),
123 124 125 126 127
        ],
      ),
    );
  }
}
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

/// A page to test platform view focus.
class FocusTestPage extends StatefulWidget {
  const FocusTestPage({super.key});

  @override
  State<FocusTestPage> createState() => _FocusTestPageState();
}

class _FocusTestPageState extends State<FocusTestPage> {

  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _controller.text = 'Flutter Text Field';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Platform View Focus Tests'),
      ),
      body: Column(
        children: <Widget>[
          const SizedBox(
            width: 300,
            height: 50,
            child: UiKitView(viewType: 'platform_text_field'),
          ),
          TextField(
            controller: _controller,
          ),
        ],
      ),
    );
  }
}
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

/// A page to test a platform view in an alert dialog prompt is still tappable.
/// See [this issue](https://github.com/flutter/flutter/issues/118366).
class ZOrderTestPage extends StatefulWidget {
  const ZOrderTestPage({super.key});

  @override
  State<ZOrderTestPage> createState() => _ZOrderTestPageState();
}

class _ZOrderTestPageState extends State<ZOrderTestPage> {
  bool _showBackground = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Platform view z order test'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Visibility(
                visible: _showBackground,
                child: const SizedBox(
                  width: 500,
                  height: 500,
                  child: UiKitView(
                    viewType: 'platform_view',
                    creationParamsCodec: StandardMessageCodec(),
                  ),
                )),
            TextButton(
                onPressed: () {
204
                  showDialog<void>(
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
                      context: context,
                      builder: (BuildContext context) {
                        return const SizedBox(
                          width: 250,
                          height: 250,
                          child: UiKitView(
                            viewType: 'platform_button',
                            creationParamsCodec: StandardMessageCodec(),
                          ),
                        );
                      });

                  // XCUITest fails to query the background platform view,
                  // Since it is covered by the dialog prompt, which removes
                  // semantic nodes underneath.
                  // As a workaround, we show the background with a delay.
                  Future<void>.delayed(const Duration(seconds: 1)).then((void value) {
                    setState(() {
                      _showBackground = true;
                    });
                  });
                },
                child: const Text('Show Alert')),
          ],
        ),
      ),
    );
  }
}