Skip to content

Commit e0957ce

Browse files
author
Robot Media
committed
Differentiate between purchases and transactions. Closes robotmedia#11
1 parent 21c9c17 commit e0957ce

File tree

3 files changed

+97
-34
lines changed

3 files changed

+97
-34
lines changed

AndroidBillingLibraryTest/src/net/robotmedia/billing/model/BillingDBTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class BillingDBTest extends AndroidTestCase {
88

99
private BillingDB mData;
1010

11-
public static void assertEqualsFromDb(Purchase a, Purchase b) {
11+
public static void assertEqualsFromDb(Transaction a, Transaction b) {
1212
assertEquals(a.orderId, b.orderId);
1313
assertEquals(a.productId, b.productId);
1414
assertEquals(a.purchaseState, b.purchaseState);
@@ -25,41 +25,41 @@ protected void setUp() throws Exception {
2525
@Override
2626
protected void tearDown() throws Exception {
2727
super.tearDown();
28-
mData.mDb.delete(BillingDB.TABLE_PURCHASES, null, null);
28+
mData.mDb.delete(BillingDB.TABLE_TRANSACTIONS, null, null);
2929
mData.close();
3030
}
3131

3232
@SmallTest
3333
public void testInsert() throws Exception {
34-
mData.insert(PurchaseTest.PURCHASE_1);
35-
final Cursor cursor = mData.queryPurchases();
34+
mData.insert(TransactionTest.PURCHASE_1);
35+
final Cursor cursor = mData.queryTransactions();
3636
assertEquals(cursor.getCount(), 1);
3737
cursor.moveToNext();
38-
final Purchase stored = BillingDB.createPurchase(cursor);
39-
stored.packageName = PurchaseTest.PURCHASE_1.packageName; // Not stored in DB
40-
stored.notificationId = PurchaseTest.PURCHASE_1.notificationId; // Not stored in DB
41-
assertEqualsFromDb(PurchaseTest.PURCHASE_1, stored);
38+
final Transaction stored = BillingDB.createTransaction(cursor);
39+
stored.packageName = TransactionTest.PURCHASE_1.packageName; // Not stored in DB
40+
stored.notificationId = TransactionTest.PURCHASE_1.notificationId; // Not stored in DB
41+
assertEqualsFromDb(TransactionTest.PURCHASE_1, stored);
4242
}
4343

4444
@SmallTest
4545
public void testUnique() throws Exception {
46-
mData.insert(PurchaseTest.PURCHASE_1);
47-
mData.insert(PurchaseTest.PURCHASE_1);
48-
final Cursor cursor = mData.queryPurchases();
46+
mData.insert(TransactionTest.PURCHASE_1);
47+
mData.insert(TransactionTest.PURCHASE_1);
48+
final Cursor cursor = mData.queryTransactions();
4949
assertEquals(cursor.getCount(), 1);
5050
}
5151

5252
@SmallTest
5353
public void testQueryPurchases() throws Exception {
54-
mData.insert(PurchaseTest.PURCHASE_1);
55-
mData.insert(PurchaseTest.PURCHASE_2);
56-
final Cursor cursor = mData.queryPurchases(PurchaseTest.PURCHASE_1.productId, PurchaseTest.PURCHASE_1.purchaseState);
54+
mData.insert(TransactionTest.PURCHASE_1);
55+
mData.insert(TransactionTest.PURCHASE_2);
56+
final Cursor cursor = mData.queryTransactions(TransactionTest.PURCHASE_1.productId, TransactionTest.PURCHASE_1.purchaseState);
5757
assertEquals(cursor.getCount(), 1);
5858
cursor.moveToNext();
59-
final Purchase stored = BillingDB.createPurchase(cursor);
60-
stored.packageName = PurchaseTest.PURCHASE_1.packageName; // Not stored in DB
61-
stored.notificationId = PurchaseTest.PURCHASE_1.notificationId; // Not stored in DB
62-
PurchaseTest.assertEquals(PurchaseTest.PURCHASE_1, stored);
59+
final Transaction stored = BillingDB.createTransaction(cursor);
60+
stored.packageName = TransactionTest.PURCHASE_1.packageName; // Not stored in DB
61+
stored.notificationId = TransactionTest.PURCHASE_1.notificationId; // Not stored in DB
62+
TransactionTest.assertEquals(TransactionTest.PURCHASE_1, stored);
6363
}
6464

6565
}

AndroidBillingLibraryTest/src/net/robotmedia/billing/model/PurchaseManagerTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ public class PurchaseManagerTest extends AndroidTestCase {
1111
protected void tearDown() throws Exception {
1212
super.tearDown();
1313
BillingDB mData = new BillingDB(getContext());
14-
mData.mDb.delete(BillingDB.TABLE_PURCHASES, null, null);
14+
mData.mDb.delete(BillingDB.TABLE_TRANSACTIONS, null, null);
1515
mData.close();
1616
}
1717

1818
@MediumTest
1919
public void testAddPurchase() throws Exception {
20-
PurchaseManager.addPurchase(getContext(), PurchaseTest.PURCHASE_1);
21-
final List<Purchase> purchases = PurchaseManager.getPurchases(getContext());
20+
TransactionManager.addTransaction(getContext(), TransactionTest.PURCHASE_1);
21+
final List<Transaction> purchases = TransactionManager.getTransactions(getContext());
2222
assertEquals(purchases.size(), 1);
23-
final Purchase stored = purchases.get(0);
24-
BillingDBTest.assertEqualsFromDb(PurchaseTest.PURCHASE_1, stored);
23+
final Transaction stored = purchases.get(0);
24+
BillingDBTest.assertEqualsFromDb(TransactionTest.PURCHASE_1, stored);
2525
}
2626

27-
public static Purchase clone(Purchase p) {
28-
return new Purchase(p.orderId, p.productId, p.packageName, p.purchaseState, p.notificationId, p.purchaseTime, p.developerPayload);
27+
public static Transaction clone(Transaction p) {
28+
return new Transaction(p.orderId, p.productId, p.packageName, p.purchaseState, p.notificationId, p.purchaseTime, p.developerPayload);
2929
}
3030

3131
@MediumTest
3232
public void testCountPurchases() throws Exception {
33-
assertEquals(PurchaseManager.countPurchases(getContext(), PurchaseTest.PURCHASE_1.productId), 0);
34-
PurchaseManager.addPurchase(getContext(), PurchaseTest.PURCHASE_1);
35-
assertEquals(PurchaseManager.countPurchases(getContext(), PurchaseTest.PURCHASE_1.productId), 1);
36-
final Purchase newOrder = clone(PurchaseTest.PURCHASE_1);
33+
assertEquals(TransactionManager.countPurchases(getContext(), TransactionTest.PURCHASE_1.productId), 0);
34+
TransactionManager.addTransaction(getContext(), TransactionTest.PURCHASE_1);
35+
assertEquals(TransactionManager.countPurchases(getContext(), TransactionTest.PURCHASE_1.productId), 1);
36+
final Transaction newOrder = clone(TransactionTest.PURCHASE_1);
3737
newOrder.orderId = "newOrder";
38-
PurchaseManager.addPurchase(getContext(), newOrder);
39-
assertEquals(PurchaseManager.countPurchases(getContext(), PurchaseTest.PURCHASE_1.productId), 2);
38+
TransactionManager.addTransaction(getContext(), newOrder);
39+
assertEquals(TransactionManager.countPurchases(getContext(), TransactionTest.PURCHASE_1.productId), 2);
4040
}
4141

4242
@MediumTest
4343
public void testIsPurchased() throws Exception {
44-
assertFalse(PurchaseManager.isPurchased(getContext(), PurchaseTest.PURCHASE_1.productId));
45-
PurchaseManager.addPurchase(getContext(), PurchaseTest.PURCHASE_1);
46-
assertTrue(PurchaseManager.isPurchased(getContext(), PurchaseTest.PURCHASE_1.productId));
44+
assertFalse(TransactionManager.isPurchased(getContext(), TransactionTest.PURCHASE_1.productId));
45+
TransactionManager.addTransaction(getContext(), TransactionTest.PURCHASE_1);
46+
assertTrue(TransactionManager.isPurchased(getContext(), TransactionTest.PURCHASE_1.productId));
4747
}
4848
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.robotmedia.billing.model;
2+
3+
import java.util.Date;
4+
5+
import org.json.JSONObject;
6+
7+
import android.test.suitebuilder.annotation.SmallTest;
8+
9+
import junit.framework.TestCase;
10+
11+
public class TransactionTest extends TestCase {
12+
13+
protected static final Transaction PURCHASE_1 = new Transaction("order1", "android.test.purchased", "com.example", Transaction.PurchaseState.PURCHASED, "notificationId", new Date().getTime(), "developerPayload");
14+
protected static final Transaction PURCHASE_2 = new Transaction("order2", "android.test.refunded", "com.example", Transaction.PurchaseState.REFUNDED, "notificationId", new Date().getTime(), "developerPayload");
15+
16+
public static void assertEquals(Transaction a, Transaction b) {
17+
assertEquals(a.orderId, b.orderId);
18+
assertEquals(a.productId, b.productId);
19+
assertEquals(a.packageName, b.packageName);
20+
assertEquals(a.purchaseState, b.purchaseState);
21+
assertEquals(a.notificationId, b.notificationId);
22+
assertEquals(a.purchaseTime, b.purchaseTime);
23+
assertEquals(a.developerPayload, b.developerPayload);
24+
}
25+
26+
@SmallTest
27+
public void testParseAllFields() throws Exception {
28+
JSONObject json = new JSONObject();
29+
json.put(Transaction.ORDER_ID, PURCHASE_1.orderId);
30+
json.put(Transaction.PRODUCT_ID, PURCHASE_1.productId);
31+
json.put(Transaction.PACKAGE_NAME, PURCHASE_1.packageName);
32+
json.put(Transaction.PURCHASE_STATE, PURCHASE_1.purchaseState.ordinal());
33+
json.put(Transaction.NOTIFICATION_ID, PURCHASE_1.notificationId);
34+
json.put(Transaction.PURCHASE_TIME, PURCHASE_1.purchaseTime);
35+
json.put(Transaction.DEVELOPER_PAYLOAD, PURCHASE_1.developerPayload);
36+
final Transaction parsed = Transaction.parse(json);
37+
assertEquals(PURCHASE_1, parsed);
38+
}
39+
40+
@SmallTest
41+
public void testParseOnlyMandatoryFields() throws Exception {
42+
JSONObject json = new JSONObject();
43+
json.put(Transaction.PRODUCT_ID, PURCHASE_1.productId);
44+
json.put(Transaction.PACKAGE_NAME, PURCHASE_1.packageName);
45+
json.put(Transaction.PURCHASE_STATE, PURCHASE_1.purchaseState.ordinal());
46+
json.put(Transaction.PURCHASE_TIME, PURCHASE_1.purchaseTime);
47+
final Transaction parsed = Transaction.parse(json);
48+
assertNull(parsed.orderId);
49+
assertEquals(PURCHASE_1.productId, parsed.productId);
50+
assertEquals(PURCHASE_1.packageName, parsed.packageName);
51+
assertEquals(PURCHASE_1.purchaseState, parsed.purchaseState);
52+
assertNull(parsed.notificationId);
53+
assertEquals(PURCHASE_1.purchaseTime, parsed.purchaseTime);
54+
assertNull(parsed.developerPayload);
55+
}
56+
57+
@SmallTest
58+
public void testPurchaseStateOrdinal() throws Exception {
59+
assertEquals(Transaction.PurchaseState.PURCHASED.ordinal(), 0);
60+
assertEquals(Transaction.PurchaseState.CANCELLED.ordinal(), 1);
61+
assertEquals(Transaction.PurchaseState.REFUNDED.ordinal(), 2);
62+
}
63+
}

0 commit comments

Comments
 (0)