"Fossies" - the Fresh Open Source Software Archive

Member "flutter-3.7.0/packages/flutter_tools/test/general.shard/android/adb_log_reader_test.dart" (24 Jan 2023, 6281 Bytes) of package /linux/misc/flutter-3.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:async';
    6 
    7 import 'package:flutter_tools/src/android/android_device.dart';
    8 import 'package:test/fake.dart';
    9 
   10 import '../../src/common.dart';
   11 import '../../src/fake_process_manager.dart';
   12 
   13 const int kLollipopVersionCode = 21;
   14 const String kLastLogcatTimestamp = '11-27 15:39:04.506';
   15 
   16 /// By default the android log reader accepts lines that match no patterns
   17 /// if the previous line was a match. Include an intentionally non-matching
   18 /// line as the first input to disable this behavior.
   19 const String kDummyLine = 'Contents are not important\n';
   20 
   21 void main() {
   22   testWithoutContext('AdbLogReader calls adb logcat with expected flags apiVersion 21', () async {
   23     final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
   24       const FakeCommand(
   25         command: <String>[
   26           'adb',
   27           '-s',
   28           '1234',
   29           'shell',
   30           '-x',
   31           'logcat',
   32           '-v',
   33           'time',
   34           '-T',
   35           "'$kLastLogcatTimestamp'",
   36         ],
   37       ),
   38     ]);
   39     await AdbLogReader.createLogReader(
   40       createFakeDevice(kLollipopVersionCode),
   41       processManager,
   42     );
   43 
   44     expect(processManager, hasNoRemainingExpectations);
   45   });
   46 
   47   testWithoutContext('AdbLogReader calls adb logcat with expected flags apiVersion < 21', () async {
   48     final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
   49       const FakeCommand(
   50         command: <String>[
   51           'adb',
   52           '-s',
   53           '1234',
   54           'shell',
   55           '-x',
   56           'logcat',
   57           '-v',
   58           'time',
   59         ],
   60       ),
   61     ]);
   62     await AdbLogReader.createLogReader(
   63       createFakeDevice(kLollipopVersionCode - 1),
   64       processManager,
   65     );
   66 
   67     expect(processManager, hasNoRemainingExpectations);
   68   });
   69 
   70   testWithoutContext('AdbLogReader calls adb logcat with expected flags null apiVersion', () async {
   71     final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
   72       const FakeCommand(
   73         command: <String>[
   74           'adb',
   75           '-s',
   76           '1234',
   77           'shell',
   78           '-x',
   79           'logcat',
   80           '-v',
   81           'time',
   82         ],
   83       ),
   84     ]);
   85     await AdbLogReader.createLogReader(
   86       createFakeDevice(null),
   87       processManager,
   88     );
   89 
   90     expect(processManager, hasNoRemainingExpectations);
   91   });
   92 
   93   testWithoutContext('AdbLogReader calls adb logcat with expected flags when requesting past logs', () async {
   94     final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
   95       const FakeCommand(
   96         command: <String>[
   97           'adb',
   98           '-s',
   99           '1234',
  100           'shell',
  101           '-x',
  102           'logcat',
  103           '-v',
  104           'time',
  105           '-s',
  106           'flutter',
  107         ],
  108       ),
  109     ]);
  110     await AdbLogReader.createLogReader(
  111       createFakeDevice(null),
  112       processManager,
  113       includePastLogs: true,
  114     );
  115 
  116     expect(processManager, hasNoRemainingExpectations);
  117   });
  118 
  119   testWithoutContext('AdbLogReader handles process early exit', () async {
  120     final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
  121       FakeCommand(
  122         command: const <String>[
  123           'adb',
  124           '-s',
  125           '1234',
  126           'shell',
  127           '-x',
  128           'logcat',
  129           '-v',
  130           'time',
  131         ],
  132         completer: Completer<void>.sync(),
  133         stdout: 'Hello There\n',
  134       ),
  135     ]);
  136     final AdbLogReader logReader = await AdbLogReader.createLogReader(
  137       createFakeDevice(null),
  138       processManager,
  139     );
  140     final Completer<void> onDone = Completer<void>.sync();
  141     logReader.logLines.listen((String _) { }, onDone: onDone.complete);
  142 
  143     logReader.dispose();
  144     await onDone.future;
  145   });
  146 
  147   testWithoutContext('AdbLogReader does not filter output from AndroidRuntime crashes', () async {
  148     final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
  149       FakeCommand(
  150         command: const <String>[
  151           'adb',
  152           '-s',
  153           '1234',
  154           'shell',
  155           '-x',
  156           'logcat',
  157           '-v',
  158           'time',
  159         ],
  160         completer: Completer<void>.sync(),
  161         // Example stack trace from an incorrectly named application:name in the AndroidManifest.xml
  162         stdout:
  163           '$kDummyLine'
  164           '05-11 12:54:46.665 E/AndroidRuntime(11787): FATAL EXCEPTION: main\n'
  165           '05-11 12:54:46.665 E/AndroidRuntime(11787): Process: com.example.foobar, PID: 11787\n'
  166           '05-11 12:54:46.665 java.lang.RuntimeException: Unable to instantiate application '
  167           'io.flutter.app.FlutterApplication2: java.lang.ClassNotFoundException:\n',
  168       ),
  169     ]);
  170     final AdbLogReader logReader = await AdbLogReader.createLogReader(
  171       createFakeDevice(null),
  172       processManager,
  173     );
  174     await expectLater(logReader.logLines, emitsInOrder(<String>[
  175       'E/AndroidRuntime(11787): FATAL EXCEPTION: main',
  176       'E/AndroidRuntime(11787): Process: com.example.foobar, PID: 11787',
  177       'java.lang.RuntimeException: Unable to instantiate application io.flutter.app.FlutterApplication2: java.lang.ClassNotFoundException:',
  178     ]));
  179 
  180     logReader.dispose();
  181   });
  182 }
  183 
  184 AndroidDevice createFakeDevice(int? sdkLevel) {
  185   return FakeAndroidDevice(
  186     sdkLevel.toString(),
  187     kLastLogcatTimestamp,
  188   );
  189 }
  190 
  191 // Unfortunately Device, despite not being immutable, has an `operator ==`.
  192 // Until we fix that, we have to also ignore related lints here.
  193 // ignore: avoid_implementing_value_types
  194 class FakeAndroidDevice extends Fake implements AndroidDevice {
  195   FakeAndroidDevice(this._apiVersion, this._lastLogcatTimestamp);
  196 
  197   final String _lastLogcatTimestamp;
  198   final String _apiVersion;
  199 
  200   @override
  201   String get name => 'test-device';
  202 
  203   @override
  204   Future<String> get apiVersion => Future<String>.value(_apiVersion);
  205 
  206   @override
  207   Future<String> lastLogcatTimestamp() async => _lastLogcatTimestamp;
  208 
  209   @override
  210   List<String> adbCommandForDevice(List<String> command) {
  211     return <String>[
  212       'adb', '-s', '1234', ...command,
  213     ];
  214   }
  215 }