test: share cursor density policy fixtures

This commit is contained in:
fufesou
2026-09-14 08:32:09 +08:00
parent 19040f3eef
commit 8cf138e2cc
3 changed files with 129 additions and 199 deletions

View File

@@ -1,13 +1,10 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_custom_cursor/cursor_manager.dart' show CursorManager; import 'package:flutter_custom_cursor/cursor_manager.dart' show CursorManager;
import 'package:flutter_hbb/consts.dart'; import 'package:flutter_hbb/consts.dart';
import 'package:flutter_hbb/common.dart' as common; import 'package:flutter_hbb/common.dart' as common;
import 'package:flutter_hbb/desktop/pages/remote_page.dart'; import 'package:flutter_hbb/desktop/pages/remote_page.dart';
import 'package:flutter_hbb/models/input_model.dart';
import 'package:flutter_hbb/models/model.dart'; import 'package:flutter_hbb/models/model.dart';
import 'package:flutter_hbb/native/custom_cursor.dart'; import 'package:flutter_hbb/native/custom_cursor.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@@ -15,92 +12,12 @@ import 'package:get/get.dart';
import 'package:image/image.dart' as img; import 'package:image/image.dart' as img;
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
const _viewport = Size(200, 160); import 'cursor_test_utils.dart';
class _Image extends ChangeNotifier implements ImageModel {
@override
bool get useTextureRender => false;
@override
ui.Image? get image => null;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _Canvas extends ChangeNotifier implements CanvasModel {
_Canvas(this.devicePixelRatio, {required this.style, required this.scale});
final String style;
@override
final double devicePixelRatio;
@override
final imageOverflow = false.obs;
@override
late final viewStyle = ViewStyle(
style: style,
width: _viewport.width,
height: _viewport.height,
displayWidth: 400,
displayHeight: 320,
);
@override
bool get cursorEmbedded => false;
@override
ScrollStyle get scrollStyle => ScrollStyle.scrollauto;
@override
Size get size => _viewport;
@override
final double scale;
@override
double get x => 0;
@override
double get y => 0;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _Input extends Fake implements InputModel {
@override
final relativeMouseMode = false.obs;
}
class _Peer extends Fake implements FfiModel {
@override
final pi = PeerInfo();
@override
bool isPeerLinux = false;
}
class _FFI extends Fake implements FFI {
_FFI(this.canvasModel);
@override
final CanvasModel canvasModel;
@override
final inputModel = _Input();
@override
final _Peer ffiModel = _Peer();
}
void main() { void main() {
final binding = TestWidgetsFlutterBinding.ensureInitialized();
_rasterBoundsTests(); _rasterBoundsTests();
final channel = common.isWindows
? SystemChannels.mouseCursor
: const MethodChannel('flutter_custom_cursor');
final registrations = <Map<dynamic, dynamic>>[]; final registrations = <Map<dynamic, dynamic>>[];
setUp(() { captureNativeCursors(registrations);
registrations.clear();
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel,
(call) async {
if (!call.method.startsWith('createCustomCursor')) return null;
final args = call.arguments as Map<dynamic, dynamic>;
registrations.add(args);
return args['name'];
});
});
tearDown(() =>
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null));
_legacySizingTests(registrations); _legacySizingTests(registrations);
for (final style in [kRemoteViewStyleAdaptive, kRemoteViewStyleCustom]) { for (final style in [kRemoteViewStyleAdaptive, kRemoteViewStyleCustom]) {
for (final density in ['0', '1', '2', '1e-300', '0.001']) { for (final density in ['0', '1', '2', '1e-300', '0.001']) {
@@ -176,8 +93,8 @@ Future<void> checkDensity(WidgetTester tester, (String, String?) input,
final id = '$style-$density'; final id = '$style-$density';
tester.view.devicePixelRatio = dpr; tester.view.devicePixelRatio = dpr;
addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.view.resetDevicePixelRatio);
final canvas = _Canvas(dpr, style: style, scale: 0.5); final canvas = CursorTestCanvas(dpr, style: style, scale: 0.5);
final ffi = _FFI(canvas)..ffiModel.pi.platform = kPeerPlatformMacOS; final ffi = CursorTestFFI(canvas)..ffiModel.pi.platform = kPeerPlatformMacOS;
final cursor = CursorModel(WeakReference(ffi))..id = id; final cursor = CursorModel(WeakReference(ffi))..id = id;
addTearDown(() async { addTearDown(() async {
expect(cursor.parent.target, same(ffi)); expect(cursor.parent.target, same(ffi));
@@ -210,12 +127,12 @@ Future<void> checkDensity(WidgetTester tester, (String, String?) input,
} }
Future<void> _paintCursor( Future<void> _paintCursor(
WidgetTester tester, _FFI ffi, CursorModel cursor) async { WidgetTester tester, CursorTestFFI ffi, CursorModel cursor) async {
await tester.pumpWidget(MediaQuery( await tester.pumpWidget(MediaQuery(
data: MediaQueryData(devicePixelRatio: ffi.canvasModel.devicePixelRatio), data: MediaQueryData(devicePixelRatio: ffi.canvasModel.devicePixelRatio),
child: MultiProvider( child: MultiProvider(
providers: [ providers: [
ChangeNotifierProvider<ImageModel>(create: (_) => _Image()), ChangeNotifierProvider<ImageModel>(create: (_) => CursorTestImage()),
ChangeNotifierProvider<CanvasModel>.value(value: ffi.canvasModel), ChangeNotifierProvider<CanvasModel>.value(value: ffi.canvasModel),
ChangeNotifierProvider<CursorModel>.value(value: cursor), ChangeNotifierProvider<CursorModel>.value(value: cursor),
], ],
@@ -239,15 +156,7 @@ void _expectLegacyRaster(Map<dynamic, dynamic> args, Size size, double dpr) {
expect((args['hotX'], args['hotY']), expect((args['hotX'], args['hotY']),
((size.width ~/ 2) * rasterScale, (size.height ~/ 2) * rasterScale)); ((size.width ~/ 2) * rasterScale, (size.height ~/ 2) * rasterScale));
expect(args['imagePixelRatio'], dpr); expect(args['imagePixelRatio'], dpr);
final bytes = args['buffer'] as Uint8List; final decoded = decodeNativeCursorRaster(args);
final decoded = common.isWindows
? img.Image.fromBytes(
width: width,
height: height,
bytes: bytes.buffer,
bytesOffset: bytes.offsetInBytes,
order: img.ChannelOrder.bgra)
: img.decodePng(bytes)!;
expect((decoded.width, decoded.height), (bufferWidth, height)); expect((decoded.width, decoded.height), (bufferWidth, height));
expect(decoded.getPixel(width - 1, height - 1).a, 255); expect(decoded.getPixel(width - 1, height - 1).a, 255);
if (bufferWidth > width) expect(decoded.getPixel(width, 0).a, 0); if (bufferWidth > width) expect(decoded.getPixel(width, 0).a, 0);

View File

@@ -1,13 +1,11 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_custom_cursor/cursor_manager.dart' show CursorManager; import 'package:flutter_custom_cursor/cursor_manager.dart' show CursorManager;
import 'package:flutter_hbb/consts.dart'; import 'package:flutter_hbb/consts.dart';
import 'package:flutter_hbb/desktop/pages/remote_page.dart'; import 'package:flutter_hbb/desktop/pages/remote_page.dart';
import 'package:flutter_hbb/models/input_model.dart';
import 'package:flutter_hbb/models/model.dart'; import 'package:flutter_hbb/models/model.dart';
import 'package:flutter_hbb/native/custom_cursor.dart'; import 'package:flutter_hbb/native/custom_cursor.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@@ -15,49 +13,7 @@ import 'package:get/get.dart';
import 'package:image/image.dart' as img; import 'package:image/image.dart' as img;
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
const _viewport = Size(200, 160); import 'cursor_test_utils.dart';
class _Image extends ChangeNotifier implements ImageModel {
@override
bool get useTextureRender => false;
@override
ui.Image? get image => null;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _Canvas extends ChangeNotifier implements CanvasModel {
_Canvas(this.devicePixelRatio, {required this.style, required this.scale});
final String style;
@override
final double devicePixelRatio;
@override
final imageOverflow = false.obs;
@override
late final viewStyle = ViewStyle(
style: style,
width: _viewport.width,
height: _viewport.height,
displayWidth: 400,
displayHeight: 320,
);
@override
bool get cursorEmbedded => false;
@override
ScrollStyle get scrollStyle => ScrollStyle.scrollauto;
@override
Size get size => _viewport;
@override
final double scale;
@override
double get x => 0;
@override
double get y => 0;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _Cursor extends CursorModel { class _Cursor extends CursorModel {
_Cursor(this.cache, this._ffi) : super(WeakReference(_ffi)); _Cursor(this.cache, this._ffi) : super(WeakReference(_ffi));
@@ -74,34 +30,11 @@ class _Cursor extends CursorModel {
double get hoty => cache.hotyOrigin; double get hoty => cache.hotyOrigin;
} }
class _Input extends Fake implements InputModel {
@override
final relativeMouseMode = false.obs;
}
class _Peer extends Fake implements FfiModel {
@override
final pi = PeerInfo();
@override
bool isPeerLinux = false;
}
class _LinuxDisplay extends Display { class _LinuxDisplay extends Display {
@override @override
double get scale => 2; double get scale => 2;
} }
class _FFI extends Fake implements FFI {
_FFI(this.canvasModel);
@override
final CanvasModel canvasModel;
@override
final inputModel = _Input();
@override
final _Peer ffiModel = _Peer();
}
Future<CursorData> _data(int density, String id) async { Future<CursorData> _data(int density, String id) async {
final bitmapDensity = density == 0 ? 1 : density; final bitmapDensity = density == 0 ? 1 : density;
final image = await createTestImage( final image = await createTestImage(
@@ -124,25 +57,10 @@ Future<CursorData> _data(int density, String id) async {
void main() { void main() {
final binding = TestWidgetsFlutterBinding.ensureInitialized(); final binding = TestWidgetsFlutterBinding.ensureInitialized();
final view = binding.platformDispatcher.views.single; final view = binding.platformDispatcher.views.single;
final channel = Platform.isWindows
? SystemChannels.mouseCursor
: const MethodChannel('flutter_custom_cursor');
final windows = Platform.isWindows; final windows = Platform.isWindows;
final registrations = <Map<dynamic, dynamic>>[]; final registrations = <Map<dynamic, dynamic>>[];
setUp(() { captureNativeCursors(registrations);
registrations.clear(); tearDown(view.resetDevicePixelRatio);
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel,
(call) async {
if (!call.method.startsWith('createCustomCursor')) return null;
final args = call.arguments as Map<dynamic, dynamic>;
registrations.add(args);
return args['name'];
});
});
tearDown(() {
view.resetDevicePixelRatio();
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null);
});
for (final forbidden in [false, true]) { for (final forbidden in [false, true]) {
test('predefined cursor forbidden=$forbidden preserves native RGBA', () { test('predefined cursor forbidden=$forbidden preserves native RGBA', () {
view.devicePixelRatio = 1; view.devicePixelRatio = 1;
@@ -187,8 +105,8 @@ Future<void> _checkPolicy(
final data = await _data(density, '$style-$zoom-$density'); final data = await _data(density, '$style-$zoom-$density');
final originalBytes = data.data; final originalBytes = data.data;
// A stale cached DPR must not affect the cursor when the window moves. // A stale cached DPR must not affect the cursor when the window moves.
final canvas = _Canvas(1, style: style, scale: viewScale); final canvas = CursorTestCanvas(1, style: style, scale: viewScale);
final ffi = _FFI(canvas); final ffi = CursorTestFFI(canvas);
ffi.ffiModel.isPeerLinux = linux; ffi.ffiModel.isPeerLinux = linux;
if (linux) ffi.ffiModel.pi.displays.add(_LinuxDisplay()); if (linux) ffi.ffiModel.pi.displays.add(_LinuxDisplay());
final cursor = _Cursor(data, ffi); final cursor = _Cursor(data, ffi);
@@ -197,7 +115,7 @@ Future<void> _checkPolicy(
data: MediaQueryData(devicePixelRatio: dpr), data: MediaQueryData(devicePixelRatio: dpr),
child: MultiProvider( child: MultiProvider(
providers: [ providers: [
ChangeNotifierProvider<ImageModel>(create: (_) => _Image()), ChangeNotifierProvider<ImageModel>(create: (_) => CursorTestImage()),
ChangeNotifierProvider<CanvasModel>.value(value: canvas), ChangeNotifierProvider<CanvasModel>.value(value: canvas),
ChangeNotifierProvider<CursorModel>.value(value: cursor), ChangeNotifierProvider<CursorModel>.value(value: cursor),
], ],
@@ -247,8 +165,8 @@ Future<void> _checkDprChange(
TestFlutterView view, List<Map<dynamic, dynamic>> registrations) async { TestFlutterView view, List<Map<dynamic, dynamic>> registrations) async {
// Keep the scale above the minimum so only DPR invalidates the cache. // Keep the scale above the minimum so only DPR invalidates the cache.
final data = await _data(2, 'dpr-cache'); final data = await _data(2, 'dpr-cache');
final canvas = _Canvas(1, style: kRemoteViewStyleAdaptive, scale: 1); final canvas = CursorTestCanvas(1, style: kRemoteViewStyleAdaptive, scale: 1);
final cursor = _Cursor(data, _FFI(canvas)); final cursor = _Cursor(data, CursorTestFFI(canvas));
for (final dpr in [2.0, 1.0]) { for (final dpr in [2.0, 1.0]) {
view.devicePixelRatio = dpr; view.devicePixelRatio = dpr;
buildCursorOfCache(cursor, 1, data); buildCursorOfCache(cursor, 1, data);
@@ -271,23 +189,15 @@ Future<void> _checkPredefinedCursor(PredefinedCursor predefined,
final original = img final original = img
.decodePng(base64Decode(predefined.png))! .decodePng(base64Decode(predefined.png))!
.convert(format: img.Format.uint8, numChannels: 4); .convert(format: img.Format.uint8, numChannels: 4);
final canvas = _Canvas(1, style: kRemoteViewStyleAdaptive, scale: 1); final canvas = CursorTestCanvas(1, style: kRemoteViewStyleAdaptive, scale: 1);
final cursor = _Cursor(cache, _FFI(canvas)); final cursor = _Cursor(cache, CursorTestFFI(canvas));
buildCursorOfCache(cursor, 1, cache); buildCursorOfCache(cursor, 1, cache);
await deleteCustomCursor(cursor.cachedKeys.single); await deleteCustomCursor(cursor.cachedKeys.single);
cursor.dispose(); cursor.dispose();
canvas.dispose(); canvas.dispose();
final args = registrations.single; final args = registrations.single;
final bytes = args['buffer'] as Uint8List; final decoded = decodeNativeCursorRaster(args);
final decoded = Platform.isWindows
? img.Image.fromBytes(
width: args['width'] as int,
height: args['height'] as int,
bytes: bytes.buffer,
bytesOffset: bytes.offsetInBytes,
order: img.ChannelOrder.bgra)
: img.decodePng(bytes)!;
expect((decoded.width, decoded.height), (original.width, original.height)); expect((decoded.width, decoded.height), (original.width, original.height));
expect(args['imagePixelRatio'], 1.0); expect(args['imagePixelRatio'], 1.0);
expect((args['hotX'], args['hotY']), (cache.hotxOrigin, cache.hotyOrigin)); expect((args['hotX'], args['hotY']), (cache.hotxOrigin, cache.hotyOrigin));

View File

@@ -0,0 +1,111 @@
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_hbb/common.dart' as common;
import 'package:flutter_hbb/models/input_model.dart';
import 'package:flutter_hbb/models/model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:image/image.dart' as img;
class CursorTestImage extends ChangeNotifier implements ImageModel {
CursorTestImage({this.useTextureRender = false});
@override
final bool useTextureRender;
@override
ui.Image? get image => null;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class CursorTestCanvas extends ChangeNotifier implements CanvasModel {
CursorTestCanvas(this.devicePixelRatio,
{required this.style, required this.scale});
final String style;
@override
final double devicePixelRatio;
@override
final imageOverflow = false.obs;
@override
late final viewStyle = ViewStyle(
style: style,
width: size.width,
height: size.height,
displayWidth: 400,
displayHeight: 320,
);
@override
bool get cursorEmbedded => false;
@override
ScrollStyle get scrollStyle => ScrollStyle.scrollauto;
@override
Size get size => const Size(200, 160);
@override
final double scale;
@override
double get x => 0;
@override
double get y => 0;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _Input extends Fake implements InputModel {
@override
final relativeMouseMode = false.obs;
}
class CursorTestPeer extends Fake implements FfiModel {
@override
final pi = PeerInfo();
@override
bool isPeerLinux = false;
}
class CursorTestFFI extends Fake implements FFI {
CursorTestFFI(this.canvasModel);
@override
final CanvasModel canvasModel;
@override
final inputModel = _Input();
@override
final CursorTestPeer ffiModel = CursorTestPeer();
}
void captureNativeCursors(List<Map<dynamic, dynamic>> registrations,
{List<String>? activations}) {
final binding = TestWidgetsFlutterBinding.ensureInitialized();
final channel = common.isWindows
? SystemChannels.mouseCursor
: const MethodChannel('flutter_custom_cursor');
setUp(() {
registrations.clear();
activations?.clear();
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel,
(call) async {
if (call.method.startsWith('setCustomCursor')) {
activations?.add(call.arguments['name'] as String);
}
if (!call.method.startsWith('createCustomCursor')) return null;
final args = call.arguments as Map<dynamic, dynamic>;
registrations.add(args);
return args['name'];
});
});
tearDown(() =>
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null));
}
img.Image decodeNativeCursorRaster(Map<dynamic, dynamic> args) {
final bytes = args['buffer'] as Uint8List;
return common.isWindows
? img.Image.fromBytes(
width: args['width'] as int,
height: args['height'] as int,
bytes: bytes.buffer,
bytesOffset: bytes.offsetInBytes,
order: img.ChannelOrder.bgra)
: img.decodePng(bytes)!;
}