timeline_test.dart 3.63 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_driver/src/driver/timeline.dart';
8

9
import '../../common.dart';
10

11 12 13
void main() {
  group('Timeline', () {
    test('parses JSON', () {
14
      final Timeline timeline = Timeline.fromJson(<String, dynamic>{
pq's avatar
pq committed
15 16
        'traceEvents': <Map<String, dynamic>>[
          <String, dynamic>{
17 18 19 20 21 22
            'name': 'test event',
            'cat': 'test category',
            'ph': 'B',
            'pid': 123,
            'tid': 234,
            'dur': 345,
23
            'tdur': 245,
24 25
            'ts': 456,
            'tts': 567,
pq's avatar
pq committed
26
            'args': <String, dynamic>{
27
              'arg1': true,
28
            },
29 30
          },
          // Tests that we don't choke on missing data
31 32
          <String, dynamic>{},
        ],
33 34 35 36
      });

      expect(timeline.events, hasLength(2));

37
      final TimelineEvent e1 = timeline.events[1];
38 39 40 41 42 43
      expect(e1.name, 'test event');
      expect(e1.category, 'test category');
      expect(e1.phase, 'B');
      expect(e1.processId, 123);
      expect(e1.threadId, 234);
      expect(e1.duration, const Duration(microseconds: 345));
44
      expect(e1.threadDuration, const Duration(microseconds: 245));
45 46
      expect(e1.timestampMicros, 456);
      expect(e1.threadTimestampMicros, 567);
47
      expect(e1.arguments, <String, dynamic>{'arg1': true});
48

49
      final TimelineEvent e2 = timeline.events[0];
50 51 52 53 54 55
      expect(e2.name, isNull);
      expect(e2.category, isNull);
      expect(e2.phase, isNull);
      expect(e2.processId, isNull);
      expect(e2.threadId, isNull);
      expect(e2.duration, isNull);
56
      expect(e2.threadDuration, isNull);
57 58 59 60
      expect(e2.timestampMicros, isNull);
      expect(e2.threadTimestampMicros, isNull);
      expect(e2.arguments, isNull);
    });
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    test('sorts JSON', () {
      final Timeline timeline = Timeline.fromJson(<String, dynamic>{
        'traceEvents': <Map<String, dynamic>>[
          <String, dynamic>{
            'name': 'test event 1',
            'ts': 457,
          },
          <String, dynamic>{
            'name': 'test event 2',
            'ts': 456,
          },
        ],
      });

      expect(timeline.events, hasLength(2));
      expect(timeline.events[0].timestampMicros, equals(456));
      expect(timeline.events[1].timestampMicros, equals(457));
      expect(timeline.events[0].name, equals('test event 2'));
      expect(timeline.events[1].name, equals('test event 1'));
    });

    test('sorts JSON nulls first', () {
      final Timeline timeline = Timeline.fromJson(<String, dynamic>{
        'traceEvents': <Map<String, dynamic>>[
          <String, dynamic>{
            'name': 'test event 0',
            'ts': null,
          },
          <String, dynamic>{
            'name': 'test event 1',
            'ts': 457,
          },
          <String, dynamic>{
            'name': 'test event 2',
            'ts': 456,
          },
          <String, dynamic>{
            'name': 'test event 3',
            'ts': null,
          },
        ],
      });

      expect(timeline.events, hasLength(4));
      expect(timeline.events[0].timestampMicros, isNull);
      expect(timeline.events[1].timestampMicros, isNull);
      expect(timeline.events[2].timestampMicros, equals(456));
      expect(timeline.events[3].timestampMicros, equals(457));
      expect(timeline.events[0].name, equals('test event 0'));
      expect(timeline.events[1].name, equals('test event 3'));
      expect(timeline.events[2].name, equals('test event 2'));
      expect(timeline.events[3].name, equals('test event 1'));
    });
115 116
  });
}