simple_semantics_test.dart 2.41 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/rendering.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9 10 11

import 'rendering_tester.dart';


void main() {
12 13
  TestRenderingFlutterBinding.ensureInitialized();

14
  test('only send semantics update if semantics have changed', () {
15
    final TestRender testRender = TestRender()
16
      ..properties = const SemanticsProperties(label: 'hello')
17 18
      ..textDirection = TextDirection.ltr;

19
    final RenderConstrainedBox tree = RenderConstrainedBox(
20 21 22 23
      additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0),
      child: testRender,
    );
    int semanticsUpdateCount = 0;
24
    final SemanticsHandle semanticsHandle = TestRenderingFlutterBinding.instance.pipelineOwner.ensureSemantics(
25 26 27
      listener: () {
        ++semanticsUpdateCount;
      },
28 29 30 31 32 33
    );

    layout(tree, phase: EnginePhase.flushSemantics);

    // Initial render does semantics.
    expect(semanticsUpdateCount, 1);
34
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
35 36 37 38 39 40 41 42 43 44

    testRender.describeSemanticsConfigurationCallCount = 0;
    semanticsUpdateCount = 0;

    // Request semantics update even though nothing changed.
    testRender.markNeedsSemanticsUpdate();
    pumpFrame(phase: EnginePhase.flushSemantics);

    // Object is asked for semantics, but no update is sent.
    expect(semanticsUpdateCount, 0);
45
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
46 47 48 49 50

    testRender.describeSemanticsConfigurationCallCount = 0;
    semanticsUpdateCount = 0;

    // Change semantics and request update.
51
    testRender.properties = const SemanticsProperties(label: 'bye');
52 53 54 55 56
    testRender.markNeedsSemanticsUpdate();
    pumpFrame(phase: EnginePhase.flushSemantics);

    // Object is asked for semantics, and update is sent.
    expect(semanticsUpdateCount, 1);
57
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
58 59 60 61 62 63

    semanticsHandle.dispose();
  });
}

class TestRender extends RenderSemanticsAnnotations {
64 65
  TestRender() : super(properties: const SemanticsProperties());

66 67 68 69 70 71 72 73
  int describeSemanticsConfigurationCallCount = 0;

  @override
  void describeSemanticsConfiguration(SemanticsConfiguration config) {
    super.describeSemanticsConfiguration(config);
    describeSemanticsConfigurationCallCount += 1;
  }
}