flutter_manifest_test.dart 17 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5 6
import 'dart:async';

7 8 9
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:test/test.dart';

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

void main() {
  setUpAll(() {
    Cache.flutterRoot = getFlutterRoot();
  });

  group('FlutterManifest', () {
23 24 25 26 27 28 29 30 31 32
    testUsingContext('is empty when the pubspec.yaml file is empty', () async {
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString('');
      expect(flutterManifest.isEmpty, true);
      expect(flutterManifest.appName, '');
      expect(flutterManifest.usesMaterialDesign, false);
      expect(flutterManifest.fontsDescriptor, isEmpty);
      expect(flutterManifest.fonts, isEmpty);
      expect(flutterManifest.assets, isEmpty);
    });

33
    test('has no fonts or assets when the "flutter" section is empty', () async {
34
      const String manifest = '''
35 36 37 38 39 40 41
name: test
dependencies:
  flutter:
    sdk: flutter
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
      expect(flutterManifest, isNotNull);
42
      expect(flutterManifest.isEmpty, false);
43 44 45 46 47 48 49 50
      expect(flutterManifest.appName, 'test');
      expect(flutterManifest.usesMaterialDesign, false);
      expect(flutterManifest.fontsDescriptor, isEmpty);
      expect(flutterManifest.fonts, isEmpty);
      expect(flutterManifest.assets, isEmpty);
    });

    test('knows if material design is used', () async {
51
      const String manifest = '''
52 53 54 55 56 57 58 59 60 61 62 63
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
      expect(flutterManifest.usesMaterialDesign, true);
    });

    test('has two assets', () async {
64
      const String manifest = '''
65 66 67 68 69 70 71 72 73 74 75
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  assets:
    - a/foo
    - a/bar
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
76 77 78
      expect(flutterManifest.assets.length, 2);
      expect(flutterManifest.assets[0], Uri.parse('a/foo'));
      expect(flutterManifest.assets[1], Uri.parse('a/bar'));
79 80 81
    });

    test('has one font family with one asset', () async {
82
      const String manifest = '''
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - asset: a/bar
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);

      expect(flutterManifest.fontsDescriptor.toString(), '[{fonts: [{asset: a/bar}], family: foo}]');
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 1);
      final Font font = fonts[0];
100
      const String fontDescriptor = '{family: foo, fonts: [{asset: a/bar}]}';
101 102 103 104 105
      expect(font.descriptor.toString(), fontDescriptor);
      expect(font.familyName, 'foo');
      final List<FontAsset> assets = font.fontAssets;
      expect(assets.length, 1);
      final FontAsset fontAsset = assets[0];
106
      expect(fontAsset.assetUri.path, 'a/bar');
107 108 109 110 111
      expect(fontAsset.weight, isNull);
      expect(fontAsset.style, isNull);
    });

    test('has one font family with a simple asset and one with weight', () async {
112
      const String manifest = '''
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - asset: a/bar
        - asset: a/bar
          weight: 400
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);

128
      const String expectedFontsDescriptor = '[{fonts: [{asset: a/bar}, {weight: 400, asset: a/bar}], family: foo}]';
129 130 131 132
      expect(flutterManifest.fontsDescriptor.toString(), expectedFontsDescriptor);
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 1);
      final Font font = fonts[0];
133
      const String fontDescriptor = '{family: foo, fonts: [{asset: a/bar}, {weight: 400, asset: a/bar}]}';
134 135 136 137 138
      expect(font.descriptor.toString(), fontDescriptor);
      expect(font.familyName, 'foo');
      final List<FontAsset> assets = font.fontAssets;
      expect(assets.length, 2);
      final FontAsset fontAsset0 = assets[0];
139
      expect(fontAsset0.assetUri.path, 'a/bar');
140 141 142
      expect(fontAsset0.weight, isNull);
      expect(fontAsset0.style, isNull);
      final FontAsset fontAsset1 = assets[1];
143
      expect(fontAsset1.assetUri.path, 'a/bar');
144 145 146 147 148
      expect(fontAsset1.weight, 400);
      expect(fontAsset1.style, isNull);
    });

    test('has one font family with a simple asset and one with weight and style', () async {
149
      const String manifest = '''
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - asset: a/bar
        - asset: a/bar
          weight: 400
          style: italic
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);

166
      const String expectedFontsDescriptor = '[{fonts: [{asset: a/bar}, {style: italic, weight: 400, asset: a/bar}], family: foo}]';
167 168 169 170
      expect(flutterManifest.fontsDescriptor.toString(), expectedFontsDescriptor);
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 1);
      final Font font = fonts[0];
171
      const String fontDescriptor = '{family: foo, fonts: [{asset: a/bar}, {weight: 400, style: italic, asset: a/bar}]}';
172 173 174 175 176
      expect(font.descriptor.toString(), fontDescriptor);
      expect(font.familyName, 'foo');
      final List<FontAsset> assets = font.fontAssets;
      expect(assets.length, 2);
      final FontAsset fontAsset0 = assets[0];
177
      expect(fontAsset0.assetUri.path, 'a/bar');
178 179 180
      expect(fontAsset0.weight, isNull);
      expect(fontAsset0.style, isNull);
      final FontAsset fontAsset1 = assets[1];
181
      expect(fontAsset1.assetUri.path, 'a/bar');
182 183 184 185 186
      expect(fontAsset1.weight, 400);
      expect(fontAsset1.style, 'italic');
    });

    test('has two font families, each with one simple asset and one with weight and style', () async {
187
      const String manifest = '''
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - asset: a/bar
        - asset: a/bar
          weight: 400
          style: italic
    - family: bar
      fonts:
        - asset: a/baz
        - weight: 400
          asset: a/baz
          style: italic
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);

210
      const String expectedFontsDescriptor = '[{fonts: [{asset: a/bar}, {style: italic, weight: 400, asset: a/bar}], family: foo},'
211 212 213 214 215 216
                                             ' {fonts: [{asset: a/baz}, {style: italic, weight: 400, asset: a/baz}], family: bar}]';
      expect(flutterManifest.fontsDescriptor.toString(), expectedFontsDescriptor);
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 2);

      final Font fooFont = fonts[0];
217
      const String barFontDescriptor = '{family: foo, fonts: [{asset: a/bar}, {weight: 400, style: italic, asset: a/bar}]}';
218 219 220 221 222
      expect(fooFont.descriptor.toString(), barFontDescriptor);
      expect(fooFont.familyName, 'foo');
      final List<FontAsset> fooAassets = fooFont.fontAssets;
      expect(fooAassets.length, 2);
      final FontAsset fooFontAsset0 = fooAassets[0];
223
      expect(fooFontAsset0.assetUri.path, 'a/bar');
224 225 226
      expect(fooFontAsset0.weight, isNull);
      expect(fooFontAsset0.style, isNull);
      final FontAsset fooFontAsset1 = fooAassets[1];
227
      expect(fooFontAsset1.assetUri.path, 'a/bar');
228 229 230 231
      expect(fooFontAsset1.weight, 400);
      expect(fooFontAsset1.style, 'italic');

      final Font barFont = fonts[1];
232
      const String fontDescriptor = '{family: bar, fonts: [{asset: a/baz}, {weight: 400, style: italic, asset: a/baz}]}';
233 234 235 236 237
      expect(barFont.descriptor.toString(), fontDescriptor);
      expect(barFont.familyName, 'bar');
      final List<FontAsset> barAssets = barFont.fontAssets;
      expect(barAssets.length, 2);
      final FontAsset barFontAsset0 = barAssets[0];
238
      expect(barFontAsset0.assetUri.path, 'a/baz');
239 240 241
      expect(barFontAsset0.weight, isNull);
      expect(barFontAsset0.style, isNull);
      final FontAsset barFontAsset1 = barAssets[1];
242
      expect(barFontAsset1.assetUri.path, 'a/baz');
243 244 245 246
      expect(barFontAsset1.weight, 400);
      expect(barFontAsset1.style, 'italic');
    });

Josh Soref's avatar
Josh Soref committed
247
    testUsingContext('has only one of two font families when one declaration is missing the "family" option', () async {
248
      const String manifest = '''
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - asset: a/bar
        - asset: a/bar
          weight: 400
          style: italic
    - fonts:
        - asset: a/baz
        - asset: a/baz
          weight: 400
          style: italic
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);


271
      const String expectedFontsDescriptor = '[{fonts: [{asset: a/bar}, {style: italic, weight: 400, asset: a/bar}], family: foo},'
272 273 274 275 276
                                             ' {fonts: [{asset: a/baz}, {style: italic, weight: 400, asset: a/baz}]}]';
      expect(flutterManifest.fontsDescriptor.toString(), expectedFontsDescriptor);
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 1);
      final Font fooFont = fonts[0];
277
      const String barFontDescriptor = '{family: foo, fonts: [{asset: a/bar}, {weight: 400, style: italic, asset: a/bar}]}';
278 279 280 281 282
      expect(fooFont.descriptor.toString(), barFontDescriptor);
      expect(fooFont.familyName, 'foo');
      final List<FontAsset> fooAassets = fooFont.fontAssets;
      expect(fooAassets.length, 2);
      final FontAsset fooFontAsset0 = fooAassets[0];
283
      expect(fooFontAsset0.assetUri.path, 'a/bar');
284 285 286
      expect(fooFontAsset0.weight, isNull);
      expect(fooFontAsset0.style, isNull);
      final FontAsset fooFontAsset1 = fooAassets[1];
287
      expect(fooFontAsset1.assetUri.path, 'a/bar');
288 289 290 291
      expect(fooFontAsset1.weight, 400);
      expect(fooFontAsset1.style, 'italic');
    });

Josh Soref's avatar
Josh Soref committed
292
    testUsingContext('has only one of two font families when one declaration is missing the "fonts" option', () async {
293
      const String manifest = '''
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - asset: a/bar
        - asset: a/bar
          weight: 400
          style: italic
    - family: bar
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);

311
      const String expectedFontsDescriptor = '[{fonts: [{asset: a/bar}, {style: italic, weight: 400, asset: a/bar}], family: foo},'
312 313 314 315 316
                                             ' {family: bar}]';
      expect(flutterManifest.fontsDescriptor.toString(), expectedFontsDescriptor);
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 1);
      final Font fooFont = fonts[0];
317
      const String barFontDescriptor = '{family: foo, fonts: [{asset: a/bar}, {weight: 400, style: italic, asset: a/bar}]}';
318 319 320 321 322
      expect(fooFont.descriptor.toString(), barFontDescriptor);
      expect(fooFont.familyName, 'foo');
      final List<FontAsset> fooAassets = fooFont.fontAssets;
      expect(fooAassets.length, 2);
      final FontAsset fooFontAsset0 = fooAassets[0];
323
      expect(fooFontAsset0.assetUri.path, 'a/bar');
324 325 326
      expect(fooFontAsset0.weight, isNull);
      expect(fooFontAsset0.style, isNull);
      final FontAsset fooFontAsset1 = fooAassets[1];
327
      expect(fooFontAsset1.assetUri.path, 'a/bar');
328 329 330 331 332
      expect(fooFontAsset1.weight, 400);
      expect(fooFontAsset1.style, 'italic');
    });

    testUsingContext('has no font family when declaration is missing the "asset" option', () async {
333
      const String manifest = '''
334 335 336 337 338 339 340 341 342 343 344 345 346
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  uses-material-design: true
  fonts:
    - family: foo
      fonts:
        - weight: 400
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);

347
      const String expectedFontsDescriptor = '[{fonts: [{weight: 400}], family: foo}]';
348 349 350 351
      expect(flutterManifest.fontsDescriptor.toString(), expectedFontsDescriptor);
      final List<Font> fonts = flutterManifest.fonts;
      expect(fonts.length, 0);
    });
352 353 354 355 356 357 358 359 360 361 362 363

    test('allows a blank flutter section', () async {
      const String manifest = '''
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
      expect(flutterManifest.isEmpty, false);
    });
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

    Future<void> checkManifestVersion({
      String manifest,
      String expectedAppVersion,
      String expectedBuildName,
      int expectedBuildNumber,
    }) async {
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
      expect(flutterManifest.appVersion, expectedAppVersion);
      expect(flutterManifest.buildName, expectedBuildName);
      expect(flutterManifest.buildNumber, expectedBuildNumber);
    }

    test('parses major.minor.patch+build version clause', () async {
      const String manifest = '''
name: test
version: 1.0.0+2
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      await checkManifestVersion(
        manifest: manifest,
        expectedAppVersion: '1.0.0+2',
        expectedBuildName: '1.0.0',
        expectedBuildNumber: 2,
      );
    });

    test('parses major.minor+build version clause', () async {
      const String manifest = '''
name: test
version: 1.0+2
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      await checkManifestVersion(
        manifest: manifest,
        expectedAppVersion: '1.0+2',
        expectedBuildName: '1.0',
        expectedBuildNumber: 2,
      );
    });

    test('parses major+build version clause', () async {
      const String manifest = '''
name: test
version: 1+2
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      await checkManifestVersion(
        manifest: manifest,
        expectedAppVersion: '1+2',
        expectedBuildName: '1',
        expectedBuildNumber: 2,
      );
    });

    test('parses major version clause', () async {
      const String manifest = '''
name: test
version: 1
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      await checkManifestVersion(
        manifest: manifest,
        expectedAppVersion: '1',
        expectedBuildName: '1',
        expectedBuildNumber: null,
      );
    });

    test('parses empty version clause', () async {
      const String manifest = '''
name: test
version:
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      await checkManifestVersion(
        manifest: manifest,
        expectedAppVersion: null,
        expectedBuildName: null,
        expectedBuildNumber: null,
      );
    });
    test('parses no version clause', () async {
      const String manifest = '''
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
      await checkManifestVersion(
        manifest: manifest,
        expectedAppVersion: null,
        expectedBuildName: null,
        expectedBuildNumber: null,
      );
    });
476
  });
477 478 479 480 481 482 483 484 485 486 487

  group('FlutterManifest with MemoryFileSystem', () {
    void assertSchemaIsReadable() async {
      const String manifest = '''
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
''';

488
      final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
      expect(flutterManifest.isEmpty, false);
    }

    void writeSchemaFile(FileSystem filesystem, String schemaData) {
      final String schemaPath = buildSchemaPath(filesystem);
      final File schemaFile = filesystem.file(schemaPath);

      final String schemaDir = buildSchemaDir(filesystem);

      filesystem.directory(schemaDir).createSync(recursive: true);
      filesystem.file(schemaFile).writeAsStringSync(schemaData);
    }

    void testUsingContextAndFs(String description, FileSystem filesystem,
        dynamic testMethod()) {
      const String schemaData = '{}';

      testUsingContext(description,
              () async {
            writeSchemaFile( filesystem, schemaData);
            testMethod();
      },
          overrides: <Type, Generator>{
            FileSystem: () => filesystem,
          }
      );
    }

    testUsingContext('Validate manifest on original fs', () async {
      assertSchemaIsReadable();
    });

    testUsingContextAndFs('Validate manifest on Posix FS',
        new MemoryFileSystem(style: FileSystemStyle.posix), () async {
          assertSchemaIsReadable();
        }
    );

    testUsingContextAndFs('Validate manifest on Windows FS',
        new MemoryFileSystem(style: FileSystemStyle.windows), () async {
          assertSchemaIsReadable();
        }
    );

  });

535
}
536