_network_image_test_web.dart 2.79 KB
Newer Older
1 2 3 4 5 6 7
// 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.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/painting/_network_image_web.dart';
8
import 'package:flutter/src/services/dom.dart';
9 10 11
import 'package:flutter_test/flutter_test.dart';

import '../image_data.dart';
12
import '_test_http_request.dart';
13 14 15 16 17 18 19 20 21 22

void runTests() {
  tearDown(() {
    debugRestoreHttpRequestFactory();
  });

  testWidgets('loads an image from the network with headers',
      (WidgetTester tester) async {
    final TestHttpRequest testHttpRequest = TestHttpRequest()
      ..status = 200
23
      ..mockEvent = MockEvent('load', createDomEvent('Event', 'test error'))
24 25 26
      ..response = (Uint8List.fromList(kTransparentImage)).buffer;

    httpRequestFactory = () {
27
      return testHttpRequest.getMock();
28 29 30 31
    };

    const Map<String, String> headers = <String, String>{
      'flutter': 'flutter',
32
      'second': 'second',
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    };

    final Image image = Image.network(
      'https://www.example.com/images/frame.png',
      headers: headers,
    );

    await tester.pumpWidget(image);

    assert(mapEquals(testHttpRequest.responseHeaders, headers), true);
  });

  testWidgets('loads an image from the network with unsuccessful HTTP code',
      (WidgetTester tester) async {
    final TestHttpRequest testHttpRequest = TestHttpRequest()
      ..status = 404
49 50
      ..mockEvent = MockEvent('error', createDomEvent('Event', 'test error'));

51 52

    httpRequestFactory = () {
53
      return testHttpRequest.getMock();
54 55 56 57
    };

    const Map<String, String> headers = <String, String>{
      'flutter': 'flutter',
58
      'second': 'second',
59 60 61 62 63 64 65 66
    };

    final Image image = Image.network(
      'https://www.example.com/images/frame2.png',
      headers: headers,
    );

    await tester.pumpWidget(image);
67
    expect((tester.takeException() as DomProgressEvent).type, 'test error');
68 69 70 71 72 73
  });

  testWidgets('loads an image from the network with empty response',
      (WidgetTester tester) async {
    final TestHttpRequest testHttpRequest = TestHttpRequest()
      ..status = 200
74
      ..mockEvent = MockEvent('load', createDomEvent('Event', 'test error'))
75 76 77
      ..response = (Uint8List.fromList(<int>[])).buffer;

    httpRequestFactory = () {
78
      return testHttpRequest.getMock();
79 80 81 82
    };

    const Map<String, String> headers = <String, String>{
      'flutter': 'flutter',
83
      'second': 'second',
84 85 86 87 88 89 90 91 92 93 94 95
    };

    final Image image = Image.network(
      'https://www.example.com/images/frame3.png',
      headers: headers,
    );

    await tester.pumpWidget(image);
    expect(tester.takeException().toString(),
        'HTTP request failed, statusCode: 200, https://www.example.com/images/frame3.png');
  });
}