Implement PropertiesStorage

This commit is contained in:
2017-07-23 20:07:44 -04:00
parent fdcb9daadc
commit 87f1d635d8
4 changed files with 321 additions and 2 deletions

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2015-2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.preferences;
import android.support.annotation.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.models.*;
import org.junit.*;
import org.mockito.*;
import java.io.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
public class PreferencesTest extends BaseUnitTest
{
@NonNull
private Preferences prefs;
@Mock
private Preferences.Listener listener;
private PropertiesStorage storage;
@Override
@Before
public void setUp() throws Exception
{
super.setUp();
File file = File.createTempFile("prefs", ".properties");
file.deleteOnExit();
storage = new PropertiesStorage(file);
prefs = new Preferences(storage);
prefs.addListener(listener);
}
@Test
public void testHabitColor() throws Exception
{
assertThat(prefs.getDefaultHabitColor(999), equalTo(999));
prefs.setDefaultHabitColor(10);
assertThat(prefs.getDefaultHabitColor(999), equalTo(10));
}
@Test
public void testDefaultOrder() throws Exception
{
assertThat(prefs.getDefaultOrder(), equalTo(HabitList.Order.BY_POSITION));
prefs.setDefaultOrder(HabitList.Order.BY_SCORE);
assertThat(prefs.getDefaultOrder(), equalTo(HabitList.Order.BY_SCORE));
storage.putString("pref_default_order", "BOGUS");
assertThat(prefs.getDefaultOrder(), equalTo(HabitList.Order.BY_POSITION));
assertThat(storage.getString("pref_default_order", ""), equalTo("BY_POSITION"));
}
@Test
public void testDefaultSpinnerPosition() throws Exception
{
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(1));
prefs.setDefaultScoreSpinnerPosition(4);
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(4));
storage.putInt("pref_score_view_interval", 9000);
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(1));
assertThat(storage.getInt("pref_score_view_interval", 0), equalTo(1));
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2015-2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.preferences;
import org.isoron.uhabits.core.*;
import org.junit.*;
import java.io.*;
import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertTrue;
public class PropertiesStorageTest extends BaseUnitTest
{
private PropertiesStorage storage;
private File file;
@Override
@Before
public void setUp() throws Exception
{
super.setUp();
file = File.createTempFile("test", ".properties");
file.deleteOnExit();
storage = new PropertiesStorage(file);
}
@Test
public void testPutGetRemove() throws Exception
{
storage.putBoolean("booleanKey", true);
assertTrue(storage.getBoolean("booleanKey", false));
assertFalse(storage.getBoolean("random", false));
storage.putInt("intKey", 64);
assertThat(storage.getInt("intKey", 200), equalTo(64));
assertThat(storage.getInt("random", 200), equalTo(200));
storage.putLong("longKey", 64L);
assertThat(storage.getLong("intKey", 200L), equalTo(64L));
assertThat(storage.getLong("random", 200L), equalTo(200L));
storage.putString("stringKey", "Hello");
assertThat(storage.getString("stringKey", ""), equalTo("Hello"));
assertThat(storage.getString("random", ""), equalTo(""));
storage.remove("stringKey");
assertThat(storage.getString("stringKey", ""), equalTo(""));
storage.clear();
assertThat(storage.getLong("intKey", 200L), equalTo(200L));
assertFalse(storage.getBoolean("booleanKey", false));
}
@Test
public void testPersistence() throws Exception
{
storage.putBoolean("booleanKey", true);
storage.putInt("intKey", 64);
storage.putLong("longKey", 64L);
storage.putString("stringKey", "Hello");
PropertiesStorage storage2 = new PropertiesStorage(file);
assertTrue(storage2.getBoolean("booleanKey", false));
assertThat(storage2.getInt("intKey", 200), equalTo(64));
assertThat(storage2.getLong("intKey", 200L), equalTo(64L));
assertThat(storage2.getString("stringKey", ""), equalTo("Hello"));
}
}