moved platform channel logic to app mock

This commit is contained in:
Vadim 2024-04-11 16:16:19 +02:00
parent 21a3c7722c
commit 47cb82c0d3
2 changed files with 37 additions and 23 deletions

View file

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:light_sensor/light_sensor.dart';
import 'package:lightmeter/application_wrapper.dart'; import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/environment.dart'; import 'package:lightmeter/environment.dart';
@ -9,6 +11,7 @@ import 'package:lightmeter/res/theme.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import '../integration_test/mocks/paid_features_mock.dart'; import '../integration_test/mocks/paid_features_mock.dart';
import '../integration_test/utils/platform_channel_mock.dart';
/// Provides [MaterialApp] with default theme and "en" localization /// Provides [MaterialApp] with default theme and "en" localization
class WidgetTestApplicationMock extends StatelessWidget { class WidgetTestApplicationMock extends StatelessWidget {
@ -40,7 +43,7 @@ class WidgetTestApplicationMock extends StatelessWidget {
} }
} }
class GoldenTestApplicationMock extends StatelessWidget { class GoldenTestApplicationMock extends StatefulWidget {
final IAPProductStatus productStatus; final IAPProductStatus productStatus;
final Widget child; final Widget child;
@ -50,13 +53,44 @@ class GoldenTestApplicationMock extends StatelessWidget {
super.key, super.key,
}); });
@override
State<GoldenTestApplicationMock> createState() => _GoldenTestApplicationMockState();
}
class _GoldenTestApplicationMockState extends State<GoldenTestApplicationMock> {
@override
void initState() {
super.initState();
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
LightSensor.methodChannel,
(methodCall) async {
switch (methodCall.method) {
case "sensor":
return true;
default:
return null;
}
},
);
setupLightSensorStreamHandler();
}
@override
void dispose() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
LightSensor.methodChannel,
null,
);
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return IAPProducts( return IAPProducts(
products: [ products: [
IAPProduct( IAPProduct(
storeId: IAPProductType.paidFeatures.storeId, storeId: IAPProductType.paidFeatures.storeId,
status: productStatus, status: widget.productStatus,
), ),
], ],
child: ApplicationWrapper( child: ApplicationWrapper(
@ -82,7 +116,7 @@ class GoldenTestApplicationMock extends StatelessWidget {
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
child: child!, child: child!,
), ),
home: child, home: widget.child,
); );
}, },
), ),

View file

@ -3,7 +3,6 @@ import 'dart:convert';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart'; import 'package:golden_toolkit/golden_toolkit.dart';
import 'package:light_sensor/light_sensor.dart';
import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/data/models/metering_screen_layout_config.dart';
import 'package:lightmeter/data/models/theme_type.dart'; import 'package:lightmeter/data/models/theme_type.dart';
@ -98,25 +97,6 @@ void main() {
}.toJson(), }.toJson(),
), ),
}); });
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
LightSensor.methodChannel,
(methodCall) async {
switch (methodCall.method) {
case "sensor":
return true;
default:
return null;
}
},
);
setupLightSensorStreamHandler();
});
tearDownAll(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
LightSensor.methodChannel,
null,
);
}); });
testGoldens( testGoldens(