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

import 'dart:async';
6
import 'dart:io' show Platform;
7 8 9 10 11 12
import 'dart:typed_data';

import 'package:flutter/material.dart';

import 'src/basic_messaging.dart';
import 'src/method_calls.dart';
13
import 'src/pair.dart';
14 15 16
import 'src/test_step.dart';

void main() {
17
  runApp(const TestApp());
18 19 20
}

class TestApp extends StatefulWidget {
21
  const TestApp({super.key});
22

23
  @override
24
  State<TestApp> createState() => _TestAppState();
25 26 27
}

class _TestAppState extends State<TestApp> {
28
  static final dynamic anUnknownValue = DateTime.fromMillisecondsSinceEpoch(1520777802314);
29 30 31 32 33 34
  static final List<dynamic> aList = <dynamic>[
    false,
    0,
    0.0,
    'hello',
    <dynamic>[
35
      <String, dynamic>{'key': 42},
36 37 38 39 40 41 42 43
    ],
  ];
  static final Map<String, dynamic> aMap = <String, dynamic>{
    'a': false,
    'b': 0,
    'c': 0.0,
    'd': 'hello',
    'e': <dynamic>[
44
      <String, dynamic>{'key': 42},
45
    ],
46
  };
47
  static final Uint8List someUint8s = Uint8List.fromList(<int>[
48 49 50 51 52
    0xBA,
    0x5E,
    0xBA,
    0x11,
  ]);
53
  static final Int32List someInt32s = Int32List.fromList(<int>[
54 55 56 57
    -0x7fffffff - 1,
    0,
    0x7fffffff,
  ]);
58
  static final Int64List someInt64s = Int64List.fromList(<int>[
59 60 61 62
    -0x7fffffffffffffff - 1,
    0,
    0x7fffffffffffffff,
  ]);
63 64 65 66 67 68 69 70 71 72 73
  static final Float32List someFloat32s = Float32List.fromList(<double>[
    double.nan,
    double.negativeInfinity,
    -double.maxFinite,
    -double.minPositive,
    -0.0,
    0.0,
    double.minPositive,
    double.maxFinite,
    double.infinity,
  ]);
74
  static final Float64List someFloat64s =
75
      Float64List.fromList(<double>[
76 77 78 79
    double.nan,
    double.negativeInfinity,
    -double.maxFinite,
    -double.minPositive,
80 81
    -0.0,
    0.0,
82 83 84
    double.minPositive,
    double.maxFinite,
    double.infinity,
85
  ]);
86 87
  static final dynamic aCompoundUnknownValue = <dynamic>[
    anUnknownValue,
88
    Pair(anUnknownValue, aList),
89
  ];
90 91 92 93 94 95 96 97 98 99 100 101 102 103
  static final List<TestStep> steps = <TestStep>[
    () => methodCallJsonSuccessHandshake(null),
    () => methodCallJsonSuccessHandshake(true),
    () => methodCallJsonSuccessHandshake(7),
    () => methodCallJsonSuccessHandshake('world'),
    () => methodCallJsonSuccessHandshake(aList),
    () => methodCallJsonSuccessHandshake(aMap),
    () => methodCallJsonNotImplementedHandshake(),
    () => methodCallStandardSuccessHandshake(null),
    () => methodCallStandardSuccessHandshake(true),
    () => methodCallStandardSuccessHandshake(7),
    () => methodCallStandardSuccessHandshake('world'),
    () => methodCallStandardSuccessHandshake(aList),
    () => methodCallStandardSuccessHandshake(aMap),
104 105
    () => methodCallStandardSuccessHandshake(anUnknownValue),
    () => methodCallStandardSuccessHandshake(aCompoundUnknownValue),
106 107 108 109 110 111
    () => methodCallJsonErrorHandshake(null),
    () => methodCallJsonErrorHandshake('world'),
    () => methodCallStandardErrorHandshake(null),
    () => methodCallStandardErrorHandshake('world'),
    () => methodCallStandardNotImplementedHandshake(),
    () => basicBinaryHandshake(null),
112 113 114 115
    if (!Platform.isMacOS)
      // Note, it was decided that this will function differently on macOS. See
      // also: https://github.com/flutter/flutter/issues/110865.
      () => basicBinaryHandshake(ByteData(0)),
116
    () => basicBinaryHandshake(ByteData(4)..setUint32(0, 0x12345678)),
117 118
    () => basicStringHandshake('hello, world'),
    () => basicStringHandshake('hello \u263A \u{1f602} unicode'),
119 120 121 122
    if (!Platform.isMacOS)
      // Note, it was decided that this will function differently on macOS. See
      // also: https://github.com/flutter/flutter/issues/110865.
      () => basicStringHandshake(''),
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
    () => basicStringHandshake(null),
    () => basicJsonHandshake(null),
    () => basicJsonHandshake(true),
    () => basicJsonHandshake(false),
    () => basicJsonHandshake(0),
    () => basicJsonHandshake(-7),
    () => basicJsonHandshake(7),
    () => basicJsonHandshake(1 << 32),
    () => basicJsonHandshake(1 << 56),
    () => basicJsonHandshake(0.0),
    () => basicJsonHandshake(-7.0),
    () => basicJsonHandshake(7.0),
    () => basicJsonHandshake(''),
    () => basicJsonHandshake('hello, world'),
    () => basicJsonHandshake('hello, "world"'),
    () => basicJsonHandshake('hello \u263A \u{1f602} unicode'),
    () => basicJsonHandshake(<dynamic>[]),
    () => basicJsonHandshake(aList),
    () => basicJsonHandshake(<String, dynamic>{}),
    () => basicJsonHandshake(aMap),
    () => basicStandardHandshake(null),
    () => basicStandardHandshake(true),
    () => basicStandardHandshake(false),
    () => basicStandardHandshake(0),
    () => basicStandardHandshake(-7),
    () => basicStandardHandshake(7),
    () => basicStandardHandshake(1 << 32),
    () => basicStandardHandshake(1 << 64),
    () => basicStandardHandshake(1 << 128),
    () => basicStandardHandshake(0.0),
    () => basicStandardHandshake(-7.0),
    () => basicStandardHandshake(7.0),
    () => basicStandardHandshake(''),
    () => basicStandardHandshake('hello, world'),
    () => basicStandardHandshake('hello \u263A \u{1f602} unicode'),
    () => basicStandardHandshake(someUint8s),
    () => basicStandardHandshake(someInt32s),
    () => basicStandardHandshake(someInt64s),
161
    () => basicStandardHandshake(someFloat32s),
162 163 164 165 166 167
    () => basicStandardHandshake(someFloat64s),
    () => basicStandardHandshake(<dynamic>[]),
    () => basicStandardHandshake(aList),
    () => basicStandardHandshake(<String, dynamic>{}),
    () => basicStandardHandshake(<dynamic, dynamic>{7: true, false: -7}),
    () => basicStandardHandshake(aMap),
168 169
    () => basicStandardHandshake(anUnknownValue),
    () => basicStandardHandshake(aCompoundUnknownValue),
170 171 172 173
    () => basicBinaryMessageToUnknownChannel(),
    () => basicStringMessageToUnknownChannel(),
    () => basicJsonMessageToUnknownChannel(),
    () => basicStandardMessageToUnknownChannel(),
174
    if (Platform.isIOS || Platform.isAndroid || Platform.isMacOS)
175
      () => basicBackgroundStandardEcho(123),
176
  ];
177
  Future<TestStepResult>? _result;
178 179 180 181
  int _step = 0;

  void _executeNextStep() {
    setState(() {
182
      if (_step < steps.length) {
183
        _result = steps[_step++]();
184
      } else {
185
        _result = Future<TestStepResult>.value(TestStepResult.complete);
186
      }
187 188 189 190 191 192 193
    });
  }

  Widget _buildTestResultWidget(
    BuildContext context,
    AsyncSnapshot<TestStepResult> snapshot,
  ) {
194
    return TestStepResult.fromSnapshot(snapshot).asWidget(context);
195 196 197 198
  }

  @override
  Widget build(BuildContext context) {
199
    return MaterialApp(
200
      title: 'Channels Test',
201 202
      home: Scaffold(
        appBar: AppBar(
203 204
          title: const Text('Channels Test'),
        ),
205
        body: Padding(
206
          padding: const EdgeInsets.all(20.0),
207
          child: FutureBuilder<TestStepResult>(
208 209 210 211
            future: _result,
            builder: _buildTestResultWidget,
          ),
        ),
212
        floatingActionButton: FloatingActionButton(
213 214
          key: const ValueKey<String>('step'),
          onPressed: _executeNextStep,
215
          child: const Icon(Icons.navigate_next),
216 217 218 219 220
        ),
      ),
    );
  }
}