mouse_tracker_test_utils.dart 3.68 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
  @override
  void initInstances() {
    super.initInstances();
    postFrameCallbacks = <void Function(Duration)>[];
  }

35 36 37 38 39 40 41 42
  late final RenderView _renderView = RenderView(
    view: platformDispatcher.implicitView!,
  );

  late final PipelineOwner _pipelineOwner = PipelineOwner(
    onSemanticsUpdate: (ui.SemanticsUpdate _) { assert(false); },
  );

43
  void setHitTest(BoxHitTest hitTest) {
44 45 46 47 48 49
    if (_pipelineOwner.rootNode == null) {
      _pipelineOwner.rootNode = _renderView;
      rootPipelineOwner.adoptChild(_pipelineOwner);
      addRenderView(_renderView);
    }
    _renderView.child = _TestHitTester(hitTest);
50 51
  }

52
  SchedulerPhase? _overridePhase;
53 54 55 56 57 58 59 60
  @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() {
61
    final SchedulerPhase? lastPhase = _overridePhase;
62 63
    _overridePhase = SchedulerPhase.persistentCallbacks;
    addPostFrameCallback((_) {
64
      mouseTracker.updateAllDevices();
65 66 67 68
    });
    _overridePhase = lastPhase;
  }

69
  List<void Function(Duration)> postFrameCallbacks = <void Function(Duration)>[];
70 71 72

  // Proxy post-frame callbacks.
  @override
73
  void addPostFrameCallback(void Function(Duration) callback, {String debugLabel = 'callback'}) {
74 75 76 77 78 79 80 81 82 83 84 85
    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].
86
class TestAnnotationTarget with Diagnosticable implements MouseTrackerAnnotation, HitTestTarget {
87
  const TestAnnotationTarget({this.onEnter, this.onHover, this.onExit, this.cursor = MouseCursor.defer, this.validForMouseTracker = true});
88 89

  @override
90
  final PointerEnterEventListener? onEnter;
91

92
  final PointerHoverEventListener? onHover;
93 94

  @override
95
  final PointerExitEventListener? onExit;
96 97 98 99

  @override
  final MouseCursor cursor;

100 101 102
  @override
  final bool validForMouseTracker;

103 104
  @override
  void handleEvent(PointerEvent event, HitTestEntry entry) {
105
    if (event is PointerHoverEvent) {
106
      onHover?.call(event);
107
    }
108 109 110 111 112
  }
}

// A hit test entry that can be assigned with a [TestAnnotationTarget] and an
// optional transform matrix.
113
class TestAnnotationEntry extends HitTestEntry<TestAnnotationTarget> {
114 115
  TestAnnotationEntry(super.target, [Matrix4? transform])
    : transform = transform ?? Matrix4.identity();
116 117 118 119

  @override
  final Matrix4 transform;
}