main.dart 2.99 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
9
import 'package:flutter/scheduler.dart';
10 11 12 13
import 'package:flutter/services.dart';
import 'package:flutter_driver/driver_extension.dart';

import 'src/tests/controls_page.dart';
14
import 'src/tests/headings_page.dart';
15
import 'src/tests/popup_page.dart';
16
import 'src/tests/text_field_page.dart';
17 18

void main() {
19
  timeDilation = 0.05; // remove animations.
20 21 22 23 24 25
  enableFlutterDriverExtension(handler: dataHandler);
  runApp(const TestApp());
}

const MethodChannel kSemanticsChannel = MethodChannel('semantics');

26 27
Future<String> dataHandler(String message) async {
  if (message.contains('getSemanticsNode')) {
28
    final Completer<String> completer = Completer<String>();
29
    final int id = int.tryParse(message.split('#')[1]) ?? 0;
30
    Future<void> completeSemantics([Object _]) async {
31
      final dynamic result = await kSemanticsChannel.invokeMethod<dynamic>('getSemanticsNode', <String, dynamic>{
32 33 34 35
        'id': id,
      });
      completer.complete(json.encode(result));
    }
36
    if (SchedulerBinding.instance.hasScheduledFrame) {
37
      SchedulerBinding.instance.addPostFrameCallback(completeSemantics);
38
    } else {
39
      completeSemantics();
40
    }
41
    return completer.future;
42
  }
43 44 45 46 47 48 49 50 51
  if (message.contains('setClipboard')) {
    final Completer<String> completer = Completer<String>();
    final String str = message.split('#')[1];
    Future<void> completeSetClipboard([Object _]) async {
      await kSemanticsChannel.invokeMethod<dynamic>('setClipboard', <String, dynamic>{
        'message': str,
      });
      completer.complete('');
    }
52
    if (SchedulerBinding.instance.hasScheduledFrame) {
53
      SchedulerBinding.instance.addPostFrameCallback(completeSetClipboard);
54
    } else {
55
      completeSetClipboard();
56
    }
57 58
    return completer.future;
  }
59
  throw UnimplementedError();
60 61
}

62
Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
63 64 65 66
  selectionControlsRoute : (BuildContext context) => const SelectionControlsPage(),
  popupControlsRoute : (BuildContext context) => const PopupControlsPage(),
  textFieldRoute : (BuildContext context) => const TextFieldPage(),
  headingsRoute: (BuildContext context) => const HeadingsPage(),
67
};
68 69

class TestApp extends StatelessWidget {
70
  const TestApp({Key key}) : super(key: key);
71 72 73

  @override
  Widget build(BuildContext context) {
74
    return MaterialApp(
75
      routes: routes,
76
      home: Builder(
77
        builder: (BuildContext context) {
78 79
          return Scaffold(
            body: ListView(
80
              children: routes.keys.map<Widget>((String value) {
81 82
                return MaterialButton(
                  child: Text(value),
83 84 85 86 87 88 89 90 91 92 93 94
                  onPressed: () {
                    Navigator.of(context).pushNamed(value);
                  },
                );
              }).toList(),
            ),
          );
        }
      ),
    );
  }
}