protocol_discovery_test.dart 6.1 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium 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:async';

7
import 'package:flutter_tools/src/device.dart';
8 9
import 'package:flutter_tools/src/protocol_discovery.dart';
import 'package:test/test.dart';
10

11
import 'src/context.dart';
12 13
import 'src/mocks.dart';

14
void main() {
15
  group('service_protocol discovery', () {
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 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
    MockDeviceLogReader logReader;
    ProtocolDiscovery discoverer;

    group('no port forwarding', () {
      /// Performs test set-up functionality that must be performed as part of
      /// the `test()` pass and not part of the `setUp()` pass.
      ///
      /// This exists to make sure we're not creating an error that tries to
      /// cross an error-zone boundary. Our use of `testUsingContext()` runs the
      /// test code inside an error zone, but the `setUp()` code is not run in
      /// any zone. This creates the potential for errors that try to cross
      /// error-zone boundaries, which are considered uncaught.
      ///
      /// This also exists for cases where our initialization requires access to
      /// a `Context` object, which is only set up inside the zone.
      ///
      /// Note that these issues do not pertain to real code and are a test-only
      /// concern, since in real code, the zone is set-up in `main()`.
      ///
      /// See also: [runZoned]
      void initialize() {
        logReader = new MockDeviceLogReader();
        discoverer = new ProtocolDiscovery.observatory(logReader);
      }

      tearDown(() {
        discoverer.cancel();
        logReader.dispose();
      });

      testUsingContext('returns non-null uri future', () async {
        initialize();
        expect(discoverer.uri, isNotNull);
      });

      testUsingContext('discovers uri if logs already produced output', () async {
        initialize();
        logReader.addLine('HELLO WORLD');
        logReader.addLine('Observatory listening on http://127.0.0.1:9999');
        final Uri uri = await discoverer.uri;
        expect(uri.port, 9999);
        expect('$uri', 'http://127.0.0.1:9999');
      });

      testUsingContext('discovers uri if logs not yet produced output', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('Observatory listening on http://127.0.0.1:3333');
        final Uri uri = await uriFuture;
        expect(uri.port, 3333);
        expect('$uri', 'http://127.0.0.1:3333');
      });

      testUsingContext('uri throws if logs produce bad line', () async {
        initialize();
        Timer.run(() {
          logReader.addLine('Observatory listening on http://127.0.0.1:apple');
        });
        expect(discoverer.uri, throwsA(isFormatException));
      });

      testUsingContext('uri waits for correct log line', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('Observatory not listening...');
        final Uri timeoutUri = Uri.parse('http://timeout');
        final Uri actualUri = await uriFuture.timeout(
            const Duration(milliseconds: 100), onTimeout: () => timeoutUri);
        expect(actualUri, timeoutUri);
      });

      testUsingContext('discovers uri if log line contains Android prefix', () async {
        initialize();
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:52584');
        final Uri uri = await discoverer.uri;
        expect(uri.port, 52584);
        expect('$uri', 'http://127.0.0.1:52584');
      });

      testUsingContext('discovers uri if log line contains auth key', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
        final Uri uri = await uriFuture;
        expect(uri.port, 54804);
        expect('$uri', 'http://127.0.0.1:54804/PTwjm8Ii8qg=/');
      });

      testUsingContext('discovers uri if log line contains non-localhost', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
        final Uri uri = await uriFuture;
        expect(uri.port, 54804);
        expect('$uri', 'http://somehost:54804/PTwjm8Ii8qg=/');
      });
112
    });
113 114

    testUsingContext('port forwarding - default port', () async {
115
      final MockDeviceLogReader logReader = new MockDeviceLogReader();
116
      final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
117 118
          logReader,
          portForwarder: new MockPortForwarder(99),
119
          hostPort: 54777);
120 121

      // Get next port future.
122
      final Future<Uri> nextUri = discoverer.uri;
123
      logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
124
      final Uri uri = await nextUri;
125 126 127 128 129 130 131 132
      expect(uri.port, 54777);
      expect('$uri', 'http://somehost:54777/PTwjm8Ii8qg=/');

      discoverer.cancel();
      logReader.dispose();
    });

    testUsingContext('port forwarding - specified port', () async {
133
      final MockDeviceLogReader logReader = new MockDeviceLogReader();
134
      final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
135 136
          logReader,
          portForwarder: new MockPortForwarder(99),
137
          hostPort: 1243);
138 139

      // Get next port future.
140
      final Future<Uri> nextUri = discoverer.uri;
141
      logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
142
      final Uri uri = await nextUri;
143 144 145 146 147 148
      expect(uri.port, 1243);
      expect('$uri', 'http://somehost:1243/PTwjm8Ii8qg=/');

      discoverer.cancel();
      logReader.dispose();
    });
149 150
  });
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

class MockPortForwarder extends DevicePortForwarder {
  final int availablePort;
  MockPortForwarder([this.availablePort]);

  @override
  Future<int> forward(int devicePort, {int hostPort}) async => hostPort ?? availablePort;

  @override
  List<ForwardedPort> get forwardedPorts => throw 'not implemented';

  @override
  Future<Null> unforward(ForwardedPort forwardedPort) {
    throw 'not implemented';
  }
}