material_states_controller_test.dart 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('MaterialStatesController constructor', () {
    expect(MaterialStatesController().value, <MaterialState>{});
    expect(MaterialStatesController(<MaterialState>{}).value, <MaterialState>{});
    expect(MaterialStatesController(<MaterialState>{MaterialState.selected}).value, <MaterialState>{MaterialState.selected});
  });

  test('MaterialStatesController update, listener', () {
    int count = 0;
    void valueChanged() {
      count += 1;
    }
    final MaterialStatesController controller = MaterialStatesController();
    controller.addListener(valueChanged);

    controller.update(MaterialState.selected, true);
    expect(controller.value, <MaterialState>{MaterialState.selected});
    expect(count, 1);
    controller.update(MaterialState.selected, true);
    expect(controller.value, <MaterialState>{MaterialState.selected});
    expect(count, 1);

    controller.update(MaterialState.hovered, false);
    expect(count, 1);
    expect(controller.value, <MaterialState>{MaterialState.selected});
    controller.update(MaterialState.selected, false);
    expect(count, 2);
    expect(controller.value, <MaterialState>{});

    controller.update(MaterialState.hovered, true);
    expect(controller.value, <MaterialState>{MaterialState.hovered});
    expect(count, 3);
    controller.update(MaterialState.hovered, true);
    expect(controller.value, <MaterialState>{MaterialState.hovered});
    expect(count, 3);
    controller.update(MaterialState.pressed, true);
    expect(controller.value, <MaterialState>{MaterialState.hovered, MaterialState.pressed});
    expect(count, 4);
    controller.update(MaterialState.selected, true);
    expect(controller.value, <MaterialState>{MaterialState.hovered, MaterialState.pressed, MaterialState.selected});
    expect(count, 5);
    controller.update(MaterialState.selected, false);
    expect(controller.value, <MaterialState>{MaterialState.hovered, MaterialState.pressed});
    expect(count, 6);
    controller.update(MaterialState.selected, false);
    expect(controller.value, <MaterialState>{MaterialState.hovered, MaterialState.pressed});
    expect(count, 6);
    controller.update(MaterialState.pressed, false);
    expect(controller.value, <MaterialState>{MaterialState.hovered});
    expect(count, 7);
    controller.update(MaterialState.hovered, false);
    expect(controller.value, <MaterialState>{});
    expect(count, 8);

    controller.removeListener(valueChanged);
    controller.update(MaterialState.selected, true);
    expect(controller.value, <MaterialState>{MaterialState.selected});
    expect(count, 8);
  });


  test('MaterialStatesController const initial value', () {
    int count = 0;
    void valueChanged() {
      count += 1;
    }
    final MaterialStatesController controller = MaterialStatesController(const <MaterialState>{MaterialState.selected});
    controller.addListener(valueChanged);

    controller.update(MaterialState.selected, true);
    expect(controller.value, <MaterialState>{MaterialState.selected});
    expect(count, 0);

    controller.update(MaterialState.selected, false);
    expect(controller.value, <MaterialState>{});
    expect(count, 1);
  });
}