Skip to content

Commit

Permalink
update test package too
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield committed Feb 20, 2024
1 parent c1ea0d4 commit 24a94c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 79 deletions.
9 changes: 6 additions & 3 deletions packages/flutter_svg_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ name: flutter_svg_test
description: A testing library which makes it easy to test flutter svg . Built to be used with the flutter svg package.
repository: https://github.com/dnfield/flutter_svg/tree/master/packages/flutter_svg_test
issue_tracker: https://github.com/dnfield/flutter_svg/issues
version: 1.0.0
version: 1.0.1

dependencies:
flutter:
sdk: flutter
flutter_svg: ^2.0.4
flutter_svg: ^2.0.10
flutter_test:
sdk: flutter

dev_dependencies:
http: ^1.0.0

environment:
sdk: '>=2.19.0-0 <4.0.0'
flutter: ">=3.7.0-0"


87 changes: 11 additions & 76 deletions packages/flutter_svg_test/test/flutter_svg_test_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
Expand All @@ -7,6 +6,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_svg_test/flutter_svg_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;

void main() {
group('finds', () {
Expand All @@ -24,12 +24,11 @@ void main() {
});

testWidgets('network svg', (WidgetTester widgetTester) async {
await HttpOverrides.runZoned(() async {
final SvgPicture asset = SvgPicture.network('svg.dart');
await widgetTester.pumpWidget(asset);
final http.Client fakeClient = _FakeHttpClient();
final SvgPicture asset = SvgPicture.network('svg.dart', httpClient: fakeClient);
await widgetTester.pumpWidget(asset);

expect(find.svg(asset.bytesLoader), findsOneWidget);
}, createHttpClient: (SecurityContext? c) => _setupFakeClient);
expect(find.svg(asset.bytesLoader), findsOneWidget);
});

testWidgets('string svg', (WidgetTester widgetTester) async {
Expand Down Expand Up @@ -68,12 +67,10 @@ void main() {
});

testWidgets('network svg with url', (WidgetTester widgetTester) async {
await HttpOverrides.runZoned(() async {
const String svgUri = 'svg.dart';
await widgetTester.pumpWidget(SvgPicture.network(svgUri));
const String svgUri = 'svg.dart';
await widgetTester.pumpWidget(SvgPicture.network(svgUri));

expect(find.svgNetworkWithUrl(svgUri), findsOneWidget);
}, createHttpClient: (SecurityContext? c) => _setupFakeClient);
expect(find.svgNetworkWithUrl(svgUri), findsOneWidget);
});

testWidgets('file svg wit path', (WidgetTester widgetTester) async {
Expand All @@ -93,13 +90,6 @@ void main() {
});
}

HttpClient get _setupFakeClient {
final _FakeHttpClientResponse fakeResponse = _FakeHttpClientResponse();
final _FakeHttpClientRequest fakeRequest =
_FakeHttpClientRequest(fakeResponse);
return _FakeHttpClient(fakeRequest);
}

class _FakeAssetBundle extends Fake implements AssetBundle {
@override
Future<String> loadString(String key, {bool cache = true}) async {
Expand All @@ -112,65 +102,10 @@ class _FakeAssetBundle extends Fake implements AssetBundle {
}
}

class _FakeHttpClient extends Fake implements HttpClient {
_FakeHttpClient(this.request);

_FakeHttpClientRequest request;

@override
Future<HttpClientRequest> getUrl(Uri url) async => request;
}

class _FakeHttpHeaders extends Fake implements HttpHeaders {
final Map<String, String?> values = <String, String?>{};

@override
void add(String name, Object value, {bool preserveHeaderCase = false}) {
values[name] = value.toString();
}

@override
List<String>? operator [](String key) {
return <String>[values[key]!];
}
}

class _FakeHttpClientRequest extends Fake implements HttpClientRequest {
_FakeHttpClientRequest(this.response);

_FakeHttpClientResponse response;

@override
final HttpHeaders headers = _FakeHttpHeaders();

@override
Future<HttpClientResponse> close() async => response;
}

class _FakeHttpClientResponse extends Fake implements HttpClientResponse {
@override
int statusCode = 200;

@override
int contentLength = _svgStr.length;

@override
HttpClientResponseCompressionState get compressionState =>
HttpClientResponseCompressionState.notCompressed;

class _FakeHttpClient extends Fake implements http.Client {
@override
StreamSubscription<List<int>> listen(
void Function(List<int> event)? onData, {
Function? onError,
void Function()? onDone,
bool? cancelOnError,
}) {
return Stream<Uint8List>.fromIterable(<Uint8List>[_svgBytes]).listen(
onData,
onDone: onDone,
onError: onError,
cancelOnError: cancelOnError,
);
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
return http.Response(_svgStr, 200);
}
}

Expand Down

0 comments on commit 24a94c3

Please sign in to comment.