test_step.dart 5.24 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 9
// 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:typed_data';

import 'package:flutter/material.dart';

10 11
import 'pair.dart';

12 13
enum TestStatus { ok, pending, failed, complete }

14
typedef TestStep = Future<TestStepResult> Function();
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

const String nothing = '-';

/// Result of a test step checking a nested communication handshake
/// between the Flutter app and the platform:
///
/// - The Flutter app sends a message to the platform.
/// - The platform, on receipt, echos the message back to Flutter in a separate message.
/// - The Flutter app records the incoming message echo and replies.
/// - The platform, on receipt of reply, echos the reply back to Flutter in a separate message.
/// - The Flutter app records the incoming reply echo.
/// - The platform finally replies to the original message with another echo.
class TestStepResult {
  const TestStepResult(
    this.name,
    this.description,
    this.status, {
    this.messageSent = nothing,
    this.messageEcho = nothing,
    this.messageReceived = nothing,
    this.replyEcho = nothing,
    this.error = nothing,
  });

  factory TestStepResult.fromSnapshot(AsyncSnapshot<TestStepResult> snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        return const TestStepResult('Not started', nothing, TestStatus.ok);
      case ConnectionState.waiting:
        return const TestStepResult('Executing', nothing, TestStatus.pending);
      case ConnectionState.done:
        if (snapshot.hasData) {
47
          return snapshot.data!;
48
        }
49
        return snapshot.error! as TestStepResult;
50
      case ConnectionState.active:
51 52 53 54 55 56 57 58 59 60 61 62 63
        throw 'Unsupported state ${snapshot.connectionState}';
    }
  }

  final String name;
  final String description;
  final TestStatus status;
  final dynamic messageSent;
  final dynamic messageEcho;
  final dynamic messageReceived;
  final dynamic replyEcho;
  final dynamic error;

64 65 66 67 68 69 70
  static const TextStyle bold = TextStyle(fontWeight: FontWeight.bold);
  static const TestStepResult complete = TestStepResult(
    'Test complete',
    nothing,
    TestStatus.complete,
  );

71
  Widget asWidget(BuildContext context) {
72
    return ListView(
73
      children: <Widget>[
74 75
        Text('Step: $name', style: bold),
        Text(description),
76
        const Text(' '),
77 78 79 80 81
        Text('Msg sent: ${_toString(messageSent)}'),
        Text('Msg rvcd: ${_toString(messageReceived)}'),
        Text('Reply echo: ${_toString(replyEcho)}'),
        Text('Msg echo: ${_toString(messageEcho)}'),
        Text('Error: ${_toString(error)}'),
82
        const Text(' '),
83
        Text(
84
          status.name,
85
          key: ValueKey<String>(
86 87 88 89 90 91
              status == TestStatus.pending ? 'nostatus' : 'status'),
          style: bold,
        ),
      ],
    );
  }
92 93

  static bool deepEquals(dynamic a, dynamic b) => _deepEquals(a, b);
94 95 96 97 98

  @override
  String toString() {
    return 'TestStepResult($status)';
  }
99 100 101 102 103 104 105 106 107 108 109
}

Future<TestStepResult> resultOfHandshake(
  String name,
  String description,
  dynamic message,
  List<dynamic> received,
  dynamic messageEcho,
  dynamic error,
) async {
  assert(message != nothing);
110
  while (received.length < 2) {
111
    received.add(nothing);
112
  }
113 114 115 116 117 118 119 120 121 122 123
  TestStatus status;
  if (!_deepEquals(messageEcho, message) ||
      received.length != 2 ||
      !_deepEquals(received[0], message) ||
      !_deepEquals(received[1], message)) {
    status = TestStatus.failed;
  } else if (error != nothing) {
    status = TestStatus.failed;
  } else {
    status = TestStatus.ok;
  }
124
  return TestStepResult(
125 126 127 128 129 130 131 132 133 134 135 136
    name,
    description,
    status,
    messageSent: message,
    messageEcho: messageEcho,
    messageReceived: received[0],
    replyEcho: received[1],
    error: error,
  );
}

String _toString(dynamic message) {
137
  if (message is ByteData) {
138 139 140
    return message.buffer
        .asUint8List(message.offsetInBytes, message.lengthInBytes)
        .toString();
141
  } else {
142
    return '$message';
143
  }
144 145 146
}

bool _deepEquals(dynamic a, dynamic b) {
147
  if (a == b) {
148
    return true;
149 150
  }
  if (a is double && a.isNaN) {
151
    return b is double && b.isNaN;
152 153
  }
  if (a is ByteData) {
154
    return b is ByteData && _deepEqualsByteData(a, b);
155 156
  }
  if (a is List) {
157
    return b is List && _deepEqualsList(a, b);
158 159
  }
  if (a is Map) {
160
    return b is Map && _deepEqualsMap(a, b);
161 162
  }
  if (a is Pair) {
163
    return b is Pair && _deepEqualsPair(a, b);
164
  }
165 166 167 168 169 170 171 172 173 174 175
  return false;
}

bool _deepEqualsByteData(ByteData a, ByteData b) {
  return _deepEqualsList(
    a.buffer.asUint8List(a.offsetInBytes, a.lengthInBytes),
    b.buffer.asUint8List(b.offsetInBytes, b.lengthInBytes),
  );
}

bool _deepEqualsList(List<dynamic> a, List<dynamic> b) {
176
  if (a.length != b.length) {
177
    return false;
178
  }
179
  for (int i = 0; i < a.length; i++) {
180
    if (!_deepEquals(a[i], b[i])) {
181
      return false;
182
    }
183 184 185 186 187
  }
  return true;
}

bool _deepEqualsMap(Map<dynamic, dynamic> a, Map<dynamic, dynamic> b) {
188
  if (a.length != b.length) {
189
    return false;
190
  }
191
  for (final dynamic key in a.keys) {
192
    if (!b.containsKey(key) || !_deepEquals(a[key], b[key])) {
193
      return false;
194
    }
195 196 197
  }
  return true;
}
198 199 200 201

bool _deepEqualsPair(Pair a, Pair b) {
  return _deepEquals(a.left, b.left) && _deepEquals(a.right, b.right);
}