screenshot_test.dart 1.86 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Ian Hickson's avatar
Ian Hickson committed
5 6 7 8 9 10
// This test is used by devicelab, test "integration_ui_ios_screenshot".
// Its use of package:image is, at the time of writing, the only use of that
// package in this repository. If package:image is a problem, it is probably
// fine to just remove this test since the value of the test is probably not
// as much as the cost of the dependency.

11 12 13 14 15 16 17
import 'package:flutter_driver/flutter_driver.dart';
import 'package:image/image.dart';

import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;

void main() {
  group('FlutterDriver', () {
18
    late FlutterDriver driver;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    tearDownAll(() async {
      await driver.close();
    });

    test('should take screenshot', () async {
      final SerializableFinder toggleBtn = find.byValueKey('toggle');
      // Cards use a magic background color that we look for in the screenshots.
      final Matcher cardsAreVisible = contains(getColor(0xff, 0x01, 0x02));
      await driver.waitFor(toggleBtn);

      bool cardsShouldBeVisible = false;
35
      Image? imageBefore = decodePng(await driver.screenshot());
36 37 38
      for (int i = 0; i < 10; i += 1) {
        await driver.tap(toggleBtn);
        cardsShouldBeVisible = !cardsShouldBeVisible;
39
        final Image? imageAfter = decodePng(await driver.screenshot());
40 41

        if (cardsShouldBeVisible) {
42 43
          expect(imageBefore?.data, isNot(cardsAreVisible));
          expect(imageAfter?.data, cardsAreVisible);
44
        } else {
45 46
          expect(imageBefore?.data, cardsAreVisible);
          expect(imageAfter?.data, isNot(cardsAreVisible));
47 48 49 50 51 52 53
        }

        imageBefore = imageAfter;
      }
    }, timeout: const Timeout(Duration(minutes: 2)));
  });
}