Files
ReelShort/test/home/feed_watch_all_gradient_test.dart

79 lines
2.3 KiB
Dart

// Author: fruit.com.cn
// 职责:检查观看全集按钮实际绘制像素,防止渐变只配置而不可见。
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/drama.dart';
import 'package:fruitshorts/l10n/app_localizations.dart';
import 'package:fruitshorts/pages/player/widgets/home_feed_recommend_panel.dart';
const _boundaryKey = Key('watch-gradient-boundary');
const _width = 280.0;
const _sampleInset = 16;
const _sampleY = 4;
const _rgbaChannels = 4;
const _minimumRedDelta = 50;
const _drama = Drama(
id: 'gradient',
title: 'Gradient',
synopsis: '',
posterUrl: '',
);
Future<void> _pumpButton(WidgetTester tester) => tester.pumpWidget(
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: Center(
child: RepaintBoundary(
key: _boundaryKey,
child: SizedBox(
width: _width,
child: HomeFeedWatchAllBar(drama: _drama, onTap: () {}),
),
),
),
),
),
);
Color _sample(ByteData bytes, int width, int x) {
final offset = (_sampleY * width + x) * _rgbaChannels;
return Color.fromARGB(
bytes.getUint8(offset + 3),
bytes.getUint8(offset),
bytes.getUint8(offset + 1),
bytes.getUint8(offset + 2),
);
}
void main() {
testWidgets('should_paint_visible_red_gradient_instead_of_flat_fill', (
tester,
) async {
await _pumpButton(tester);
final boundary = tester.renderObject<RenderRepaintBoundary>(
find.byKey(_boundaryKey),
);
await tester.runAsync(() async {
final image = await boundary.toImage();
final bytes = (await image.toByteData(
format: ui.ImageByteFormat.rawRgba,
))!;
final left = _sample(bytes, image.width, _sampleInset);
final middle = _sample(bytes, image.width, image.width ~/ 2);
final right = _sample(bytes, image.width, image.width - _sampleInset);
image.dispose();
expect(left.a, 1);
expect(right.a, 1);
expect((right.r - left.r) * 255, greaterThan(_minimumRedDelta));
expect(left.g, lessThan(middle.g));
expect(middle.g, lessThan(right.g));
});
});
}