app_test.dart 6.46 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 'dart:math' as math;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

import 'package:wide_gamut_test/main.dart' as app;

// See: https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbgr10_xr.
double _decodeBGR10(int x) {
  const double max = 1.25098;
  const double min = -0.752941;
  const double intercept = min;
  const double slope = (max - min) / 1024.0;
  return (x * slope) + intercept;
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
double _decodeHalf(int x) {
  if (x == 0x7c00) {
    return double.infinity;
  }
  if (x == 0xfc00) {
    return -double.infinity;
  }
  final double sign = x & 0x8000 == 0 ? 1.0 : -1.0;
  final int exponent = (x >> 10) & 0x1f;
  final int fraction = x & 0x3ff;
  if (exponent == 0) {
    return sign * math.pow(2.0, -14) * (fraction / 1024.0);
  } else {
    return sign * math.pow(2.0, exponent - 15) * (1.0 + fraction / 1024.0);
  }
}

40 41 42 43
bool _isAlmost(double x, double y, double epsilon) {
  return (x - y).abs() < epsilon;
}

44 45
List<double> _deepRed = <double>[1.0931, -0.2268, -0.1501];

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
bool _findRGBAF16Color(
    Uint8List bytes, int width, int height, List<double> color) {
  final ByteData byteData = ByteData.sublistView(bytes);
  expect(bytes.lengthInBytes, width * height * 8);
  expect(bytes.lengthInBytes, byteData.lengthInBytes);
  bool foundDeepRed = false;
  for (int i = 0; i < bytes.lengthInBytes; i += 8) {
    final int pixel = byteData.getUint64(i, Endian.host);
    final double blue = _decodeHalf((pixel >> 32) & 0xffff);
    final double green = _decodeHalf((pixel >> 16) & 0xffff);
    final double red = _decodeHalf((pixel >> 0) & 0xffff);
    if (_isAlmost(red, color[0], 0.01) &&
        _isAlmost(green, color[1], 0.01) &&
        _isAlmost(blue, color[2], 0.01)) {
      foundDeepRed = true;
    }
  }
  return foundDeepRed;
}

66 67
bool _findBGRA10Color(
    Uint8List bytes, int width, int height, List<double> color) {
68 69 70 71 72 73 74 75 76
  final ByteData byteData = ByteData.sublistView(bytes);
  expect(bytes.lengthInBytes, width * height * 8);
  expect(bytes.lengthInBytes, byteData.lengthInBytes);
  bool foundDeepRed = false;
  for (int i = 0; i < bytes.lengthInBytes; i += 8) {
    final int pixel = byteData.getUint64(i, Endian.host);
    final double blue = _decodeBGR10((pixel >> 6) & 0x3ff);
    final double green = _decodeBGR10((pixel >> 22) & 0x3ff);
    final double red = _decodeBGR10((pixel >> 38) & 0x3ff);
77 78 79
    if (_isAlmost(red, color[0], 0.01) &&
        _isAlmost(green, color[1], 0.01) &&
        _isAlmost(blue, color[2], 0.01)) {
80 81 82 83 84 85
      foundDeepRed = true;
    }
  }
  return foundDeepRed;
}

86 87
bool _findBGR10Color(
    Uint8List bytes, int width, int height, List<double> color) {
88 89 90 91 92 93 94 95 96
  final ByteData byteData = ByteData.sublistView(bytes);
  expect(bytes.lengthInBytes, width * height * 4);
  expect(bytes.lengthInBytes, byteData.lengthInBytes);
  bool foundDeepRed = false;
  for (int i = 0; i < bytes.lengthInBytes; i += 4) {
    final int pixel = byteData.getUint32(i, Endian.host);
    final double blue = _decodeBGR10(pixel & 0x3ff);
    final double green = _decodeBGR10((pixel >> 10) & 0x3ff);
    final double red = _decodeBGR10((pixel >> 20) & 0x3ff);
97 98 99
    if (_isAlmost(red, color[0], 0.01) &&
        _isAlmost(green, color[1], 0.01) &&
        _isAlmost(blue, color[2], 0.01)) {
100 101 102 103 104 105
      foundDeepRed = true;
    }
  }
  return foundDeepRed;
}

106
bool _findColor(List<Object?> result, List<double> color) {
107 108 109 110 111 112
  expect(result, isNotNull);
  expect(result.length, 4);
  final int width = (result[0] as int?)!;
  final int height = (result[1] as int?)!;
  final String format = (result[2] as String?)!;
  if (format == 'MTLPixelFormatBGR10_XR') {
113
    return _findBGR10Color((result[3] as Uint8List?)!, width, height, color);
114
  } else if (format == 'MTLPixelFormatBGRA10_XR') {
115
    return _findBGRA10Color((result[3] as Uint8List?)!, width, height, color);
116 117
  } else if (format == 'MTLPixelFormatRGBA16Float') {
    return _findRGBAF16Color((result[3] as Uint8List?)!, width, height, color);
118 119 120 121 122
  } else {
    fail('Unsupported pixel format: $format');
  }
}

123 124 125 126 127
void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('end-to-end test', () {
    testWidgets('look for display p3 deepest red', (WidgetTester tester) async {
128 129 130 131 132 133
      app.run(app.Setup.image);
      await tester.pumpAndSettle(const Duration(seconds: 2));

      const MethodChannel channel = MethodChannel('flutter/screenshot');
      final List<Object?> result =
          await channel.invokeMethod('test') as List<Object?>;
134
      expect(_findColor(result, _deepRed), isTrue);
135 136 137
    });
    testWidgets('look for display p3 deepest red', (WidgetTester tester) async {
      app.run(app.Setup.canvasSaveLayer);
138 139 140 141 142
      await tester.pumpAndSettle(const Duration(seconds: 2));

      const MethodChannel channel = MethodChannel('flutter/screenshot');
      final List<Object?> result =
          await channel.invokeMethod('test') as List<Object?>;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      expect(_findColor(result, _deepRed), isTrue);
    });
    testWidgets('no p3 deepest red without image', (WidgetTester tester) async {
      app.run(app.Setup.none);
      await tester.pumpAndSettle(const Duration(seconds: 2));

      const MethodChannel channel = MethodChannel('flutter/screenshot');
      final List<Object?> result =
          await channel.invokeMethod('test') as List<Object?>;
      expect(_findColor(result, _deepRed), isFalse);
      expect(_findColor(result, <double>[0.0, 1.0, 0.0]), isFalse);
    });
    testWidgets('p3 deepest red with blur', (WidgetTester tester) async {
      app.run(app.Setup.blur);
      await tester.pumpAndSettle(const Duration(seconds: 2));

      const MethodChannel channel = MethodChannel('flutter/screenshot');
      final List<Object?> result =
          await channel.invokeMethod('test') as List<Object?>;
      expect(_findColor(result, _deepRed), isTrue);
      expect(_findColor(result, <double>[0.0, 1.0, 0.0]), isTrue);
164
    });
165 166 167 168 169 170 171 172 173
    testWidgets('draw image with wide gamut works', (WidgetTester tester) async {
      app.run(app.Setup.drawnImage);
      await tester.pumpAndSettle(const Duration(seconds: 2));

      const MethodChannel channel = MethodChannel('flutter/screenshot');
      final List<Object?> result =
          await channel.invokeMethod('test') as List<Object?>;
      expect(_findColor(result, <double>[0.0, 1.0, 0.0]), isTrue);
    });
174 175
  });
}