Files
iron-arc/app/test/billing_test.dart
SoftwareApp a26e0b93b5 m7c: Play Billing and a complete deletion path
- billing module with a testable seam: models (product ids, purchase events),
  StoreClient + PlayStoreClient over in_app_purchase, Entitlements persisted in
  SharedPreferences, BillingService orchestrating query/buy/restore/grant.
- rules encoded: grant before completePurchase (Play refunds unacknowledged
  purchases after three days), pending purchases are shown but never granted,
  restorePurchases on start so a reinstall keeps the purchase, and Remove Ads
  only hides interstitials - rewarded video stays available.
- ads gate: AdsService.adsRemoved stops both the counter and the preload, so an
  owner never requests an interstitial.
- settings sheet: shop section (price from the store, never hardcoded),
  "restore purchases", and "delete local data" with confirmation; the existing
  mail-based deletion request stays the account-deletion path since the app has
  no accounts.
- 9 new tests with a fake store cover purchase, restore, pending, cancel,
  error, restart persistence, missing store and a store that throws. 17 total.
- verified on device: the app talks to the real Play Billing service
  (IInAppBillingService in logcat) and degrades to "no products created"
  without a crash; com.android.vending.BILLING is in the merged manifest.
- docs: monetization 7c (rules + verification), decision D6 (local entitlement
  until purchases affect the server), compliance rows for billing/restore/
  deletion, blocked.md 3c (create remove_ads in the Console).
- release 0.9.0 (13).
2026-10-25 04:06:44 +01:00

228 lines
6.7 KiB
Dart

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:iron_arc/src/billing/billing_service.dart';
import 'package:iron_arc/src/billing/entitlements.dart';
import 'package:iron_arc/src/billing/models.dart';
import 'package:iron_arc/src/billing/store_client.dart';
import 'package:iron_arc/src/ui/links.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// Stand-in for Google Play: the module is testable before the Console has any
/// products, which is the only way to verify the grant/complete logic here.
class FakeStore implements StoreClient {
FakeStore({
this.available = true,
List<StoreProduct>? products,
this.purchaseStarts = true,
this.throwOnQuery = false,
}) : products = products ?? _defaultProducts;
static const List<StoreProduct> _defaultProducts = [
StoreProduct(
id: ProductIds.removeAds, title: 'Werbung entfernen', price: '4,99 €'),
];
final bool available;
final bool purchaseStarts;
final bool throwOnQuery;
final List<StoreProduct> products;
final StreamController<PurchaseEvent> _events =
StreamController<PurchaseEvent>.broadcast();
int completedCalls = 0;
int restoreCalls = 0;
int buyCalls = 0;
bool disposed = false;
@override
Stream<PurchaseEvent> get events => _events.stream;
void emit(PurchaseEvent event) => _events.add(event);
@override
Future<bool> isAvailable() async => available;
@override
Future<List<StoreProduct>> queryProducts(Set<String> ids) async {
if (throwOnQuery) throw StateError('store down');
return products.where((p) => ids.contains(p.id)).toList();
}
@override
Future<bool> buy(StoreProduct product) async {
buyCalls++;
return purchaseStarts;
}
/// Mirrors Play: acknowledging is only required (and only counted) when the
/// platform flags `pendingCompletePurchase`.
@override
Future<void> complete(PurchaseEvent event) async {
if (event.complete) completedCalls++;
}
@override
Future<void> restore() async => restoreCalls++;
@override
Future<void> dispose() async {
disposed = true;
await _events.close();
}
}
Future<BillingService> _service(FakeStore store) async {
final entitlements = await Entitlements.load();
final billing = BillingService(entitlements: entitlements, client: store);
await billing.init();
return billing;
}
void main() {
setUp(() => SharedPreferences.setMockInitialValues(<String, Object>{}));
test('query on start exposes the catalogue and restores silently', () async {
final store = FakeStore();
final billing = await _service(store);
expect(billing.available, isTrue);
expect(billing.canPurchase, isTrue);
expect(billing.product(ProductIds.removeAds)?.price, '4,99 €');
// Restoring on start is the documented rule (monetization.md §3).
expect(store.restoreCalls, 1);
billing.dispose();
await store.dispose();
});
test('purchase grants the entitlement and is completed', () async {
final store = FakeStore();
final billing = await _service(store);
expect(await billing.buy(ProductIds.removeAds), isTrue);
expect(billing.adsRemoved, isFalse,
reason: 'nothing is granted before the store answers');
store.emit(const PurchaseEvent(
productId: ProductIds.removeAds,
state: PurchaseState.purchased,
complete: true,
));
await pumpEventQueue();
expect(billing.entitlements.adsRemoved, isTrue);
expect(store.completedCalls, 1,
reason: 'Play refunds purchases that are not acknowledged');
billing.dispose();
await store.dispose();
});
test('pending purchases are reported but never granted', () async {
final store = FakeStore();
final billing = await _service(store);
store.emit(const PurchaseEvent(
productId: ProductIds.removeAds,
state: PurchaseState.pending,
complete: true,
));
await pumpEventQueue();
expect(billing.entitlements.adsRemoved, isFalse);
expect(store.completedCalls, 0);
expect(billing.error, contains('pending'));
billing.dispose();
await store.dispose();
});
test('cancel and error leave the entitlement untouched', () async {
final store = FakeStore();
final billing = await _service(store);
store.emit(const PurchaseEvent(
productId: ProductIds.removeAds, state: PurchaseState.canceled));
await pumpEventQueue();
expect(billing.entitlements.adsRemoved, isFalse);
expect(billing.error, isNull);
store.emit(const PurchaseEvent(
productId: ProductIds.removeAds,
state: PurchaseState.error,
error: 'ITEM_ALREADY_OWNED'));
await pumpEventQueue();
expect(billing.entitlements.adsRemoved, isFalse);
expect(billing.error, 'ITEM_ALREADY_OWNED');
billing.dispose();
await store.dispose();
});
test('restored purchases grant too (reinstall path)', () async {
final store = FakeStore();
final billing = await _service(store);
store.emit(const PurchaseEvent(
productId: ProductIds.removeAds,
state: PurchaseState.restored,
complete: false,
));
await pumpEventQueue();
expect(billing.adsRemoved, isTrue);
expect(store.completedCalls, 0, reason: 'restores need no acknowledgement');
billing.dispose();
await store.dispose();
});
test('entitlement survives a restart', () async {
final first = await _service(FakeStore());
await first.entitlements.grant(ProductIds.removeAds);
expect(first.entitlements.adsRemoved, isTrue);
first.dispose();
final second = await Entitlements.load();
expect(second.adsRemoved, isTrue, reason: 'written through to disk');
await second.clearAll();
expect(second.adsRemoved, isFalse);
});
test('missing store: no crash, clear error, purchase disabled', () async {
final store = FakeStore(available: false);
final billing = await _service(store);
expect(billing.available, isFalse);
expect(billing.canPurchase, isFalse);
expect(await billing.buy(ProductIds.removeAds), isFalse);
expect(billing.error, isNotNull);
billing.dispose();
await store.dispose();
});
test('store that throws while querying is handled', () async {
final store = FakeStore(throwOnQuery: true);
final billing = await _service(store);
expect(billing.canPurchase, isFalse);
expect(billing.error, 'Produkte konnten nicht geladen werden');
billing.dispose();
await store.dispose();
});
test('deletion request carries the device id and the contact address', () {
final uri = deletionRequestUri('dev-abc123');
expect(uri.scheme, 'mailto');
expect(uri.path, kContactEmail);
expect(Uri.decodeComponent(uri.query), contains('dev-abc123'));
expect(Uri.decodeComponent(uri.query), contains('Datenlöschung'));
});
}