|
28 | 28 | import android.util.Log; |
29 | 29 | import android.view.View; |
30 | 30 | import android.widget.EditText; |
| 31 | +import android.widget.LinearLayout; |
31 | 32 | import android.widget.Spinner; |
32 | 33 | import android.widget.TextView; |
33 | 34 | import android.widget.ToggleButton; |
@@ -278,6 +279,123 @@ public void testEditTransaction(){ |
278 | 279 | mSolo.waitForText("Pasta"); |
279 | 280 | } |
280 | 281 |
|
| 282 | + /** |
| 283 | + * Tests that transactions splits are automatically balanced and an imbalance account will be created |
| 284 | + * This test case assumes that single entry is used |
| 285 | + */ |
| 286 | + public void testAutoBalanceTransactions(){ |
| 287 | + setDoubleEntryEnabled(false); |
| 288 | + mTransactionsDbAdapter.deleteAllRecords(); |
| 289 | + mSolo.sleep(1000); |
| 290 | + assertThat(mTransactionsDbAdapter.getTotalTransactionsCount()).isEqualTo(0); |
| 291 | + String imbalanceAcctUID = mAccountsDbAdapter.getImbalanceAccountUID(Currency.getInstance(CURRENCY_CODE)); |
| 292 | + assertThat(imbalanceAcctUID).isNull(); |
| 293 | + |
| 294 | + mSolo.waitForText(TRANSACTION_NAME); |
| 295 | + |
| 296 | + validateTransactionListDisplayed(); |
| 297 | + clickSherlockActionBarItem(R.id.menu_add_transaction); |
| 298 | + |
| 299 | + mSolo.waitForText("New transaction"); |
| 300 | + |
| 301 | + //validate creation of transaction |
| 302 | + mSolo.enterText(0, "Autobalance"); |
| 303 | + mSolo.enterText(1, "499"); |
| 304 | + |
| 305 | + View typeToogleButton = mSolo.getView(R.id.btn_open_splits); |
| 306 | + assertThat(typeToogleButton).isNotVisible(); //no double entry so no split editor |
| 307 | + |
| 308 | + mSolo.clickOnActionBarItem(R.id.menu_save); |
| 309 | + |
| 310 | + mSolo.sleep(2000); |
| 311 | + |
| 312 | + assertThat(mTransactionsDbAdapter.getTotalTransactionsCount()).isEqualTo(1); |
| 313 | + Transaction transaction = mTransactionsDbAdapter.getAllTransactions().get(0); |
| 314 | + assertThat(transaction.getSplits()).hasSize(2); |
| 315 | + imbalanceAcctUID = mAccountsDbAdapter.getImbalanceAccountUID(Currency.getInstance(CURRENCY_CODE)); |
| 316 | + assertThat(imbalanceAcctUID).isNotNull(); |
| 317 | + assertThat(imbalanceAcctUID).isNotEmpty(); |
| 318 | + assertTrue(mAccountsDbAdapter.isHiddenAccount(imbalanceAcctUID)); //imbalance account should be hidden in single entry mode |
| 319 | + |
| 320 | + assertThat(transaction.getSplits()).extracting("mAccountUID").contains(imbalanceAcctUID); |
| 321 | + |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * Tests input of transaction splits using the split editor. |
| 326 | + * Also validates that the imbalance from the split editor will be automatically added as a split |
| 327 | + */ |
| 328 | + public void testSplitEditor(){ |
| 329 | + setDoubleEntryEnabled(true); |
| 330 | + mTransactionsDbAdapter.deleteAllRecords(); |
| 331 | + mSolo.sleep(1000); |
| 332 | + //when we start there should be no imbalance account in the system |
| 333 | + String imbalanceAcctUID = mAccountsDbAdapter.getImbalanceAccountUID(Currency.getInstance(CURRENCY_CODE)); |
| 334 | + assertThat(imbalanceAcctUID).isNull(); |
| 335 | + |
| 336 | + mSolo.waitForText(TRANSACTION_NAME); |
| 337 | + |
| 338 | + validateTransactionListDisplayed(); |
| 339 | + clickSherlockActionBarItem(R.id.menu_add_transaction); |
| 340 | + |
| 341 | + mSolo.waitForText("New transaction"); |
| 342 | + |
| 343 | + //validate creation of transaction |
| 344 | + mSolo.enterText(0, "Autobalance"); |
| 345 | + mSolo.enterText(1, "4499"); |
| 346 | + |
| 347 | + mSolo.clickOnButton(1); |
| 348 | + mSolo.waitForDialogToOpen(); |
| 349 | + |
| 350 | + LinearLayout splitListView = (LinearLayout) mSolo.getView(R.id.split_list_layout); |
| 351 | + assertThat(splitListView).hasChildCount(1); |
| 352 | + |
| 353 | + //TODO: enable this assert when we fix the sign of amounts in split editor |
| 354 | + //assertThat(mSolo.getEditText(0).getText().toString()).isEqualTo("44.99"); |
| 355 | + View addSplit = mSolo.getView(R.id.btn_add_split); |
| 356 | + mSolo.clickOnView(addSplit); |
| 357 | + mSolo.sleep(5000); |
| 358 | + assertThat(splitListView).hasChildCount(2); |
| 359 | + |
| 360 | + mSolo.enterText(0, "4000"); |
| 361 | + |
| 362 | + TextView imbalanceTextView = (TextView) mSolo.getView(R.id.imbalance_textview); |
| 363 | + assertThat(imbalanceTextView).hasText("-4.99 $"); |
| 364 | + |
| 365 | + mSolo.clickOnView(mSolo.getView(R.id.btn_save)); |
| 366 | + mSolo.waitForDialogToClose(); |
| 367 | + mSolo.sleep(3000); |
| 368 | + //after we use split editor, we should not be able to toggle the transaction type |
| 369 | + assertThat(mSolo.getView(R.id.input_transaction_type)).isNotVisible(); |
| 370 | + |
| 371 | + mSolo.clickOnActionBarItem(R.id.menu_save); |
| 372 | + |
| 373 | + mSolo.sleep(3000); |
| 374 | + |
| 375 | + List<Transaction> transactions = mTransactionsDbAdapter.getAllTransactions(); |
| 376 | + assertThat(transactions).hasSize(1); |
| 377 | + |
| 378 | + Transaction transaction = transactions.get(0); |
| 379 | + |
| 380 | + assertThat(transaction.getSplits()).hasSize(3); //auto-balanced |
| 381 | + imbalanceAcctUID = mAccountsDbAdapter.getImbalanceAccountUID(Currency.getInstance(CURRENCY_CODE)); |
| 382 | + assertThat(imbalanceAcctUID).isNotNull(); |
| 383 | + assertThat(imbalanceAcctUID).isNotEmpty(); |
| 384 | + assertFalse(mAccountsDbAdapter.isHiddenAccount(imbalanceAcctUID)); |
| 385 | + |
| 386 | + //at least one split will belong to the imbalance account |
| 387 | + assertThat(transaction.getSplits()).extracting("mAccountUID").contains(imbalanceAcctUID); |
| 388 | + |
| 389 | + List<Split> imbalanceSplits = mSplitsDbAdapter.getSplitsForTransactionInAccount(transaction.getUID(), imbalanceAcctUID); |
| 390 | + assertThat(imbalanceSplits).hasSize(1); |
| 391 | + |
| 392 | + Split split = imbalanceSplits.get(0); |
| 393 | + assertThat(split.getAmount().toPlainString()).isEqualTo("4.99"); |
| 394 | + assertThat(split.getType()).isEqualTo(TransactionType.CREDIT); |
| 395 | + |
| 396 | + } |
| 397 | + |
| 398 | + |
281 | 399 | private void setDoubleEntryEnabled(boolean enabled){ |
282 | 400 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); |
283 | 401 | Editor editor = prefs.edit(); |
@@ -346,34 +464,27 @@ public void testToggleTransactionType(){ |
346 | 464 | validateEditTransactionFields(mTransaction); |
347 | 465 |
|
348 | 466 | TransactionTypeToggleButton toggleButton = (TransactionTypeToggleButton) mSolo.getView(R.id.input_transaction_type); |
349 | | - assertThat(toggleButton).isVisible(); |
350 | | - |
351 | | - String label = toggleButton.getText().toString(); |
352 | | - assertTrue(mSolo.searchToggleButton(label)); |
353 | | - assertThat(getActivity().getString(R.string.label_receive)).isEqualTo(label); |
| 467 | + assertThat(toggleButton).isNotNull(); |
| 468 | + assertThat(toggleButton).isVisible(); |
| 469 | + assertThat(toggleButton).hasText(R.string.label_receive); |
354 | 470 |
|
355 | 471 | mSolo.clickOnView(toggleButton); |
356 | | - mSolo.sleep(3000); |
357 | | - String amountString = mSolo.getEditText(1).getText().toString(); |
358 | | - NumberFormat formatter = NumberFormat.getInstance(); |
359 | | - try { |
360 | | - amountString = formatter.parse(amountString).toString(); |
361 | | - } catch (ParseException e) { |
362 | | - e.printStackTrace(); |
363 | | - } |
364 | | - Money amount = new Money(amountString, Currency.getInstance(Locale.getDefault()).getCurrencyCode()); |
365 | | - assertThat("-9.99").isEqualTo(amount.toPlainString()); |
| 472 | + mSolo.sleep(2000); |
366 | 473 |
|
367 | | - clickSherlockActionBarItem(R.id.menu_save); |
| 474 | + assertThat(toggleButton).hasText(R.string.label_spend); |
| 475 | + EditText amountView = (EditText) mSolo.getView(R.id.input_transaction_amount); |
| 476 | + String amountString = amountView.getText().toString(); |
| 477 | + assertThat(amountString).startsWith("-"); |
| 478 | + assertThat("-9.99").isEqualTo(amountString); |
| 479 | + |
| 480 | + mSolo.clickOnActionBarItem(R.id.menu_save); |
368 | 481 | mSolo.waitForText(DUMMY_ACCOUNT_NAME); |
369 | 482 |
|
370 | 483 | List<Transaction> transactions = mTransactionsDbAdapter.getAllTransactionsForAccount(DUMMY_ACCOUNT_UID); |
371 | | - |
372 | | - assertEquals(1, transactions.size()); |
| 484 | + assertThat(transactions).hasSize(1); |
373 | 485 | Transaction trx = transactions.get(0); |
| 486 | + assertThat(trx.getSplits()).hasSize(2); //auto-balancing of splits |
374 | 487 | assertTrue(trx.getBalance(DUMMY_ACCOUNT_UID).isNegative()); |
375 | | - |
376 | | - mSolo.goBack(); |
377 | 488 | } |
378 | 489 |
|
379 | 490 | public void testOpenTransactionEditShouldNotModifyTransaction(){ |
|
0 commit comments