test_step.dart 1.82 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 26 27

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) {
          return snapshot.data;
        } else {
28
          final TestStepResult result = snapshot.error as TestStepResult;
29 30 31 32 33 34 35 36 37 38 39 40
          return result;
        }
        break;
      default:
        throw 'Unsupported state ${snapshot.connectionState}';
    }
  }

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

41 42 43 44 45 46 47
  static const TextStyle bold = TextStyle(fontWeight: FontWeight.bold);
  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 53
        Text('Step: $name', style: bold),
        Text(description),
54
        const Text(' '),
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,
        ),
      ],
    );
  }
}