custom_semantics_test.dart 3.48 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 The Chromium 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 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
7 8
import 'package:sample_catalog/custom_semantics.dart' as custom_semantics show main;
import 'package:sample_catalog/custom_semantics.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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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

void main() {
  testWidgets('custom_semantics sample smoke test', (WidgetTester tester) async {
    // Turn on Semantics
    final SemanticsHandle semanticsHandler = tester.binding.pipelineOwner.ensureSemantics();
    final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner;

    // Build the sample app
    custom_semantics.main();
    await tester.pump();

    // Verify it correctly exposes its semantics.
    // TODO(goderbauer): Use `SemanticsTester` after https://github.com/flutter/flutter/issues/12286.
    final SemanticsNode semantics = tester
        .renderObject(find.byType(AdjustableDropdownListTile))
        .debugSemantics;

    expectAdjustable(semantics,
      hasIncreaseAction: true,
      hasDecreaseAction: true,
      label: 'Timeout',
      decreasedValue: '5 seconds',
      value: '15 seconds',
      increasedValue: '30 seconds',
    );

    // Increase
    semanticsOwner.performAction(semantics.id, SemanticsAction.increase);
    await tester.pump();

    expectAdjustable(semantics,
      hasIncreaseAction: true,
      hasDecreaseAction: true,
      label: 'Timeout',
      decreasedValue: '15 seconds',
      value: '30 seconds',
      increasedValue: '1 minute',
    );

    // Increase all the way to highest value
    semanticsOwner.performAction(semantics.id, SemanticsAction.increase);
    await tester.pump();

    expectAdjustable(semantics,
      hasIncreaseAction: false,
      hasDecreaseAction: true,
      label: 'Timeout',
      decreasedValue: '30 seconds',
      value: '1 minute',
    );

    // Decrease
    semanticsOwner.performAction(semantics.id, SemanticsAction.decrease);
    await tester.pump();

    expectAdjustable(semantics,
      hasIncreaseAction: true,
      hasDecreaseAction: true,
      label: 'Timeout',
      decreasedValue: '15 seconds',
      value: '30 seconds',
      increasedValue: '1 minute',
    );

    // Decrease all the way to lowest value
    semanticsOwner.performAction(semantics.id, SemanticsAction.decrease);
    await tester.pump();
    semanticsOwner.performAction(semantics.id, SemanticsAction.decrease);
    await tester.pump();
    semanticsOwner.performAction(semantics.id, SemanticsAction.decrease);
    await tester.pump();

    expectAdjustable(semantics,
      hasIncreaseAction: true,
      hasDecreaseAction: false,
      label: 'Timeout',
      value: '1 second',
      increasedValue: '5 seconds',
    );

    // Clean-up
    semanticsHandler.dispose();
  });
}

94 95
void expectAdjustable(
  SemanticsNode node, {
96 97 98 99 100 101
  bool hasIncreaseAction = true,
  bool hasDecreaseAction = true,
  String label = '',
  String decreasedValue = '',
  String value = '',
  String increasedValue = '',
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}) {
  final SemanticsData semanticsData = node.getSemanticsData();

  int actions = 0;
  if (hasIncreaseAction)
    actions |= SemanticsAction.increase.index;
  if (hasDecreaseAction)
    actions |= SemanticsAction.decrease.index;

  expect(semanticsData.actions, actions);
  expect(semanticsData.label, label);
  expect(semanticsData.decreasedValue, decreasedValue);
  expect(semanticsData.value, value);
  expect(semanticsData.increasedValue, increasedValue);
}