simple_semantics_test.dart 2.23 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/material.dart';
import 'package:flutter/rendering.dart';
7
import '../flutter_test_alternative.dart';
8 9 10 11 12 13

import 'rendering_tester.dart';


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

18
    final RenderObject tree = RenderConstrainedBox(
19 20 21 22 23 24 25 26 27 28 29 30 31 32
      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);
33
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
34 35 36 37 38 39 40 41 42 43

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

    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);
56
    expect(testRender.describeSemanticsConfigurationCallCount, isPositive);
57 58 59 60 61 62 63 64 65 66 67 68 69 70

    semanticsHandle.dispose();
  });
}

class TestRender extends RenderSemanticsAnnotations {
  int describeSemanticsConfigurationCallCount = 0;

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