Skip to content

Commit 7667b7b

Browse files
committed
Added test for #329
Fixed: missing strings generated by gradle causing Travis builds to fail Code refactoring
1 parent 49e25ca commit 7667b7b

9 files changed

Lines changed: 74 additions & 76 deletions

File tree

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
sudo: true
21
language: android
32
jdk: oraclejdk7
43
android:
@@ -23,10 +22,7 @@ android:
2322

2423
before_install:
2524
#- echo yes | android update sdk --all --filter build-tools-22.0.1 --no-ui --force > /dev/null
26-
# required libs for android build tools
27-
- if [ `uname -m` = x86_64 ]; then sudo apt-get update; fi
28-
- if [ `uname -m` = x86_64 ]; then sudo apt-get install -qq --force-yes libgd2-xpm ia32-libs ia32-libs-multiarch; fi
29-
25+
3026
# Emulator Management: Create, Start and Wait
3127
before_script:
3228
- echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a

app/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ android {
3434
resValue "string", "dropbox_app_key", RELEASE_DROPBOX_APP_KEY
3535
resValue "string", "dropbox_app_secret", RELEASE_DROPBOX_APP_SECRET
3636
resValue "string", "manifest_dropbox_app_key", "db-${RELEASE_DROPBOX_APP_KEY}"
37+
} else {
38+
resValue "string", "dropbox_app_key", "dhjh8ke9wf05948"
39+
resValue "string", "dropbox_app_secret", "h2t9fphj3nr4wkw"
40+
resValue "string", "manifest_dropbox_app_key", "db-dhjh8ke9wf05948"
3741
}
3842
}
3943

@@ -81,9 +85,6 @@ android {
8185
applicationId 'org.gnucash.android.devel'
8286
testApplicationId 'org.gnucash.android.test.devel'
8387
resValue "string", "app_name", "GnuCash-devel"
84-
resValue "string", "dropbox_app_key", "dhjh8ke9wf05948"
85-
resValue "string", "dropbox_app_secret", "h2t9fphj3nr4wkw"
86-
resValue "string", "manifest_dropbox_app_key", "db-dhjh8ke9wf05948"
8788
versionName "${versionMajor}.${versionMinor}.${versionPatch}-dev${versionBuild}_${buildTime()}"
8889
resValue "string", "app_version_name", "${versionName}"
8990
}

app/src/androidTest/java/org/gnucash/android/test/ui/AccountsActivityTest.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected void setUp() throws Exception {
6666
Context context = getInstrumentation().getTargetContext();
6767
Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
6868
editor.putBoolean(context.getString(R.string.key_first_run), false);
69+
editor.putInt(AccountsActivity.LAST_OPEN_TAB_INDEX, AccountsActivity.INDEX_TOP_LEVEL_ACCOUNTS_FRAGMENT);
6970
editor.commit();
7071

7172
mDbHelper = new DatabaseHelper(context);
@@ -86,14 +87,6 @@ protected void setUp() throws Exception {
8687
account.setCurrency(Currency.getInstance(DUMMY_ACCOUNT_CURRENCY_CODE));
8788
mAccountsDbAdapter.addAccount(account);
8889

89-
getInstrumentation().runOnMainSync(new Runnable() {
90-
@Override
91-
public void run() {
92-
getActivity().setTab(AccountsActivity.INDEX_TOP_LEVEL_ACCOUNTS_FRAGMENT);
93-
}
94-
});
95-
96-
9790
//the What's new dialog is usually displayed on first run
9891
String dismissDialog = getActivity().getString(R.string.label_dismiss);
9992
if (mSolo.waitForText(dismissDialog,1,1000)){
@@ -158,7 +151,6 @@ public void testCreateAccount(){
158151

159152
EditText inputAccountName = (EditText) mSolo.getCurrentActivity().findViewById(R.id.edit_text_account_name);
160153
String NEW_ACCOUNT_NAME = "A New Account";
161-
// mSolo.enterText(0, NEW_ACCOUNT_NAME);
162154
mSolo.enterText(inputAccountName, NEW_ACCOUNT_NAME);
163155
mSolo.clickOnActionBarItem(R.id.menu_save);
164156

@@ -183,14 +175,12 @@ public void testChangeParentAccount(){
183175

184176
mSolo.clickLongOnText(accountName);
185177
mSolo.clickOnView(getActivity().findViewById(R.id.context_menu_edit_accounts));
186-
// clickSherlockActionBarItem(R.id.context_menu_edit_accounts);
187178
mSolo.waitForView(EditText.class);
188179

189180
mSolo.clickOnCheckBox(1);
190181
mSolo.pressSpinnerItem(2, 0);
191-
// mSolo.clickOnText(DUMMY_ACCOUNT_NAME);
192182

193-
clickSherlockActionBarItem(R.id.menu_save);
183+
mSolo.clickOnView(getActivity().findViewById(R.id.menu_save));
194184

195185
mSolo.waitForText(getActivity().getString(R.string.title_accounts));
196186
Account editedAccount = mAccountsDbAdapter.getAccount(accountUID);

app/src/androidTest/java/org/gnucash/android/test/ui/TransactionsActivityTest.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.gnucash.android.test.ui;
1818

19+
import android.content.ContentValues;
1920
import android.content.Intent;
2021
import android.content.SharedPreferences;
2122
import android.content.SharedPreferences.Editor;
@@ -34,6 +35,7 @@
3435
import org.gnucash.android.R;
3536
import org.gnucash.android.db.AccountsDbAdapter;
3637
import org.gnucash.android.db.DatabaseHelper;
38+
import org.gnucash.android.db.DatabaseSchema;
3739
import org.gnucash.android.db.SplitsDbAdapter;
3840
import org.gnucash.android.db.TransactionsDbAdapter;
3941
import org.gnucash.android.model.*;
@@ -103,8 +105,8 @@ protected void setUp() throws Exception {
103105

104106
long id1 = mAccountsDbAdapter.addAccount(account);
105107
long id2 = mAccountsDbAdapter.addAccount(account2);
106-
assertTrue(id1 > 0);
107-
assertTrue(id2 > 0);
108+
assertThat(id1).isGreaterThan(0);
109+
assertThat(id2).isGreaterThan(0);
108110

109111
mTransaction = new Transaction(TRANSACTION_NAME);
110112
mTransaction.setNote("What up?");
@@ -209,7 +211,7 @@ private void validateEditTransactionFields(Transaction transaction){
209211

210212
//TODO: Add test for only one account but with double-entry enabled
211213

212-
public void atestAddTransaction(){
214+
public void testAddTransaction(){
213215
setDoubleEntryEnabled(true);
214216
mSolo.waitForText(TRANSACTION_NAME);
215217

@@ -311,6 +313,29 @@ public void testDefaultTransactionType(){
311313
mSolo.goBack();
312314
}
313315

316+
public void testChildAccountsShouldUseParentTransferAccountSetting(){
317+
Account transferAccount = new Account("New Transfer Acct");
318+
mAccountsDbAdapter.addAccount(transferAccount);
319+
320+
Account childAccount = new Account("Child Account");
321+
childAccount.setParentUID(DUMMY_ACCOUNT_UID);
322+
mAccountsDbAdapter.addAccount(childAccount);
323+
ContentValues contentValues = new ContentValues();
324+
contentValues.put(DatabaseSchema.AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID, transferAccount.getUID());
325+
mAccountsDbAdapter.updateRecord(DUMMY_ACCOUNT_UID, contentValues);
326+
327+
328+
Intent intent = new Intent(mSolo.getCurrentActivity(), TransactionsActivity.class);
329+
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
330+
intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, childAccount.getUID());
331+
getActivity().startActivity(intent);
332+
mSolo.sleep(1000);
333+
Spinner spinner = (Spinner) mSolo.getView(R.id.input_double_entry_accounts_spinner);
334+
335+
long transferAccountID = mAccountsDbAdapter.getID(transferAccount.getUID());
336+
assertThat(transferAccountID).isEqualTo(spinner.getSelectedItemId());
337+
}
338+
314339
public void testToggleTransactionType(){
315340
mSolo.waitForText(DUMMY_ACCOUNT_NAME);
316341

app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ public List<Account> getAllAccounts(){
533533
* @return List of {@link Account}s in the database
534534
*/
535535
public List<Account> getSimpleAccountList(){
536-
LinkedList<Account> accounts = new LinkedList<Account>();
537-
Cursor c = fetchAccounts(null, null, null);
536+
LinkedList<Account> accounts = new LinkedList<>();
537+
Cursor c = fetchAccounts(null, null, AccountEntry.COLUMN_FULL_NAME + " ASC");
538538

539539
try {
540540
while (c.moveToNext()) {
@@ -553,7 +553,7 @@ public List<Account> getSimpleAccountList(){
553553
* @return List of {@link Account}s in the database
554554
*/
555555
public List<Account> getSimpleAccountList(String where, String[] whereArgs, String orderBy){
556-
LinkedList<Account> accounts = new LinkedList<Account>();
556+
LinkedList<Account> accounts = new LinkedList<>();
557557
Cursor c = fetchAccounts(where, whereArgs, orderBy);
558558
try {
559559
while (c.moveToNext()) {

app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import java.util.GregorianCalendar;
9191
import java.util.List;
9292
import java.util.Locale;
93+
import java.util.Objects;
9394

9495
/**
9596
* Fragment for creating or editing transactions
@@ -487,19 +488,15 @@ private void initalizeViews() {
487488
if (mUseDoubleEntry){
488489
String currentAccountUID = mAccountUID;
489490
long defaultTransferAccountID = 0;
490-
491+
String rootAccountUID = mAccountsDbAdapter.getOrCreateGnuCashRootAccountUID();
491492
do {
492-
long transAccID = mAccountsDbAdapter.getDefaultTransferAccountID(mAccountsDbAdapter.getID(currentAccountUID));
493-
if (transAccID > 0) {
494-
defaultTransferAccountID = transAccID;
493+
defaultTransferAccountID = mAccountsDbAdapter.getDefaultTransferAccountID(mAccountsDbAdapter.getID(currentAccountUID));
494+
if (defaultTransferAccountID > 0) {
495+
setSelectedTransferAccount(defaultTransferAccountID);
496+
break; //we found a parent with default transfer setting
495497
}
496498
currentAccountUID = mAccountsDbAdapter.getParentAccountUID(currentAccountUID);
497-
}
498-
while (currentAccountUID != null && defaultTransferAccountID == 0);
499-
500-
if (defaultTransferAccountID > 0){
501-
setSelectedTransferAccount(defaultTransferAccountID);
502-
}
499+
} while (!currentAccountUID.equals(rootAccountUID));
503500
}
504501
}
505502

app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ protected void onCreate(Bundle savedInstanceState) {
273273

274274
setupActionBarNavigation();
275275

276-
if (getIntent().getAction().equals(Intent.ACTION_INSERT_OR_EDIT)) {
276+
final String action = getIntent().getAction();
277+
if (action.equals(Intent.ACTION_INSERT_OR_EDIT) || action.equals(Intent.ACTION_INSERT)) {
277278
mPager.setVisibility(View.GONE);
278279
mTitlePageIndicator.setVisibility(View.GONE);
279280

app/src/test/java/org/gnucash/android/test/unit/db/AccountsDbAdapterTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public void shouldCreateDefaultRootAccount(){
116116
List<Account> accounts = mAccountsDbAdapter.getSimpleAccountList();
117117
assertThat(accounts).extracting("mAccountType").contains(AccountType.ROOT);
118118

119+
String rootAccountUID = mAccountsDbAdapter.getOrCreateGnuCashRootAccountUID();
120+
assertThat(rootAccountUID).isEqualTo(accounts.get(1).getParentUID());
119121
}
120122

121123
@Test

app/src/test/java/org/gnucash/android/test/unit/model/MoneyTest.java

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
public class MoneyTest{
3131

3232
private static final String CURRENCY_CODE = "EUR";
33-
private Money money;
33+
private Money mMoneyInEur;
3434
private int mHashcode;
3535
private String amountString = "15.75";
3636

3737
@Before
3838
public void setUp() throws Exception {
39-
money = new Money(new BigDecimal(amountString), Currency.getInstance(CURRENCY_CODE));
40-
mHashcode = money.hashCode();
39+
mMoneyInEur = new Money(new BigDecimal(amountString), Currency.getInstance(CURRENCY_CODE));
40+
mHashcode = mMoneyInEur.hashCode();
4141
}
4242

4343
@Test
@@ -64,101 +64,87 @@ public void testCreation(){
6464

6565
@Test
6666
public void testAddition(){
67-
Money result = money.add(new Money("5", CURRENCY_CODE));
67+
Money result = mMoneyInEur.add(new Money("5", CURRENCY_CODE));
6868
assertEquals("20.75", result.toPlainString());
69-
assertNotSame(result, money);
69+
assertNotSame(result, mMoneyInEur);
7070
validateImmutability();
7171
}
7272

73-
@Test
73+
@Test(expected = IllegalArgumentException.class)
7474
public void testAdditionWithIncompatibleCurrency(){
7575
Money addend = new Money("4", "USD");
76-
Exception expectedException = null;
77-
try{
78-
money.add(addend);
79-
} catch (Exception e) {
80-
expectedException = e;
81-
}
82-
assertNotNull(expectedException);
83-
assertTrue(expectedException instanceof IllegalArgumentException);
76+
mMoneyInEur.add(addend);
8477
}
8578

8679
@Test
8780
public void testSubtraction(){
88-
Money result = money.subtract(new Money("2", CURRENCY_CODE));
81+
Money result = mMoneyInEur.subtract(new Money("2", CURRENCY_CODE));
8982
assertEquals(new BigDecimal("13.75"), result.asBigDecimal());
90-
assertNotSame(result, money);
83+
assertNotSame(result, mMoneyInEur);
9184
validateImmutability();
9285
}
9386

94-
@Test
87+
@Test(expected = IllegalArgumentException.class)
9588
public void testSubtractionWithDifferentCurrency(){
9689
Money addend = new Money("4", "USD");
97-
Exception expectedException = null;
98-
try{
99-
money.subtract(addend);
100-
} catch (Exception e) {
101-
expectedException = e;
102-
}
103-
assertNotNull(expectedException);
104-
assertTrue(expectedException instanceof IllegalArgumentException);
90+
mMoneyInEur.subtract(addend);
10591
}
10692

10793
@Test
10894
public void testMultiplication(){
109-
Money result = money.multiply(new Money(BigDecimal.TEN, Currency.getInstance(CURRENCY_CODE)));
95+
Money result = mMoneyInEur.multiply(new Money(BigDecimal.TEN, Currency.getInstance(CURRENCY_CODE)));
11096
assertThat("157.50").isEqualTo(result.toPlainString());
111-
assertThat(result).isNotEqualTo(money);
97+
assertThat(result).isNotEqualTo(mMoneyInEur);
11298
validateImmutability();
11399
}
114100

115101
@Test(expected = IllegalArgumentException.class)
116102
public void testMultiplicationWithDifferentCurrencies(){
117103
Money addend = new Money("4", "USD");
118-
money.multiply(addend);
104+
mMoneyInEur.multiply(addend);
119105
}
120106

121107
@Test
122108
public void testDivision(){
123-
Money result = money.divide(2);
109+
Money result = mMoneyInEur.divide(2);
124110
assertThat(result.toPlainString()).isEqualTo("7.88");
125-
assertThat(result).isNotEqualTo(money);
111+
assertThat(result).isNotEqualTo(mMoneyInEur);
126112
validateImmutability();
127113
}
128114

129115
@Test(expected = IllegalArgumentException.class)
130116
public void testDivisionWithDifferentCurrency(){
131117
Money addend = new Money("4", "USD");
132-
money.divide(addend);
118+
mMoneyInEur.divide(addend);
133119
}
134120

135121
@Test
136122
public void testNegation(){
137-
Money result = money.negate();
123+
Money result = mMoneyInEur.negate();
138124
assertThat(result.toPlainString()).startsWith("-");
139125
validateImmutability();
140126
}
141127

142128
@Test
143129
public void testPrinting(){
144-
assertEquals(money.asString(), money.toPlainString());
145-
assertEquals(amountString, money.asString());
130+
assertEquals(mMoneyInEur.asString(), mMoneyInEur.toPlainString());
131+
assertEquals(amountString, mMoneyInEur.asString());
146132

147133
// the unicode for Euro symbol is \u20AC
148134
String symbol = Currency.getInstance("EUR").getSymbol(Locale.GERMAN);
149135
String symbolUS = Currency.getInstance("EUR").getSymbol(Locale.US);
150-
assertEquals("15,75 " + symbol, money.formattedString(Locale.GERMAN));
151-
assertEquals("15.75 " + symbolUS, money.formattedString(Locale.US));
136+
assertEquals("15,75 " + symbol, mMoneyInEur.formattedString(Locale.GERMAN));
137+
assertEquals("15.75 " + symbolUS, mMoneyInEur.formattedString(Locale.US));
152138

153139
//always prints with 2 decimal places only
154140
Money some = new Money("9.7469");
155141
assertEquals("9.75", some.asString());
156142
}
157143

158144
public void validateImmutability(){
159-
assertEquals(mHashcode, money.hashCode());
160-
assertEquals(amountString, money.toPlainString());
161-
assertEquals(CURRENCY_CODE, money.getCurrency().getCurrencyCode());
145+
assertEquals(mHashcode, mMoneyInEur.hashCode());
146+
assertEquals(amountString, mMoneyInEur.toPlainString());
147+
assertEquals(CURRENCY_CODE, mMoneyInEur.getCurrency().getCurrencyCode());
162148
}
163149

164150
}

0 commit comments

Comments
 (0)