Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests done
  • Loading branch information
LouiseHsu committed Aug 27, 2024
commit bdbdc1ba75ee2bf0e455b4fe3e59148f28d01c1c
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,17 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
.map((SK2Product productWrapper) =>
AppStoreProduct2Details.fromSK2Product(productWrapper))
.toList();
final ProductDetailsResponse response = ProductDetailsResponse(
productDetails: productDetails,
notFoundIDs: invalidProductIdentifiers.toList());
final ProductDetailsResponse response = ProductDetailsResponse(
productDetails: productDetails,
notFoundIDs: invalidProductIdentifiers.toList(),
error: exception == null
? null
: IAPError(
source: kIAPSource,
code: exception.code,
message: exception.message ?? '',
details: exception.details),
);
return response;
}
final SKRequestMaker requestMaker = SKRequestMaker();
Expand Down Expand Up @@ -198,6 +206,8 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
/// Use countryCode instead.
@Deprecated('Use countryCode')
Future<String?> getCountryCode() => countryCode();


}

enum _TransactionRestoreState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ extension on SK2ProductTypeMessage {
}
}

extension on SK2ProductType {
SK2ProductTypeMessage convertToPigeon() {
switch (this) {
case SK2ProductType.autoRenewable:
return SK2ProductTypeMessage.autoRenewable;
case SK2ProductType.consumable:
return SK2ProductTypeMessage.consumable;
case SK2ProductType.nonConsumable:
return SK2ProductTypeMessage.nonConsumable;
case SK2ProductType.nonRenewable:
return SK2ProductTypeMessage.nonRenewable;
}
}
}

enum SK2SubscriptionOfferType { introductory, promotional }

extension on SK2SubscriptionOfferTypeMessage {
Expand Down Expand Up @@ -196,6 +211,10 @@ class SK2PriceLocale {
SK2PriceLocale({required this.currencyCode, required this.currencySymbol});
final String currencyCode;
final String currencySymbol;

SK2PriceLocaleMessage convertToPigeon() {
return SK2PriceLocaleMessage(currencyCode: currencyCode, currencySymbol: currencySymbol);
}
}

extension on SK2PriceLocaleMessage {
Expand Down Expand Up @@ -265,6 +284,17 @@ class SK2Product {
.map((SK2ProductMessage product) => product.convertFromPigeon())
.toList();
}

SK2ProductMessage convertToPigeon() {
return SK2ProductMessage(
id: id,
displayName: displayName,
description: description,
price: price,
displayPrice: displayPrice,
type: type.convertToPigeon(),
priceLocale: priceLocale.convertToPigeon());
}
}

// let me test what i wanted you to type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ class FakeStoreKit2Platform implements TestInAppPurchase2Api {

late Set<String> validProductIDs;
late Map<String, SK2Product> validProducts;
PlatformException? queryProductException;

void reset() {
validProductIDs = <String>{'123', '456'};
validProducts = <String, SK2Product>{};
for (final String validID in validProductIDs) {
final SK2Product product = SK2Product(
id: validID,
displayName: "test_product",
displayPrice: "0.99",
description: "description",
price: 0.99,
type: SK2ProductType.consumable,
priceLocale: SK2PriceLocale(
currencyCode: 'USD',
currencySymbol: r'$'));
validProducts[validID] = product;
}
}

@override
bool canMakePayments() {
Expand All @@ -290,16 +309,22 @@ class FakeStoreKit2Platform implements TestInAppPurchase2Api {

@override
Future<List<SK2ProductMessage?>> products(List<String?> identifiers) {
if (queryProductException != null) {
throw queryProductException!;
}
final List<String?> productIDS = identifiers;
final List<String> invalidFound = <String>[];
final List<SKProductWrapper> products = <SKProductWrapper>[];
final List<SK2Product> products = <SK2Product>[];
for (final String? productID in productIDS) {
if (!validProductIDs.contains(productID)) {
invalidFound.add(productID!);
} else {
if (validProductIDs.contains(productID)) {
products.add(validProducts[productID]!);
}
}
final List<SK2ProductMessage?> result = <SK2ProductMessage?>[];
for (SK2Product p in products) {
result.add(p.convertToPigeon());
}

return Future<List<SK2ProductMessage?>>.value(result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2013 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/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_interface.dart';
import 'package:in_app_purchase_storekit/in_app_purchase_storekit.dart';

import 'fakes/fake_storekit_platform.dart';
import 'sk2_test_api.g.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

final FakeStoreKit2Platform fakeStoreKit2Platform = FakeStoreKit2Platform();
late InAppPurchaseStoreKitPlatform iapStoreKitPlatform;

setUpAll(() {
TestInAppPurchase2Api.setup(fakeStoreKit2Platform);
});

setUp(() {
InAppPurchaseStoreKitPlatform.registerPlatform();
iapStoreKitPlatform =
InAppPurchasePlatform.instance as InAppPurchaseStoreKitPlatform;
fakeStoreKit2Platform.reset();
});

tearDown(() => fakeStoreKit2Platform.reset());

group('isAvailable', () {
test('true', () async {
expect(await iapStoreKitPlatform.isAvailable(), isTrue);
});
});

group('query product list', () {
test('should get product list and correct invalid identifiers', () async {
final InAppPurchaseStoreKitPlatform connection =
InAppPurchaseStoreKitPlatform();
final ProductDetailsResponse response =
await connection.queryProductDetails(<String>{'123', '456', '789'});
final List<ProductDetails> products = response.productDetails;
expect(products.first.id, '123');
expect(products[1].id, '456');
expect(response.notFoundIDs, <String>['789']);
expect(response.error, isNull);
expect(response.productDetails.first.currencySymbol, r'$');
expect(response.productDetails[1].currencySymbol, r'$');
});
test(
'if query products throws error, should get error object in the response',
() async {
fakeStoreKit2Platform.queryProductException = PlatformException(
code: 'error_code',
message: 'error_message',
details: <Object, Object>{'info': 'error_info'});
final InAppPurchaseStoreKitPlatform connection =
InAppPurchaseStoreKitPlatform();
final ProductDetailsResponse response =
await connection.queryProductDetails(<String>{'123', '456', '789'});
expect(response.productDetails, <ProductDetails>[]);
expect(response.notFoundIDs, <String>['123', '456', '789']);
expect(response.error, isNotNull);
expect(response.error!.source, kIAPSource);
expect(response.error!.code, 'error_code');
expect(response.error!.message, 'error_message');
expect(response.error!.details, <Object, Object>{'info': 'error_info'});
});
});}