test_step.dart 1.92 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 10
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';

enum TestStatus { ok, pending, failed, complete }

11
typedef TestStep = Future<TestStepResult> Function();
12 13 14 15 16 17 18 19 20 21 22 23 24 25

const String nothing = '-';

class TestStepResult {
  const TestStepResult(this.name, this.description, this.status);

  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) {
26
          return snapshot.data!;
27
        } else {
28 29
          final Object? result = snapshot.error;
          return result! as TestStepResult;
30
        }
31
      case ConnectionState.active:
32 33 34 35 36 37 38 39
        throw 'Unsupported state ${snapshot.connectionState}';
    }
  }

  final String name;
  final String description;
  final TestStatus status;

40 41
  static const TextStyle normal = TextStyle(height: 1.0);
  static const TextStyle bold = TextStyle(fontWeight: FontWeight.bold, height: 1.0);
42 43 44 45 46 47
  static const TestStepResult complete = TestStepResult(
    'Test complete',
    nothing,
    TestStatus.complete,
  );

48
  Widget asWidget(BuildContext context) {
49
    return Column(
50 51
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
52
        Text('Step: $name', style: bold),
53 54
        Text(description, style: normal),
        const Text(' ', style: normal),
55
        Text(
56
          status.toString().substring('TestStatus.'.length),
57
          key: ValueKey<String>(
58 59 60 61 62 63 64
              status == TestStatus.pending ? 'nostatus' : 'status'),
          style: bold,
        ),
      ],
    );
  }
}