screenshot_command_test.dart 4.61 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:file/memory.dart';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/screenshot.dart';
import 'package:flutter_tools/src/vmservice.dart';

import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart';

void main() {
  setUpAll(() {
    Cache.disableLocking();
  });

  group('Validate screenshot options', () {
    testUsingContext('rasterizer and skia screenshots do not require a device', () async {
      // Throw a specific exception when attempting to make a VM Service connection to
      // verify that we've made it past the initial validation.
25
      openChannelForTesting = (String url, {CompressionOptions? compression, Logger? logger}) async {
26 27 28 29
        expect(url, 'ws://localhost:8181/ws');
        throw Exception('dummy');
      };

30
      await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
31
        .run(<String>['screenshot', '--type=skia', '--observatory-url=http://localhost:8181']),
32
        throwsA(isException.having((Exception exception) => exception.toString(), 'message', contains('dummy'))),
33 34
      );

35
      await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
36
        .run(<String>['screenshot', '--type=rasterizer', '--observatory-url=http://localhost:8181']),
37
        throwsA(isException.having((Exception exception) => exception.toString(), 'message', contains('dummy'))),
38 39 40 41 42
      );
    });


    testUsingContext('rasterizer and skia screenshots require observatory uri', () async {
43
      await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
44 45 46 47
        .run(<String>['screenshot', '--type=skia']),
        throwsToolExit(message: 'Observatory URI must be specified for screenshot type skia')
      );

48
      await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
49 50 51 52 53 54
        .run(<String>['screenshot', '--type=rasterizer',]),
        throwsToolExit(message: 'Observatory URI must be specified for screenshot type rasterizer'),
      );
    });

    testUsingContext('device screenshots require device', () async {
55
      await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
56 57 58 59 60 61
        .run(<String>['screenshot']),
        throwsToolExit(message: 'Must have a connected device for screenshot type device'),
      );
    });

    testUsingContext('device screenshots cannot provided Observatory', () async {
62
      await expectLater(() => createTestCommandRunner(ScreenshotCommand(fs: MemoryFileSystem.test()))
63
        .run(<String>['screenshot',  '--observatory-url=http://localhost:8181']),
64 65 66 67
        throwsToolExit(message: 'Observatory URI cannot be provided for screenshot type device'),
      );
    });
  });
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

  group('Screenshot file validation', () {
    testWithoutContext('successful in pwd', () async {
      final MemoryFileSystem fs = MemoryFileSystem.test();
      fs.file('test.png').createSync();
      fs.directory('sub_dir').createSync();
      fs.file('sub_dir/test.png').createSync();

      expect(() => ScreenshotCommand.checkOutput(fs.file('test.png'), fs),
          returnsNormally);
      expect(
          () => ScreenshotCommand.checkOutput(fs.file('sub_dir/test.png'), fs),
          returnsNormally);
    });

    testWithoutContext('failed in pwd', () async {
      final MemoryFileSystem fs = MemoryFileSystem.test();
      fs.directory('sub_dir').createSync();

      expect(
          () => ScreenshotCommand.checkOutput(fs.file('test.png'), fs),
          throwsToolExit(
              message: 'File was not created, ensure path is valid'));
      expect(
          () => ScreenshotCommand.checkOutput(fs.file('../'), fs),
          throwsToolExit(
              message: 'File was not created, ensure path is valid'));
      expect(
          () => ScreenshotCommand.checkOutput(fs.file('.'), fs),
          throwsToolExit(
              message: 'File was not created, ensure path is valid'));
      expect(
          () => ScreenshotCommand.checkOutput(fs.file('/'), fs),
          throwsToolExit(
              message: 'File was not created, ensure path is valid'));
      expect(
          () => ScreenshotCommand.checkOutput(fs.file('sub_dir/test.png'), fs),
          throwsToolExit(
              message: 'File was not created, ensure path is valid'));
    });
  });
109
}