Implement tests for Preferences

pull/316/head^2
Alinson S. Xavier 8 years ago
parent 281861cac5
commit 864636705d

@ -104,7 +104,7 @@ public class BaseAndroidTest extends TestCase
logger = appComponent.getHabitsLogger(); logger = appComponent.getHabitsLogger();
modelFactory = appComponent.getModelFactory(); modelFactory = appComponent.getModelFactory();
prefs.reset(); prefs.clear();
fixtures = new HabitFixtures(modelFactory, habitList); fixtures = new HabitFixtures(modelFactory, habitList);
fixtures.purgeHabits(appComponent.getHabitList()); fixtures.purgeHabits(appComponent.getHabitList());

@ -84,7 +84,7 @@ public class BaseUserInterfaceTest
private void resetState() throws Exception private void resetState() throws Exception
{ {
prefs.reset(); prefs.clear();
prefs.setFirstRun(false); prefs.setFirstRun(false);
prefs.updateLastHint(100, DateUtils.getToday()); prefs.updateLastHint(100, DateUtils.getToday());
habitList.removeAll(); habitList.removeAll();

@ -28,6 +28,10 @@ import java.util.*;
public class Preferences public class Preferences
{ {
public static final String DEFAULT_SYNC_SERVER =
"https://sync.loophabits.org";
@NonNull @NonNull
private final Storage storage; private final Storage storage;
@ -138,13 +142,12 @@ public class Preferences
public long getSnoozeInterval() public long getSnoozeInterval()
{ {
return Long.parseLong(storage.getString("pref_snooze_interval", "15")); return storage.getLong("pref_snooze_interval", 15L);
} }
public String getSyncAddress() public String getSyncAddress()
{ {
return storage.getString("pref_sync_address", return storage.getString("pref_sync_address", DEFAULT_SYNC_SERVER);
"https://sync.loophabits.org");
} }
public void setSyncAddress(String address) public void setSyncAddress(String address)
@ -160,6 +163,7 @@ public class Preferences
id = UUID.randomUUID().toString(); id = UUID.randomUUID().toString();
storage.putString("pref_sync_client_id", id); storage.putString("pref_sync_client_id", id);
return id; return id;
} }
@ -180,8 +184,12 @@ public class Preferences
public void incrementLaunchCount() public void incrementLaunchCount()
{ {
int count = storage.getInt("launch_count", 0); storage.putInt("launch_count", getLaunchCount() + 1);
storage.putInt("launch_count", count + 1); }
public int getLaunchCount()
{
return storage.getInt("launch_count", 0);
} }
public boolean isDeveloper() public boolean isDeveloper()
@ -234,7 +242,7 @@ public class Preferences
listeners.remove(listener); listeners.remove(listener);
} }
public void reset() public void clear()
{ {
storage.clear(); storage.clear();
} }
@ -251,7 +259,7 @@ public class Preferences
public void setNotificationsSticky(boolean sticky) public void setNotificationsSticky(boolean sticky)
{ {
storage.getBoolean("pref_sticky_notifications", sticky); storage.putBoolean("pref_sticky_notifications", sticky);
for (Listener l : listeners) l.onNotificationsChanged(); for (Listener l : listeners) l.onNotificationsChanged();
} }
@ -287,6 +295,32 @@ public class Preferences
storage.putLong("last_hint_timestamp", timestamp.getUnixTime()); storage.putLong("last_hint_timestamp", timestamp.getUnixTime());
} }
public void setSyncKey(String key)
{
storage.putString("pref_sync_key", key);
for(Listener l : listeners) l.onSyncFeatureChanged();
}
public void setPureBlackEnabled(boolean enabled)
{
storage.putBoolean("pref_pure_black", enabled);
}
public int getLastAppVersion()
{
return storage.getInt("last_version", 0);
}
public void setSnoozeInterval(int interval)
{
storage.putLong("pref_snooze_interval", interval);
}
public void setNumericalHabitsFeatureEnabled(boolean enabled)
{
storage.putBoolean("pref_feature_numerical_habits", enabled);
}
public interface Listener public interface Listener
{ {
default void onCheckmarkSequenceChanged() {} default void onCheckmarkSequenceChanged() {}

@ -23,6 +23,7 @@ import android.support.annotation.*;
import org.isoron.uhabits.core.*; import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.*;
import org.junit.*; import org.junit.*;
import org.mockito.*; import org.mockito.*;
@ -30,6 +31,8 @@ import java.io.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class PreferencesTest extends BaseUnitTest public class PreferencesTest extends BaseUnitTest
{ {
@ -53,6 +56,14 @@ public class PreferencesTest extends BaseUnitTest
prefs.addListener(listener); prefs.addListener(listener);
} }
@Test
public void testClear() throws Exception
{
prefs.setDefaultHabitColor(99);
prefs.clear();
assertThat(prefs.getDefaultHabitColor(0), equalTo(0));
}
@Test @Test
public void testHabitColor() throws Exception public void testHabitColor() throws Exception
{ {
@ -86,4 +97,127 @@ public class PreferencesTest extends BaseUnitTest
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(1)); assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(1));
assertThat(storage.getInt("pref_score_view_interval", 0), equalTo(1)); assertThat(storage.getInt("pref_score_view_interval", 0), equalTo(1));
} }
@Test
public void testLastHint() throws Exception
{
assertThat(prefs.getLastHintNumber(), equalTo(-1));
assertNull(prefs.getLastHintTimestamp());
prefs.updateLastHint(34, Timestamp.ZERO.plus(100));
assertThat(prefs.getLastHintNumber(), equalTo(34));
assertThat(prefs.getLastHintTimestamp(), equalTo(Timestamp.ZERO.plus(100)));
}
@Test
public void testSync() throws Exception
{
assertThat(prefs.getLastSync(), equalTo(0L));
prefs.setLastSync(100);
assertThat(prefs.getLastSync(), equalTo(100L));
assertThat(prefs.getSyncAddress(),
equalTo(Preferences.DEFAULT_SYNC_SERVER));
prefs.setSyncAddress("example");
assertThat(prefs.getSyncAddress(), equalTo("example"));
verify(listener).onSyncFeatureChanged();
reset(listener);
assertThat(prefs.getSyncKey(), equalTo(""));
prefs.setSyncKey("123");
assertThat(prefs.getSyncKey(), equalTo("123"));
verify(listener).onSyncFeatureChanged();
reset(listener);
assertFalse(prefs.isSyncEnabled());
prefs.setSyncEnabled(true);
assertTrue(prefs.isSyncEnabled());
verify(listener).onSyncFeatureChanged();
reset(listener);
String id = prefs.getSyncClientId();
assertFalse(id.isEmpty());
assertThat(prefs.getSyncClientId(), equalTo(id));
}
@Test
public void testTheme() throws Exception
{
assertThat(prefs.getTheme(), equalTo(ThemeSwitcher.THEME_LIGHT));
prefs.setTheme(ThemeSwitcher.THEME_DARK);
assertThat(prefs.getTheme(), equalTo(ThemeSwitcher.THEME_DARK));
assertFalse(prefs.isPureBlackEnabled());
prefs.setPureBlackEnabled(true);
assertTrue(prefs.isPureBlackEnabled());
}
@Test
public void testNotifications() throws Exception
{
assertFalse(prefs.shouldMakeNotificationsSticky());
prefs.setNotificationsSticky(true);
assertTrue(prefs.shouldMakeNotificationsSticky());
assertThat(prefs.getSnoozeInterval(), equalTo(15L));
prefs.setSnoozeInterval(30);
assertThat(prefs.getSnoozeInterval(), equalTo(30L));
}
@Test
public void testAppVersionAndLaunch() throws Exception
{
assertThat(prefs.getLastAppVersion(), equalTo(0));
prefs.setLastAppVersion(23);
assertThat(prefs.getLastAppVersion(), equalTo(23));
assertTrue(prefs.isFirstRun());
prefs.setFirstRun(false);
assertFalse(prefs.isFirstRun());
assertThat(prefs.getLaunchCount(), equalTo(0));
prefs.incrementLaunchCount();
assertThat(prefs.getLaunchCount(), equalTo(1));
}
@Test
public void testCheckmarks() throws Exception
{
assertFalse(prefs.isCheckmarkSequenceReversed());
prefs.setCheckmarkSequenceReversed(true);
assertTrue(prefs.isCheckmarkSequenceReversed());
assertFalse(prefs.isShortToggleEnabled());
prefs.setShortToggleEnabled(true);
assertTrue(prefs.isShortToggleEnabled());
}
@Test
public void testNumericalHabits() throws Exception
{
assertFalse(prefs.isNumericalHabitsFeatureEnabled());
prefs.setNumericalHabitsFeatureEnabled(true);
assertTrue(prefs.isNumericalHabitsFeatureEnabled());
}
@Test
public void testDeveloper() throws Exception
{
assertFalse(prefs.isDeveloper());
prefs.setDeveloper(true);
assertTrue(prefs.isDeveloper());
}
@Test
public void testFiltering() throws Exception
{
assertFalse(prefs.getShowArchived());
assertTrue(prefs.getShowCompleted());
prefs.setShowArchived(true);
prefs.setShowCompleted(false);
assertTrue(prefs.getShowArchived());
assertFalse(prefs.getShowCompleted());
}
} }

Loading…
Cancel
Save