common.dart 2.79 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7
// 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 'dart:developer' as developer;
import 'dart:isolate' as isolate;

8
import 'package:flutter/foundation.dart';
9
import 'package:flutter/scheduler.dart';
10
import 'package:flutter/widgets.dart';
Ian Hickson's avatar
Ian Hickson committed
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
import 'package:flutter_test/flutter_test.dart';
import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';

export 'package:vm_service/vm_service.dart' show TimelineEvent;

late String isolateId;

late VmService _vmService;

void initTimelineTests() {
  setUpAll(() async {
    final developer.ServiceProtocolInfo info = await developer.Service.getInfo();
    if (info.serverUri == null) {
      fail('This test _must_ be run with --enable-vmservice.');
    }
    _vmService = await vmServiceConnectUri('ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws');
    await _vmService.setVMTimelineFlags(<String>['Dart']);
    isolateId = developer.Service.getIsolateID(isolate.Isolate.current)!;
  });
}

Future<List<TimelineEvent>> fetchTimelineEvents() async {
  final Timeline timeline = await _vmService.getVMTimeline();
  await _vmService.clearVMTimeline();
  return timeline.traceEvents!;
}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

Future<List<TimelineEvent>> fetchInterestingEvents(Set<String> interestingLabels) async {
  return (await fetchTimelineEvents()).where((TimelineEvent event) {
    return interestingLabels.contains(event.json!['name'])
        && event.json!['ph'] == 'B'; // "Begin" mark of events, vs E which is for the "End" mark of events.
  }).toList();
}

String eventToName(TimelineEvent event) => event.json!['name'] as String;

Future<List<String>> fetchInterestingEventNames(Set<String> interestingLabels) async {
  return (await fetchInterestingEvents(interestingLabels)).map<String>(eventToName).toList();
}

Future<void> runFrame(VoidCallback callback) {
  final Future<void> result = SchedulerBinding.instance.endOfFrame; // schedules a frame
  callback();
  return result;
}
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

// This binding skips the zones tests. These tests were written before we
// verified zones properly, and they have been legacied-in to avoid having
// to refactor them.
//
// When creating new tests, avoid relying on this class.
class ZoneIgnoringTestBinding extends WidgetsFlutterBinding {
  @override
  void initInstances() {
    super.initInstances();
    _instance = this;
  }

  @override
  bool debugCheckZone(String entryPoint) { return true; }

  static ZoneIgnoringTestBinding get instance => BindingBase.checkInstance(_instance);
  static ZoneIgnoringTestBinding? _instance;

  static ZoneIgnoringTestBinding ensureInitialized() {
    if (ZoneIgnoringTestBinding._instance == null) {
      ZoneIgnoringTestBinding();
    }
    return ZoneIgnoringTestBinding.instance;
  }
}