mouse_tracker_test_utils.dart 3.28 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
9
import 'package:flutter/rendering.dart';
10 11
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
12
import 'package:flutter_test/flutter_test.dart' show TestDefaultBinaryMessengerBinding;
13 14 15 16 17 18 19

class _TestHitTester extends RenderBox {
  _TestHitTester(this.hitTestOverride);

  final BoxHitTest hitTestOverride;

  @override
20
  bool hitTest(BoxHitTestResult result, {required ui.Offset position}) {
21 22 23 24 25 26 27
    return hitTestOverride(result, position);
  }
}

// A binding used to test MouseTracker, allowing the test to override hit test
// searching.
class TestMouseTrackerFlutterBinding extends BindingBase
28
    with SchedulerBinding, ServicesBinding, GestureBinding, SemanticsBinding, RendererBinding, TestDefaultBinaryMessengerBinding {
29 30 31 32 33 34 35 36 37 38
  @override
  void initInstances() {
    super.initInstances();
    postFrameCallbacks = <void Function(Duration)>[];
  }

  void setHitTest(BoxHitTest hitTest) {
    renderView.child = _TestHitTester(hitTest);
  }

39
  SchedulerPhase? _overridePhase;
40 41 42 43 44 45 46 47
  @override
  SchedulerPhase get schedulerPhase => _overridePhase ?? super.schedulerPhase;

  // Manually schedule a post-frame check.
  //
  // In real apps this is done by the renderer binding, but in tests we have to
  // bypass the phase assertion of [MouseTracker.schedulePostFrameCheck].
  void scheduleMouseTrackerPostFrameCheck() {
48
    final SchedulerPhase? lastPhase = _overridePhase;
49 50 51 52 53 54 55
    _overridePhase = SchedulerPhase.persistentCallbacks;
    addPostFrameCallback((_) {
      mouseTracker.updateAllDevices(renderView.hitTestMouseTrackers);
    });
    _overridePhase = lastPhase;
  }

56
  List<void Function(Duration)> postFrameCallbacks = <void Function(Duration)>[];
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

  // Proxy post-frame callbacks.
  @override
  void addPostFrameCallback(void Function(Duration) callback) {
    postFrameCallbacks.add(callback);
  }

  void flushPostFrameCallbacks(Duration duration) {
    for (final void Function(Duration) callback in postFrameCallbacks) {
      callback(duration);
    }
    postFrameCallbacks.clear();
  }
}

// An object that mocks the behavior of a render object with [MouseTrackerAnnotation].
class TestAnnotationTarget with Diagnosticable implements MouseTrackerAnnotation, HitTestTarget  {
74
  const TestAnnotationTarget({this.onEnter, this.onHover, this.onExit, this.cursor = MouseCursor.defer, this.validForMouseTracker = true});
75 76

  @override
77
  final PointerEnterEventListener? onEnter;
78

79
  final PointerHoverEventListener? onHover;
80 81

  @override
82
  final PointerExitEventListener? onExit;
83 84 85 86

  @override
  final MouseCursor cursor;

87 88 89
  @override
  final bool validForMouseTracker;

90 91 92
  @override
  void handleEvent(PointerEvent event, HitTestEntry entry) {
    if (event is PointerHoverEvent)
93
      onHover?.call(event);
94 95 96 97 98 99
  }
}

// A hit test entry that can be assigned with a [TestAnnotationTarget] and an
// optional transform matrix.
class TestAnnotationEntry extends HitTestEntry {
100
  TestAnnotationEntry(TestAnnotationTarget target, [Matrix4? transform])
101 102 103 104 105
    : transform = transform ?? Matrix4.identity(), super(target);

  @override
  final Matrix4 transform;
}