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

5 6
// @dart = 2.8

7 8
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
9
import '../flutter_test_alternative.dart';
10 11 12 13 14 15

import 'rendering_tester.dart';


void main() {
  test('only send semantics update if semantics have changed', () {
16
    final TestRender testRender = TestRender()
17 18 19
      ..label = 'hello'
      ..textDirection = TextDirection.ltr;

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

    layout(tree, phase: EnginePhase.flushSemantics);

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

    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);
46
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
47 48 49 50 51 52 53 54 55 56 57

    testRender.describeSemanticsConfigurationCallCount = 0;
    semanticsUpdateCount = 0;

    // Change semantics and request update.
    testRender.label = 'bye';
    testRender.markNeedsSemanticsUpdate();
    pumpFrame(phase: EnginePhase.flushSemantics);

    // Object is asked for semantics, and update is sent.
    expect(semanticsUpdateCount, 1);
58
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
59 60 61 62 63 64 65 66 67 68 69 70 71 72

    semanticsHandle.dispose();
  });
}

class TestRender extends RenderSemanticsAnnotations {
  int describeSemanticsConfigurationCallCount = 0;

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