mouse_tracker_test_utils.dart 3.16 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 12 13 14 15 16 17 18
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';

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

  final BoxHitTest hitTestOverride;

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

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

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

38
  SchedulerPhase? _overridePhase;
39 40 41 42 43 44 45 46
  @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() {
47
    final SchedulerPhase? lastPhase = _overridePhase;
48 49 50 51 52 53 54
    _overridePhase = SchedulerPhase.persistentCallbacks;
    addPostFrameCallback((_) {
      mouseTracker.updateAllDevices(renderView.hitTestMouseTrackers);
    });
    _overridePhase = lastPhase;
  }

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

  // 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  {
73
  const TestAnnotationTarget({this.onEnter, this.onHover, this.onExit, this.cursor = MouseCursor.defer, this.validForMouseTracker = true});
74 75

  @override
76
  final PointerEnterEventListener? onEnter;
77

78
  final PointerHoverEventListener? onHover;
79 80

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

  @override
  final MouseCursor cursor;

86 87 88
  @override
  final bool validForMouseTracker;

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

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

  @override
  final Matrix4 transform;
}