Skip to content

Commit baa094c

Browse files
committed
Fixed: crash when creating a sub-account and changing the type to another different from the parent
Also added test case Fixed: crash when dismissing error dialog after export Fixed: crash when resuming app with passcode set and app killed
1 parent b1b5d57 commit baa094c

24 files changed

Lines changed: 76 additions & 37 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.gnucash.android.db.SplitsDbAdapter;
3636
import org.gnucash.android.db.TransactionsDbAdapter;
3737
import org.gnucash.android.model.Account;
38+
import org.gnucash.android.model.AccountType;
3839
import org.gnucash.android.model.Money;
3940
import org.gnucash.android.receivers.AccountCreator;
4041
import org.gnucash.android.ui.account.AccountsActivity;
@@ -47,19 +48,24 @@
4748
import java.util.Currency;
4849
import java.util.List;
4950

51+
import static android.support.test.espresso.Espresso.onData;
5052
import static android.support.test.espresso.Espresso.onView;
5153
import static android.support.test.espresso.action.ViewActions.clearText;
5254
import static android.support.test.espresso.action.ViewActions.click;
5355
import static android.support.test.espresso.action.ViewActions.longClick;
5456
import static android.support.test.espresso.action.ViewActions.scrollTo;
57+
import static android.support.test.espresso.action.ViewActions.swipeRight;
5558
import static android.support.test.espresso.action.ViewActions.typeText;
5659
import static android.support.test.espresso.assertion.ViewAssertions.matches;
60+
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
5761
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
5862
import static android.support.test.espresso.matcher.ViewMatchers.isNotChecked;
5963
import static android.support.test.espresso.matcher.ViewMatchers.withId;
6064
import static android.support.test.espresso.matcher.ViewMatchers.withText;
6165
import static org.assertj.core.api.Assertions.assertThat;
6266
import static org.hamcrest.Matchers.allOf;
67+
import static org.hamcrest.Matchers.instanceOf;
68+
import static org.hamcrest.Matchers.is;
6369
import static org.hamcrest.Matchers.not;
6470

6571
@RunWith(AndroidJUnit4.class)
@@ -213,6 +219,31 @@ public void testChangeParentAccount() {
213219
assertThat(DUMMY_ACCOUNT_UID).isEqualTo(parentUID);
214220
}
215221

222+
/**
223+
* When creating a sub-account (starting from within another account), if we change the account
224+
* type to another type with no accounts of that type, then the parent account list should be hidden.
225+
* The account which is then created is not a sub-account, but rather a top-level account
226+
*/
227+
@Test
228+
public void shouldHideParentAccountViewWhenNoParentsExist(){
229+
onView(withText(DUMMY_ACCOUNT_NAME)).perform(click());
230+
onView(withId(R.id.fragment_transaction_list)).perform(swipeRight());
231+
onView(withText(R.string.label_create_account)).check(matches(isDisplayed())).perform(click());
232+
sleep(1000);
233+
onView(withId(R.id.checkbox_parent_account)).check(matches(allOf(isChecked())));
234+
onView(withId(R.id.input_account_name)).perform(typeText("Trading account"));
235+
onView(withId(R.id.input_account_type_spinner)).perform(click());
236+
onData(allOf(is(instanceOf(String.class)), is(AccountType.TRADING.name()))).perform(click());
237+
238+
onView(withId(R.id.layout_parent_account)).check(matches(not(isDisplayed())));
239+
onView(withId(R.id.menu_save)).perform(click());
240+
241+
//no sub-accounts
242+
assertThat(mAccountsDbAdapter.getSubAccountCount(DUMMY_ACCOUNT_UID)).isEqualTo(0);
243+
assertThat(mAccountsDbAdapter.getSubAccountCount(mAccountsDbAdapter.getOrCreateGnuCashRootAccountUID())).isEqualTo(2);
244+
assertThat(mAccountsDbAdapter.getSimpleAccountList()).extracting("mAccountType").contains(AccountType.TRADING);
245+
}
246+
216247
@Test
217248
public void testEditAccount(){
218249
String editedAccountName = "Edited Account";

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

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

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

19-
import android.app.Fragment;
2019
import android.content.ContentValues;
2120
import android.content.Intent;
2221
import android.content.SharedPreferences;
@@ -28,8 +27,6 @@
2827
import android.support.test.runner.AndroidJUnit4;
2928
import android.test.ActivityInstrumentationTestCase2;
3029
import android.util.Log;
31-
import android.widget.LinearLayout;
32-
import android.widget.Spinner;
3330

3431
import org.gnucash.android.R;
3532
import org.gnucash.android.db.AccountsDbAdapter;
@@ -58,7 +55,6 @@
5855
import java.util.List;
5956
import java.util.Locale;
6057

61-
import static android.support.test.espresso.Espresso.onData;
6258
import static android.support.test.espresso.Espresso.onView;
6359
import static android.support.test.espresso.action.ViewActions.clearText;
6460
import static android.support.test.espresso.action.ViewActions.click;
@@ -69,15 +65,10 @@
6965
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
7066
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
7167
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
72-
import static android.support.test.espresso.matcher.ViewMatchers.isNotChecked;
73-
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
7468
import static android.support.test.espresso.matcher.ViewMatchers.withId;
75-
import static android.support.test.espresso.matcher.ViewMatchers.withSpinnerText;
7669
import static android.support.test.espresso.matcher.ViewMatchers.withText;
77-
import static org.assertj.android.api.Assertions.assertThat;
7870
import static org.assertj.core.api.Assertions.assertThat;
7971
import static org.hamcrest.Matchers.allOf;
80-
import static org.hamcrest.Matchers.instanceOf;
8172
import static org.hamcrest.Matchers.is;
8273
import static org.hamcrest.Matchers.not;
8374

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public long getID(@NonNull String uid){
286286
* Returns the string unique ID (GUID) of a record in the database
287287
* @param id long database record ID
288288
* @return GUID of the record
289+
* @throws IllegalArgumentException if the record ID does not exist in the database
289290
*/
290291
public String getUID(long id){
291292
Cursor cursor = mDb.query(mTableName,

app/src/main/java/org/gnucash/android/importer/ImportAsyncTask.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,15 @@ protected void onPostExecute(Boolean importSuccess) {
9494
if (mDelegate != null)
9595
mDelegate.onTaskComplete();
9696

97-
if (progressDialog != null && progressDialog.isShowing())
98-
progressDialog.dismiss();
97+
try {
98+
if (progressDialog != null && progressDialog.isShowing())
99+
progressDialog.dismiss();
100+
} catch (IllegalArgumentException ex){
101+
//TODO: This is a hack to catch "View not attached to window" exceptions
102+
//FIXME by moving the creation and display of the progress dialog to the Fragment
103+
} finally {
104+
progressDialog = null;
105+
}
99106

100107
int message = importSuccess ? R.string.toast_success_importing_accounts : R.string.toast_error_importing_accounts;
101108
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void onCreate(Bundle savedInstanceState) {
239239
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
240240
Bundle savedInstanceState) {
241241
View view = inflater.inflate(R.layout.fragment_new_account, container, false);
242-
getSherlockActivity().getSupportActionBar().setTitle(R.string.title_add_account);
242+
getSherlockActivity().getSupportActionBar().setTitle(R.string.label_create_account);
243243
mCurrencySpinner = (Spinner) view.findViewById(R.id.input_currency_spinner);
244244
mNameEditText = (EditText) view.findViewById(R.id.input_account_name);
245245
//mNameEditText.requestFocus();
@@ -443,14 +443,14 @@ private void setSelectedCurrency(String currencyCode){
443443
* @param parentAccountId Record ID of parent account to be selected
444444
*/
445445
private void setParentAccountSelection(long parentAccountId){
446-
if (parentAccountId > 0 && parentAccountId != mRootAccountId){
447-
mParentCheckBox.setChecked(true);
448-
mParentAccountSpinner.setEnabled(true);
449-
} else
446+
if (parentAccountId <= 0 || parentAccountId == mRootAccountId) {
450447
return;
448+
}
451449

452450
for (int pos = 0; pos < mParentAccountCursorAdapter.getCount(); pos++) {
453451
if (mParentAccountCursorAdapter.getItemId(pos) == parentAccountId){
452+
mParentCheckBox.setChecked(true);
453+
mParentAccountSpinner.setEnabled(true);
454454
mParentAccountSpinner.setSelection(pos, true);
455455
break;
456456
}
@@ -581,11 +581,15 @@ private void loadParentAccountList(AccountType accountType){
581581
mParentAccountCursor.close();
582582

583583
mParentAccountCursor = mAccountsDbAdapter.fetchAccountsOrderedByFullName(condition, null);
584-
if (mParentAccountCursor.getCount() <= 0){
585-
final View view = getView();
586-
assert view != null;
584+
final View view = getView();
585+
assert view != null;
586+
if (mParentAccountCursor.getCount() <= 0){
587+
mParentCheckBox.setChecked(false); //disable before hiding, else we can still read it when saving
587588
view.findViewById(R.id.layout_parent_account).setVisibility(View.GONE);
588589
view.findViewById(R.id.label_parent_account).setVisibility(View.GONE);
590+
} else {
591+
view.findViewById(R.id.layout_parent_account).setVisibility(View.VISIBLE);
592+
view.findViewById(R.id.label_parent_account).setVisibility(View.VISIBLE);
589593
}
590594

591595
mParentAccountCursorAdapter = new QualifiedAccountNameCursorAdapter(

app/src/main/java/org/gnucash/android/ui/settings/PasscodePreferenceFragment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.actionbarsherlock.app.SherlockPreferenceActivity;
3333

3434
import org.gnucash.android.R;
35+
import org.gnucash.android.app.GnuCashApplication;
3536
import org.gnucash.android.ui.UxArgument;
3637
import org.gnucash.android.ui.passcode.PasscodeLockScreenActivity;
3738
import org.gnucash.android.ui.passcode.PasscodePreferenceActivity;
@@ -108,6 +109,10 @@ public boolean onPreferenceClick(Preference preference) {
108109
public void onActivityResult(int requestCode, int resultCode, Intent data) {
109110
super.onActivityResult(requestCode, resultCode, data);
110111

112+
if (mEditor == null){
113+
mEditor = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext()).edit();
114+
}
115+
111116
switch (requestCode) {
112117
case PASSCODE_REQUEST_CODE:
113118
if (resultCode == Activity.RESULT_OK && data != null) {

app/src/main/res/layout/fragment_account_detail.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
android:id="@+id/add_preference_button"
4141
android:layout_width="match_parent"
4242
android:layout_height="wrap_content"
43-
android:text="@string/title_add_account" />
43+
android:text="@string/label_create_account" />
4444
</LinearLayout>
4545
</LinearLayout>

app/src/main/res/layout/fragment_accounts_list.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
android:id="@+id/add_account_button"
5151
style="@style/ButtonStyle"
5252
android:onClick="onNewAccountClick"
53-
android:text="@string/title_add_account" />
53+
android:text="@string/label_create_account" />
5454
</LinearLayout>
5555
</RelativeLayout>

app/src/main/res/menu/account_actions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
1919
<item android:id="@+id/menu_add_account"
2020
android:icon="@drawable/content_new_holo_dark"
21-
android:title="@string/title_add_account"
21+
android:title="@string/label_create_account"
2222
android:showAsAction="always"/>
2323

2424
<item android:id="@+id/menu_recurring_transactions"

app/src/main/res/menu/sub_account_actions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<item android:id="@+id/menu_add_account"
2424
android:icon="@drawable/content_new_holo_dark"
25-
android:title="@string/title_add_account"
25+
android:title="@string/label_create_account"
2626
android:showAsAction="always"/>
2727

2828
<item android:id="@+id/menu_edit_account"

0 commit comments

Comments
 (0)