precache_test.dart 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Copyright 2019 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 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/precache.dart';
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';

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

void main() {
  group('precache', () {
    final MockCache cache = MockCache();
    Set<DevelopmentArtifact> artifacts;

    when(cache.isUpToDate()).thenReturn(false);
    when(cache.updateAll(any)).thenAnswer((Invocation invocation) {
      artifacts = invocation.positionalArguments.first;
      return Future<void>.value(null);
    });

    testUsingContext('Adds artifact flags to requested artifacts', () async {
      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(
30
        const <String>['precache', '--ios', '--android', '--web', '--macos', '--linux', '--windows', '--fuchsia']
31 32 33 34 35 36 37 38 39
      );
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
        DevelopmentArtifact.universal,
        DevelopmentArtifact.iOS,
        DevelopmentArtifact.android,
        DevelopmentArtifact.web,
        DevelopmentArtifact.macOS,
        DevelopmentArtifact.linux,
        DevelopmentArtifact.windows,
40
        DevelopmentArtifact.fuchsia,
41 42 43 44 45 46 47 48 49 50 51 52 53 54
      }));
    }, overrides: <Type, Generator>{
      Cache: () => cache,
    });

    final MockFlutterVersion flutterVersion = MockFlutterVersion();
    when(flutterVersion.isStable).thenReturn(true);

    testUsingContext('Adds artifact flags to requested artifacts on stable', () async {
      // Release lock between test cases.
      Cache.releaseLockEarly();
      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(
55
       const <String>['precache', '--ios', '--android', '--web', '--macos', '--linux', '--windows', '--fuchsia']
56 57 58 59 60 61 62 63 64 65
      );
     expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
       DevelopmentArtifact.universal,
       DevelopmentArtifact.iOS,
       DevelopmentArtifact.android,
     }));
    }, overrides: <Type, Generator>{
      Cache: () => cache,
      FlutterVersion: () => flutterVersion,
    });
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    testUsingContext('Downloads artifacts when --force is provided', () async {
      when(cache.isUpToDate()).thenReturn(true);
      // Release lock between test cases.
      Cache.releaseLockEarly();
      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(const <String>['precache', '--force']);
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
       DevelopmentArtifact.universal,
       DevelopmentArtifact.iOS,
       DevelopmentArtifact.android,
     }));
    }, overrides: <Type, Generator>{
      Cache: () => cache,
      FlutterVersion: () => flutterVersion,
    });
83 84 85 86 87
  });
}

class MockFlutterVersion extends Mock implements FlutterVersion {}
class MockCache extends Mock implements Cache {}