|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:ui' as ui; |
| 3 | + |
| 4 | +import 'package:buzz/features/channels/message_content.dart'; |
| 5 | +import 'package:buzz/features/channels/reaction_row.dart'; |
| 6 | +import 'package:buzz/features/channels/timeline_message.dart'; |
| 7 | +import 'package:buzz/shared/theme/theme.dart'; |
| 8 | +import 'package:buzz/shared/custom_emoji/custom_emoji_provider.dart'; |
| 9 | +import 'package:flutter/material.dart'; |
| 10 | +import 'package:flutter/rendering.dart'; |
| 11 | +import 'package:flutter/services.dart'; |
| 12 | +import 'package:flutter_test/flutter_test.dart'; |
| 13 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 14 | + |
| 15 | +// Run on macOS to exercise real Inter and Apple color-emoji glyphs. The normal |
| 16 | +// widget-test font (Ahem) cannot distinguish a red emoji from a black symbol. |
| 17 | +// Optionally set HEART_SCREENSHOT to a PNG path to capture the real widgets. |
| 18 | +void main() { |
| 19 | + testWidgets( |
| 20 | + 'hearts render in color in reactions and message bodies', |
| 21 | + (tester) async { |
| 22 | + await tester.runAsync(() async { |
| 23 | + final inter = FontLoader('Inter') |
| 24 | + ..addFont(rootBundle.load('assets/fonts/InterVariable.ttf')); |
| 25 | + await inter.load(); |
| 26 | + final emoji = FontLoader('Apple Color Emoji') |
| 27 | + ..addFont( |
| 28 | + File( |
| 29 | + '/System/Library/Fonts/Apple Color Emoji.ttc', |
| 30 | + ).readAsBytes().then((bytes) => bytes.buffer.asByteData()), |
| 31 | + ); |
| 32 | + await emoji.load(); |
| 33 | + }); |
| 34 | + final scene = GlobalKey(); |
| 35 | + final reaction = GlobalKey(); |
| 36 | + final message = GlobalKey(); |
| 37 | + final emojiOnly = GlobalKey(); |
| 38 | + await tester.pumpWidget( |
| 39 | + ProviderScope( |
| 40 | + overrides: [customEmojiListProvider.overrideWithValue(const [])], |
| 41 | + child: MaterialApp( |
| 42 | + theme: AppTheme.light(), |
| 43 | + home: Scaffold( |
| 44 | + body: Center( |
| 45 | + child: RepaintBoundary( |
| 46 | + key: scene, |
| 47 | + child: Container( |
| 48 | + width: 360, |
| 49 | + color: Colors.white, |
| 50 | + padding: const EdgeInsets.all(24), |
| 51 | + child: Column( |
| 52 | + mainAxisSize: MainAxisSize.min, |
| 53 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 54 | + children: [ |
| 55 | + const Text('Reactions'), |
| 56 | + const SizedBox(height: 8), |
| 57 | + RepaintBoundary( |
| 58 | + key: reaction, |
| 59 | + child: ReactionRow( |
| 60 | + messageId: 'heart', |
| 61 | + reactions: const [ |
| 62 | + TimelineReaction( |
| 63 | + emoji: '❤️', |
| 64 | + count: 5, |
| 65 | + reactedByCurrentUser: true, |
| 66 | + userPubkeys: [], |
| 67 | + ), |
| 68 | + ], |
| 69 | + onToggle: (_) {}, |
| 70 | + ), |
| 71 | + ), |
| 72 | + const SizedBox(height: 24), |
| 73 | + const Text('Message text'), |
| 74 | + const SizedBox(height: 8), |
| 75 | + RepaintBoundary( |
| 76 | + key: message, |
| 77 | + child: const MessageContent( |
| 78 | + content: 'Thanks for the release ❤️ **Love it ❤️**', |
| 79 | + channelNames: {'general': 'general'}, |
| 80 | + ), |
| 81 | + ), |
| 82 | + const SizedBox(height: 24), |
| 83 | + const Text('Emoji-only message'), |
| 84 | + const SizedBox(height: 8), |
| 85 | + RepaintBoundary( |
| 86 | + key: emojiOnly, |
| 87 | + child: const MessageContent( |
| 88 | + content: '❤️', |
| 89 | + channelNames: {'general': 'general'}, |
| 90 | + scaleEmojiOnly: true, |
| 91 | + ), |
| 92 | + ), |
| 93 | + ], |
| 94 | + ), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ); |
| 102 | + await tester.pumpAndSettle(); |
| 103 | + await tester.runAsync(() async { |
| 104 | + final screenshot = Platform.environment['HEART_SCREENSHOT']; |
| 105 | + if (screenshot != null) { |
| 106 | + final image = await _capture(scene); |
| 107 | + final png = (await image.toByteData(format: ui.ImageByteFormat.png))!; |
| 108 | + await File(screenshot).parent.create(recursive: true); |
| 109 | + await File(screenshot).writeAsBytes(png.buffer.asUint8List()); |
| 110 | + image.dispose(); |
| 111 | + } |
| 112 | + for (final entry in { |
| 113 | + 'reaction': reaction, |
| 114 | + 'message': message, |
| 115 | + 'emoji-only message': emojiOnly, |
| 116 | + }.entries) { |
| 117 | + final image = await _capture(entry.value); |
| 118 | + final rgba = (await image.toByteData( |
| 119 | + format: ui.ImageByteFormat.rawRgba, |
| 120 | + ))!; |
| 121 | + var redPixels = 0; |
| 122 | + for (var i = 0; i < rgba.lengthInBytes; i += 4) { |
| 123 | + final red = rgba.getUint8(i); |
| 124 | + if (red > 140 && |
| 125 | + red > rgba.getUint8(i + 1) * 1.5 && |
| 126 | + red > rgba.getUint8(i + 2) * 1.5 && |
| 127 | + rgba.getUint8(i + 3) > 200) { |
| 128 | + redPixels++; |
| 129 | + } |
| 130 | + } |
| 131 | + image.dispose(); |
| 132 | + expect( |
| 133 | + redPixels, |
| 134 | + greaterThan(20), |
| 135 | + reason: |
| 136 | + '${entry.key} must contain red emoji, not monochrome hearts', |
| 137 | + ); |
| 138 | + } |
| 139 | + }); |
| 140 | + }, |
| 141 | + skip: !Platform.isMacOS, |
| 142 | + variant: TargetPlatformVariant.only(TargetPlatform.iOS), |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +Future<ui.Image> _capture(GlobalKey key) { |
| 147 | + final boundary = |
| 148 | + key.currentContext!.findRenderObject()! as RenderRepaintBoundary; |
| 149 | + return boundary.toImage(pixelRatio: 3); |
| 150 | +} |
0 commit comments