Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pay/lib/src/widgets/pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ abstract class PayButton extends StatefulWidget {
/// button loads. If the payment provider is available for a given user, the
/// [_payButton] is added to the tree. Otherwise, if set, the replacement widget
/// in [childOnError] is shown.
class _PayButtonState extends State<PayButton> {
late final Future<bool> _userCanPayFuture;
class _PayButtonState extends State<PayButton> with WidgetsBindingObserver {
late Future<bool> _userCanPayFuture;

/// A method to initialize payment result streams
///
Expand All @@ -146,6 +146,7 @@ class _PayButtonState extends State<PayButton> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
if (!widget._isPlatformSupported) return;

_userCanPayFuture = _userCanPay();
Expand All @@ -155,6 +156,23 @@ class _PayButtonState extends State<PayButton> {
}
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
if (widget._isPlatformSupported) {
setState(() {
_userCanPayFuture = _userCanPay();
});
}
}
}

@override
Widget build(BuildContext context) {
if (!widget._isPlatformSupported) {
Expand Down
77 changes: 77 additions & 0 deletions pay/test/pay_button_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pay/pay.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

const channel = MethodChannel('plugins.flutter.io/pay');
const providerGooglePay = PayProvider.google_pay;
const payConfigString =
'{"provider": "google_pay", "data": {}}';
final paymentConfiguration =
PaymentConfiguration.fromJsonString(payConfigString);

testWidgets('PayButton re-evaluates readiness when app is resumed',
(WidgetTester tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;
final log = <MethodCall>[];
var canPayResponse = true;

TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
log.add(methodCall);
if (methodCall.method == 'userCanPay') {
return canPayResponse;
}
return null;
});

// Pump the GooglePayButton widget.
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: GooglePayButton(
paymentConfiguration: paymentConfiguration,
paymentItems: const [],
onPaymentResult: (_) {},
),
),
),
);

// Initial build/initState should call userCanPay.
await tester.pumpAndSettle();
expect(log.length, 1);
expect(log.first.method, 'userCanPay');

// Simulate resuming the app.
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();

// Verify that userCanPay was called a second time.
expect(log.length, 2);
expect(log[1].method, 'userCanPay');

// Clean up.
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
debugDefaultTargetPlatformOverride = null;
});
}