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

5 6
// @dart = 2.8

7
import 'package:flutter/semantics.dart';
8 9 10 11 12
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('SemanticsEvent.toString', () {
    expect(
13
      TestSemanticsEvent().toString(),
14 15 16
      'TestSemanticsEvent()',
    );
    expect(
17
      TestSemanticsEvent(number: 10).toString(),
18 19 20
      'TestSemanticsEvent(number: 10)',
    );
    expect(
21
      TestSemanticsEvent(text: 'hello').toString(),
22 23 24
      'TestSemanticsEvent(text: hello)',
    );
    expect(
25
      TestSemanticsEvent(text: 'hello', number: 10).toString(),
26 27 28
      'TestSemanticsEvent(number: 10, text: hello)',
    );
  });
29 30
  test('SemanticsEvent.toMap', () {
    expect(
31
      TestSemanticsEvent(text: 'hi', number: 11).toMap(),
32
      <String, dynamic>{
33
        'type': 'TestEvent',
34
        'data': <String, dynamic>{
35
          'text': 'hi',
36 37 38
          'number': 11,
        },
      },
39 40
    );
    expect(
41
      TestSemanticsEvent(text: 'hi', number: 11).toMap(nodeId: 123),
42
      <String, dynamic>{
43 44
        'type': 'TestEvent',
        'nodeId': 123,
45
        'data': <String, dynamic>{
46
          'text': 'hi',
47 48 49
          'number': 11,
        },
      },
50 51
    );
  });
52 53 54 55 56 57 58 59 60
}

class TestSemanticsEvent extends SemanticsEvent {
  TestSemanticsEvent({ this.text, this.number }) : super('TestEvent');

  final String text;
  final int number;

  @override
61
  Map<String, dynamic> getDataMap() {
62 63 64 65 66 67 68 69
    final Map<String, dynamic> result = <String, dynamic>{};
    if (text != null)
      result['text'] = text;
    if (number != null)
      result['number'] = number;
    return result;
  }
}