actions_test.dart 14 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
6
import 'package:flutter/gestures.dart';
7
import 'package:flutter/material.dart';
8
import 'package:flutter/services.dart';
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

typedef PostInvokeCallback = void Function({Action action, Intent intent, FocusNode focusNode, ActionDispatcher dispatcher});

class TestAction extends CallbackAction {
  const TestAction({
    @required OnInvokeCallback onInvoke,
  })  : assert(onInvoke != null),
        super(key, onInvoke: onInvoke);

  static const LocalKey key = ValueKey<Type>(TestAction);

  void _testInvoke(FocusNode node, Intent invocation) => invoke(node, invocation);
}

class TestDispatcher extends ActionDispatcher {
  const TestDispatcher({this.postInvoke});

  final PostInvokeCallback postInvoke;

  @override
  bool invokeAction(Action action, Intent intent, {FocusNode focusNode}) {
    final bool result = super.invokeAction(action, intent, focusNode: focusNode);
    postInvoke?.call(action: action, intent: intent, focusNode: focusNode, dispatcher: this);
    return result;
  }
}

class TestDispatcher1 extends TestDispatcher {
  const TestDispatcher1({PostInvokeCallback postInvoke}) : super(postInvoke: postInvoke);
}

void main() {
43
  test('Action passes parameters on when invoked.', () {
44 45 46 47 48 49 50 51 52 53 54 55 56
    bool invoked = false;
    FocusNode passedNode;
    final TestAction action = TestAction(onInvoke: (FocusNode node, Intent invocation) {
      invoked = true;
      passedNode = node;
    });
    final FocusNode testNode = FocusNode(debugLabel: 'Test Node');
    action._testInvoke(testNode, null);
    expect(passedNode, equals(testNode));
    expect(action.intentKey, equals(TestAction.key));
    expect(invoked, isTrue);
  });
  group(ActionDispatcher, () {
57
    test('ActionDispatcher invokes actions when asked.', () {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      bool invoked = false;
      FocusNode passedNode;
      const ActionDispatcher dispatcher = ActionDispatcher();
      final FocusNode testNode = FocusNode(debugLabel: 'Test Node');
      final bool result = dispatcher.invokeAction(
        TestAction(
          onInvoke: (FocusNode node, Intent invocation) {
            invoked = true;
            passedNode = node;
          },
        ),
        const Intent(TestAction.key),
        focusNode: testNode,
      );
      expect(passedNode, equals(testNode));
      expect(result, isTrue);
      expect(invoked, isTrue);
    });
  });
  group(Actions, () {
    Intent invokedIntent;
    Action invokedAction;
    FocusNode invokedNode;
    ActionDispatcher invokedDispatcher;

    void collect({Action action, Intent intent, FocusNode focusNode, ActionDispatcher dispatcher}) {
      invokedIntent = intent;
      invokedAction = action;
      invokedNode = focusNode;
      invokedDispatcher = dispatcher;
    }

    void clear() {
      invokedIntent = null;
      invokedAction = null;
      invokedNode = null;
      invokedDispatcher = null;
    }

    setUp(clear);

99
    testWidgets('Actions widget can invoke actions with default dispatcher', (WidgetTester tester) async {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      final GlobalKey containerKey = GlobalKey();
      bool invoked = false;
      FocusNode passedNode;
      final FocusNode testNode = FocusNode(debugLabel: 'Test Node');

      await tester.pumpWidget(
        Actions(
          actions: <LocalKey, ActionFactory>{
            TestAction.key: () => TestAction(
                  onInvoke: (FocusNode node, Intent invocation) {
                    invoked = true;
                    passedNode = node;
                  },
                ),
          },
          child: Container(key: containerKey),
        ),
      );

      await tester.pump();
      final bool result = Actions.invoke(
        containerKey.currentContext,
        const Intent(TestAction.key),
        focusNode: testNode,
      );
      expect(passedNode, equals(testNode));
      expect(result, isTrue);
      expect(invoked, isTrue);
    });
129
    testWidgets('Actions widget can invoke actions with custom dispatcher', (WidgetTester tester) async {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      final GlobalKey containerKey = GlobalKey();
      bool invoked = false;
      const Intent intent = Intent(TestAction.key);
      FocusNode passedNode;
      final FocusNode testNode = FocusNode(debugLabel: 'Test Node');
      final Action testAction = TestAction(
        onInvoke: (FocusNode node, Intent intent) {
          invoked = true;
          passedNode = node;
        },
      );

      await tester.pumpWidget(
        Actions(
          dispatcher: TestDispatcher(postInvoke: collect),
          actions: <LocalKey, ActionFactory>{
            TestAction.key: () => testAction,
          },
          child: Container(key: containerKey),
        ),
      );

      await tester.pump();
      final bool result = Actions.invoke(
        containerKey.currentContext,
        intent,
        focusNode: testNode,
      );
      expect(passedNode, equals(testNode));
      expect(invokedNode, equals(testNode));
      expect(result, isTrue);
      expect(invoked, isTrue);
      expect(invokedIntent, equals(intent));
    });
164
    testWidgets('Actions can invoke actions in ancestor dispatcher', (WidgetTester tester) async {
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      final GlobalKey containerKey = GlobalKey();
      bool invoked = false;
      const Intent intent = Intent(TestAction.key);
      FocusNode passedNode;
      final FocusNode testNode = FocusNode(debugLabel: 'Test Node');
      final Action testAction = TestAction(
        onInvoke: (FocusNode node, Intent invocation) {
          invoked = true;
          passedNode = node;
        },
      );

      await tester.pumpWidget(
        Actions(
          dispatcher: TestDispatcher1(postInvoke: collect),
          actions: <LocalKey, ActionFactory>{
            TestAction.key: () => testAction,
          },
          child: Actions(
            dispatcher: TestDispatcher(postInvoke: collect),
            actions: const <LocalKey, ActionFactory>{},
            child: Container(key: containerKey),
          ),
        ),
      );

      await tester.pump();
      final bool result = Actions.invoke(
        containerKey.currentContext,
        intent,
        focusNode: testNode,
      );
      expect(passedNode, equals(testNode));
      expect(invokedNode, equals(testNode));
      expect(result, isTrue);
      expect(invoked, isTrue);
      expect(invokedIntent, equals(intent));
      expect(invokedAction, equals(testAction));
      expect(invokedDispatcher.runtimeType, equals(TestDispatcher1));
    });
205
    testWidgets("Actions can invoke actions in ancestor dispatcher if a lower one isn't specified", (WidgetTester tester) async {
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
      final GlobalKey containerKey = GlobalKey();
      bool invoked = false;
      const Intent intent = Intent(TestAction.key);
      FocusNode passedNode;
      final FocusNode testNode = FocusNode(debugLabel: 'Test Node');
      final Action testAction = TestAction(
        onInvoke: (FocusNode node, Intent invocation) {
          invoked = true;
          passedNode = node;
        },
      );

      await tester.pumpWidget(
        Actions(
          dispatcher: TestDispatcher1(postInvoke: collect),
          actions: <LocalKey, ActionFactory>{
            TestAction.key: () => testAction,
          },
          child: Actions(
            actions: const <LocalKey, ActionFactory>{},
            child: Container(key: containerKey),
          ),
        ),
      );

      await tester.pump();
      final bool result = Actions.invoke(
        containerKey.currentContext,
        intent,
        focusNode: testNode,
      );
      expect(passedNode, equals(testNode));
      expect(invokedNode, equals(testNode));
      expect(result, isTrue);
      expect(invoked, isTrue);
      expect(invokedIntent, equals(intent));
      expect(invokedAction, equals(testAction));
      expect(invokedDispatcher.runtimeType, equals(TestDispatcher1));
    });
245
    testWidgets('Actions widget can be found with of', (WidgetTester tester) async {
246 247 248 249 250 251 252 253 254 255 256 257 258
      final GlobalKey containerKey = GlobalKey();
      final ActionDispatcher testDispatcher = TestDispatcher1(postInvoke: collect);

      await tester.pumpWidget(
        Actions(
          dispatcher: testDispatcher,
          actions: const <LocalKey, ActionFactory>{},
          child: Container(key: containerKey),
        ),
      );

      await tester.pump();
      final ActionDispatcher dispatcher = Actions.of(
259 260
        containerKey.currentContext,
        nullOk: true,
261 262 263
      );
      expect(dispatcher, equals(testDispatcher));
    });
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    testWidgets('FocusableActionDetector keeps track of focus and hover even when disabled.', (WidgetTester tester) async {
      FocusManager.instance.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
      final GlobalKey containerKey = GlobalKey();
      bool invoked = false;
      const Intent intent = Intent(TestAction.key);
      final FocusNode focusNode = FocusNode(debugLabel: 'Test Node');
      final Action testAction = TestAction(
        onInvoke: (FocusNode node, Intent invocation) {
          invoked = true;
        },
      );
      bool hovering = false;
      bool focusing = false;

      Future<void> buildTest(bool enabled) async {
        await tester.pumpWidget(
          Center(
            child: Actions(
              dispatcher: TestDispatcher1(postInvoke: collect),
              actions: const <LocalKey, ActionFactory>{},
              child: FocusableActionDetector(
                enabled: enabled,
                focusNode: focusNode,
                shortcuts: <LogicalKeySet, Intent>{
                  LogicalKeySet(LogicalKeyboardKey.enter): intent,
                },
                actions: <LocalKey, ActionFactory>{
                  TestAction.key: () => testAction,
                },
                onShowHoverHighlight: (bool value) => hovering = value,
                onShowFocusHighlight: (bool value) => focusing = value,
                child: Container(width: 100, height: 100, key: containerKey),
              ),
            ),
          ),
        );
        return tester.pump();
      }
      await buildTest(true);
      focusNode.requestFocus();
      await tester.pump();
      final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
      addTearDown(gesture.removePointer);
      await gesture.moveTo(tester.getCenter(find.byKey(containerKey)));
      await tester.pump();
      await tester.sendKeyEvent(LogicalKeyboardKey.enter);
      expect(hovering, isTrue);
      expect(focusing, isTrue);
      expect(invoked, isTrue);

      invoked = false;
      await buildTest(false);
      expect(hovering, isFalse);
      expect(focusing, isFalse);
      await tester.sendKeyEvent(LogicalKeyboardKey.enter);
      await tester.pump();
      expect(invoked, isFalse);
      await buildTest(true);
      expect(focusing, isFalse);
      expect(hovering, isTrue);
      await buildTest(false);
      expect(focusing, isFalse);
      expect(hovering, isFalse);
      await gesture.moveTo(Offset.zero);
      await buildTest(true);
      expect(hovering, isFalse);
      expect(focusing, isFalse);
    });
332 333
  });
  group('Diagnostics', () {
334
    testWidgets('default Intent debugFillProperties', (WidgetTester tester) async {
335 336 337 338 339
      final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

      const Intent(ValueKey<String>('foo')).debugFillProperties(builder);

      final List<String> description = builder.properties
340 341 342 343 344
        .where((DiagnosticsNode node) {
          return !node.isFiltered(DiagnosticLevel.info);
        })
        .map((DiagnosticsNode node) => node.toString())
        .toList();
345 346 347

      expect(description, equals(<String>['key: [<\'foo\'>]']));
    });
348
    testWidgets('CallbackAction debugFillProperties', (WidgetTester tester) async {
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
      final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

      CallbackAction(
        const ValueKey<String>('foo'),
        onInvoke: (FocusNode node, Intent intent) {},
      ).debugFillProperties(builder);

      final List<String> description = builder.properties
          .where((DiagnosticsNode node) {
            return !node.isFiltered(DiagnosticLevel.info);
          })
          .map((DiagnosticsNode node) => node.toString())
          .toList();

      expect(description, equals(<String>['intentKey: [<\'foo\'>]']));
    });
365
    testWidgets('default Actions debugFillProperties', (WidgetTester tester) async {
366 367
      final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

368 369 370 371 372
      Actions(
        actions: const <LocalKey, ActionFactory>{},
        dispatcher: const ActionDispatcher(),
        child: Container(),
      ).debugFillProperties(builder);
373 374

      final List<String> description = builder.properties
375 376 377 378 379
        .where((DiagnosticsNode node) {
          return !node.isFiltered(DiagnosticLevel.info);
        })
        .map((DiagnosticsNode node) => node.toString())
        .toList();
380 381 382 383

      expect(description[0], equalsIgnoringHashCodes('dispatcher: ActionDispatcher#00000'));
      expect(description[1], equals('actions: {}'));
    });
384
    testWidgets('Actions implements debugFillProperties', (WidgetTester tester) async {
385 386 387 388
      final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

      Actions(
        key: const ValueKey<String>('foo'),
389
        dispatcher: const ActionDispatcher(),
390 391 392 393 394 395 396
        actions: <LocalKey, ActionFactory>{
          const ValueKey<String>('bar'): () => TestAction(onInvoke: (FocusNode node, Intent intent) {}),
        },
        child: Container(key: const ValueKey<String>('baz')),
      ).debugFillProperties(builder);

      final List<String> description = builder.properties
397 398 399 400 401
          .where((DiagnosticsNode node) {
            return !node.isFiltered(DiagnosticLevel.info);
          })
          .map((DiagnosticsNode node) => node.toString())
          .toList();
402 403 404

      expect(description[0], equalsIgnoringHashCodes('dispatcher: ActionDispatcher#00000'));
      expect(description[1], equals('actions: {[<\'bar\'>]: Closure: () => TestAction}'));
405
    }, skip: isBrowser);
406 407
  });
}