|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'package:cupertino_ui/cupertino_ui.dart'; |
| 8 | +import 'package:flutter/foundation.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +/// Tuple-like test class for storing a [stream] and [eventKind]. |
| 12 | +/// |
| 13 | +/// Used to store the [stream] and [eventKind] that a dispatched event would be |
| 14 | +/// sent on. |
| 15 | +@immutable |
| 16 | +class DispatchedEventKey { |
| 17 | + const DispatchedEventKey({required this.stream, required this.eventKind}); |
| 18 | + |
| 19 | + final String stream; |
| 20 | + final String eventKind; |
| 21 | + |
| 22 | + @override |
| 23 | + String toString() { |
| 24 | + return '[DispatchedEventKey]($stream, $eventKind)'; |
| 25 | + } |
| 26 | + |
| 27 | + @override |
| 28 | + bool operator ==(Object other) { |
| 29 | + return other is DispatchedEventKey && stream == other.stream && eventKind == other.eventKind; |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + int get hashCode => Object.hash(stream, eventKind); |
| 34 | +} |
| 35 | + |
| 36 | +class TestWidgetInspectorService extends Object with WidgetInspectorService { |
| 37 | + TestWidgetInspectorService() { |
| 38 | + selection.addListener(() => selectionChangedCallback?.call()); |
| 39 | + } |
| 40 | + |
| 41 | + final Map<String, ServiceExtensionCallback> extensions = <String, ServiceExtensionCallback>{}; |
| 42 | + |
| 43 | + final Map<DispatchedEventKey, List<Map<Object, Object?>>> eventsDispatched = |
| 44 | + <DispatchedEventKey, List<Map<Object, Object?>>>{}; |
| 45 | + |
| 46 | + final List<Object?> objectsInspected = <Object?>[]; |
| 47 | + |
| 48 | + @override |
| 49 | + void registerServiceExtension({ |
| 50 | + required String name, |
| 51 | + required ServiceExtensionCallback callback, |
| 52 | + required RegisterServiceExtensionCallback registerExtension, |
| 53 | + }) { |
| 54 | + assert(!extensions.containsKey(name)); |
| 55 | + extensions[name] = callback; |
| 56 | + } |
| 57 | + |
| 58 | + @override |
| 59 | + void postEvent(String eventKind, Map<Object, Object?> eventData, {String stream = 'Extension'}) { |
| 60 | + dispatchedEvents(eventKind, stream: stream).add(eventData); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + void inspect(Object? object) { |
| 65 | + objectsInspected.add(object); |
| 66 | + } |
| 67 | + |
| 68 | + List<Map<Object, Object?>> dispatchedEvents(String eventKind, {String stream = 'Extension'}) { |
| 69 | + return eventsDispatched.putIfAbsent( |
| 70 | + DispatchedEventKey(stream: stream, eventKind: eventKind), |
| 71 | + () => <Map<Object, Object?>>[], |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + List<Object?> inspectedObjects() { |
| 76 | + return objectsInspected; |
| 77 | + } |
| 78 | + |
| 79 | + Iterable<Map<Object, Object?>> getServiceExtensionStateChangedEvents(String extensionName) { |
| 80 | + return dispatchedEvents( |
| 81 | + 'Flutter.ServiceExtensionStateChanged', |
| 82 | + ).where((Map<Object, Object?> event) => event['extension'] == extensionName); |
| 83 | + } |
| 84 | + |
| 85 | + Future<Object?> testExtension(String name, Map<String, String> arguments) async { |
| 86 | + expect(extensions, contains(name)); |
| 87 | + // Encode and decode to JSON to match behavior using a real service |
| 88 | + // extension where only JSON is allowed. |
| 89 | + return (json.decode(json.encode(await extensions[name]!(arguments))) |
| 90 | + as Map<String, dynamic>)['result']; |
| 91 | + } |
| 92 | + |
| 93 | + Future<String> testBoolExtension(String name, Map<String, String> arguments) async { |
| 94 | + expect(extensions, contains(name)); |
| 95 | + // Encode and decode to JSON to match behavior using a real service |
| 96 | + // extension where only JSON is allowed. |
| 97 | + return (json.decode(json.encode(await extensions[name]!(arguments))) |
| 98 | + as Map<String, dynamic>)['enabled'] |
| 99 | + .toString(); |
| 100 | + } |
| 101 | + |
| 102 | + int rebuildCount = 0; |
| 103 | + |
| 104 | + @override |
| 105 | + Future<void> forceRebuild() async { |
| 106 | + rebuildCount++; |
| 107 | + final WidgetsBinding binding = WidgetsBinding.instance; |
| 108 | + |
| 109 | + if (binding.rootElement != null) { |
| 110 | + binding.buildOwner!.reassemble(binding.rootElement!); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + void resetAllState() { |
| 116 | + super.resetAllState(); |
| 117 | + eventsDispatched.clear(); |
| 118 | + objectsInspected.clear(); |
| 119 | + rebuildCount = 0; |
| 120 | + } |
| 121 | +} |
0 commit comments