-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix widget not refreshing on auto updates
Before this change: * when the banks were auto updated * and the bank's balance change was lower than the notification threshold ... then the widget wasn't updated. With this change in place the widget is actually updated even in this case. This change also adds unit testing of the auto update data retrieval code.
- Loading branch information
Showing
5 changed files
with
178 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
app/src/test/java/com/liato/bankdroid/appwidget/DataRetrieverTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.liato.bankdroid.appwidget; | ||
|
||
import com.liato.bankdroid.banking.Account; | ||
import com.liato.bankdroid.banking.Bank; | ||
import com.liato.bankdroid.db.DBAdapter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.support.annotation.NonNull; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DataRetrieverTaskTest { | ||
private static class TestableBank extends Bank { | ||
private final int balanceBeforeUpdate; | ||
private final int balanceAfterUpdate; | ||
private boolean hasUpdated = false; | ||
|
||
public TestableBank(int balanceBeforeUpdate, int balanceAfterUpdate) { | ||
super(Mockito.mock(Context.class)); | ||
|
||
this.balanceBeforeUpdate = balanceBeforeUpdate; | ||
this.balanceAfterUpdate = balanceAfterUpdate; | ||
} | ||
|
||
@Override | ||
public void update() { | ||
hasUpdated = true; | ||
} | ||
|
||
@Override | ||
public ArrayList<Account> getAccounts() { | ||
int balance; | ||
if (hasUpdated) { | ||
balance = balanceAfterUpdate; | ||
} else { | ||
balance = balanceBeforeUpdate; | ||
} | ||
|
||
Account account = Mockito.mock(Account.class); | ||
Mockito.when(account.getBalance()).thenReturn(BigDecimal.valueOf(balance)); | ||
|
||
ArrayList<Account> accounts = new ArrayList<>(); | ||
accounts.add(account); | ||
|
||
return accounts; | ||
} | ||
|
||
@Override | ||
public BigDecimal getBalance() { | ||
return getAccounts().get(0).getBalance(); | ||
} | ||
} | ||
|
||
private static class TestableDataRetrieverTask extends AutoRefreshService.DataRetrieverTask { | ||
private final Bank bank; | ||
private boolean hasRefreshedWidget = false; | ||
|
||
private TestableDataRetrieverTask( | ||
AutoRefreshService autoRefreshService, SharedPreferences prefs, Bank bank) { | ||
super(autoRefreshService, prefs); | ||
|
||
this.bank = bank; | ||
} | ||
|
||
@Override | ||
protected List<Bank> getBanks() { | ||
List<Bank> returnMe = new ArrayList<>(); | ||
returnMe.add(bank); | ||
return returnMe; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
protected DBAdapter getDBAdapter() { | ||
return Mockito.mock(DBAdapter.class); | ||
} | ||
|
||
@Override | ||
protected void sendWidgetRefresh() { | ||
hasRefreshedWidget = true; | ||
} | ||
} | ||
|
||
@Test | ||
public void testIncreaseLessThanNotificationThreshold() throws Exception { | ||
AutoRefreshService autoRefreshService = Mockito.mock(AutoRefreshService.class); | ||
|
||
SharedPreferences prefs = Mockito.mock(SharedPreferences.class); | ||
Mockito.when(prefs.getString("notify_min_delta", "0")).thenReturn("300"); | ||
|
||
TestableDataRetrieverTask testMe = | ||
new TestableDataRetrieverTask(autoRefreshService, prefs, new TestableBank(100, 200)); | ||
testMe.doInBackground(); | ||
|
||
Assert.assertTrue("Widget should have been refreshed", testMe.hasRefreshedWidget); | ||
} | ||
|
||
@Test | ||
public void testNoChange() throws Exception { | ||
AutoRefreshService autoRefreshService = Mockito.mock(AutoRefreshService.class); | ||
|
||
SharedPreferences prefs = Mockito.mock(SharedPreferences.class); | ||
Mockito.when(prefs.getString("notify_min_delta", "0")).thenReturn("0"); | ||
|
||
TestableDataRetrieverTask testMe = | ||
new TestableDataRetrieverTask(autoRefreshService, prefs, new TestableBank(100, 100)); | ||
testMe.doInBackground(); | ||
|
||
Assert.assertFalse("Widget shouldn't have been refreshed", testMe.hasRefreshedWidget); | ||
} | ||
} |