image_resolution_test.dart 6.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

class TestAssetBundle extends CachingAssetBundle {
14
  TestAssetBundle(this._assetBundleMap);
15

16
  final Map<String, List<String>> _assetBundleMap;
17 18 19 20 21 22 23 24 25 26

  Map<String, int> loadCallCount = <String, int>{};

  String get _assetBundleContents {
    return json.encode(_assetBundleMap);
  }

  @override
  Future<ByteData> load(String key) async {
    if (key == 'AssetManifest.json')
27
      return ByteData.view(Uint8List.fromList(
28 29 30 31
          const Utf8Encoder().convert(_assetBundleContents)).buffer);

    loadCallCount[key] = loadCallCount[key] ?? 0 + 1;
    if (key == 'one')
32
      return ByteData(1)
33
        ..setInt8(0, 49);
34
    throw FlutterError('key not found');
35 36 37 38 39 40 41 42 43 44
  }
}

void main() {
  group('1.0 scale device tests', () {
    void _buildAndTestWithOneAsset(String mainAssetPath) {
      final Map<String, List<String>> assetBundleMap = <String, List<String>>{};

      assetBundleMap[mainAssetPath] = <String>[];

45
      final AssetImage assetImage = AssetImage(
46
          mainAssetPath,
47
          bundle: TestAssetBundle(assetBundleMap));
48
      const ImageConfiguration configuration = ImageConfiguration();
49 50

      assetImage.obtainKey(configuration)
51 52 53 54
        .then(expectAsync1((AssetBundleImageKey bundleKey) {
          expect(bundleKey.name, mainAssetPath);
          expect(bundleKey.scale, 1.0);
        }));
55 56 57 58 59 60
    }

    test('When asset is main variant check scale is 1.0', () {
      _buildAndTestWithOneAsset('assets/normalFolder/normalFile.png');
    });

61 62 63
    test('When asset path and key are the same string even though it could be took as a 3.0x variant', () async {
      _buildAndTestWithOneAsset('assets/parentFolder/3.0x/normalFile.png');
    });
64

65 66 67
    test('When asset path contains variant identifier as part of parent folder name scale is 1.0', () {
      _buildAndTestWithOneAsset('assets/parentFolder/__3.0x__/leafFolder/normalFile.png');
    });
68

69 70 71
    test('When asset path contains variant identifier as part of leaf folder name scale is 1.0', () {
      _buildAndTestWithOneAsset('assets/parentFolder/__3.0x_leaf_folder_/normalFile.png');
    });
72

73 74 75
    test('When asset path contains variant identifier as part of parent folder name scale is 1.0', () {
      _buildAndTestWithOneAsset('assets/parentFolder/__3.0x__/leafFolder/normalFile.png');
    });
76

77 78 79
    test('When asset path contains variant identifier in parent folder scale is 1.0', () {
      _buildAndTestWithOneAsset('assets/parentFolder/3.0x/leafFolder/normalFile.png');
    });
80 81 82 83 84 85 86 87 88 89 90 91 92
  });


  group('High-res device behavior tests', () {
    test('When asset is not main variant check scale is not 1.0', () {
      const String mainAssetPath = 'assets/normalFolder/normalFile.png';
      const String variantPath = 'assets/normalFolder/3.0x/normalFile.png';

      final Map<String, List<String>> assetBundleMap =
      <String, List<String>>{};

      assetBundleMap[mainAssetPath] = <String>[mainAssetPath, variantPath];

93
      final TestAssetBundle testAssetBundle = TestAssetBundle(
94 95
          assetBundleMap);

96
      final AssetImage assetImage = AssetImage(
97 98 99 100 101
          mainAssetPath,
          bundle: testAssetBundle);

      // we have the exact match for this scale, let's use it
      assetImage.obtainKey(const ImageConfiguration())
102 103 104 105
        .then(expectAsync1((AssetBundleImageKey bundleKey) {
          expect(bundleKey.name, mainAssetPath);
          expect(bundleKey.scale, 1.0);
        }));
106 107

      // we also have the exact match for this scale, let's use it
108
      assetImage.obtainKey(ImageConfiguration(
109 110 111
        bundle: testAssetBundle,
        devicePixelRatio: 3.0,
      )).then(expectAsync1((AssetBundleImageKey bundleKey) {
112 113 114 115 116
        expect(bundleKey.name, variantPath);
        expect(bundleKey.scale, 3.0);
      }));
    });

117
    test('When high-res device and high-res asset not present in bundle then  return main variant', () {
118 119 120 121 122 123 124
      const String mainAssetPath = 'assets/normalFolder/normalFile.png';

      final Map<String, List<String>> assetBundleMap =
      <String, List<String>>{};

      assetBundleMap[mainAssetPath] = <String>[mainAssetPath];

125
      final TestAssetBundle testAssetBundle = TestAssetBundle(
126 127
          assetBundleMap);

128
      final AssetImage assetImage = AssetImage(
129
          mainAssetPath,
130
          bundle: TestAssetBundle(assetBundleMap));
131 132 133


      assetImage.obtainKey(const ImageConfiguration())
134 135 136 137
        .then(expectAsync1((AssetBundleImageKey bundleKey) {
          expect(bundleKey.name, mainAssetPath);
          expect(bundleKey.scale, 1.0);
        }));
138

139
      assetImage.obtainKey(ImageConfiguration(
140
        bundle: testAssetBundle,
141
        devicePixelRatio: 3.0),
142
      ).then(expectAsync1((AssetBundleImageKey bundleKey) {
143 144 145 146
        expect(bundleKey.name, mainAssetPath);
        expect(bundleKey.scale, 1.0);
      }));
    });
147
  });
148

149
  group('Regression - When assets available are 1.0 and 3.0 check devices with a range of scales', () {
150 151 152 153
    const String mainAssetPath = 'assets/normalFolder/normalFile.png';
    const String variantPath = 'assets/normalFolder/3.0x/normalFile.png';


154 155 156 157 158
    void _buildBundleAndTestVariantLogic(
      double deviceRatio,
      double chosenAssetRatio,
      String expectedAssetPath,
    ) {
159 160 161 162 163
      final Map<String, List<String>> assetBundleMap =
      <String, List<String>>{};

      assetBundleMap[mainAssetPath] = <String>[mainAssetPath, variantPath];

164
      final TestAssetBundle testAssetBundle = TestAssetBundle(
165 166
          assetBundleMap);

167
      final AssetImage assetImage = AssetImage(
168 169 170 171
          mainAssetPath,
          bundle: testAssetBundle);

      // we have 1.0 and 3.0, asking for 1.5 should give
172
      assetImage.obtainKey(ImageConfiguration(
173
        bundle: testAssetBundle,
174
        devicePixelRatio: deviceRatio),
175
      ).then(expectAsync1((AssetBundleImageKey bundleKey) {
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        expect(bundleKey.name, expectedAssetPath);
        expect(bundleKey.scale, chosenAssetRatio);
      }));
    }

    test('Obvious case 1.0 - we have exact asset', () {
      _buildBundleAndTestVariantLogic(1.0, 1.0, mainAssetPath);
    });

    test('Obvious case 3.0 - we have exact asset', () {
      _buildBundleAndTestVariantLogic(3.0, 3.0, variantPath);
    });

    test('Typical case 2.0', () {
      _buildBundleAndTestVariantLogic(2.0, 1.0, mainAssetPath);
    });

    test('Borderline case 2.01', () {
      _buildBundleAndTestVariantLogic(2.01, 3.0, variantPath);
    });
    test('Borderline case 2.9', () {
      _buildBundleAndTestVariantLogic(2.9, 3.0, variantPath);
    });

    test('Typical case 4.0', () {
      _buildBundleAndTestVariantLogic(4.0, 3.0, variantPath);
    });
203
  });
204 205

}