Search in sources :

Example 6 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class TrayProviderHelperTest method testQueryFails.

public void testQueryFails() throws Exception {
    Uri uri = new TrayUri(getProviderMockContext()).get();
    MockContentProvider mockContentProvider = new MockContentProvider(getProviderMockContext()) {

        @Override
        public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
            throw new IllegalStateException("something serious is wrong");
        }
    };
    getProviderMockContext().addProvider(uri.getAuthority(), mockContentProvider);
    getProviderMockContext().enableMockResolver(true);
    try {
        mProviderHelper.queryProvider(getUri(MODULE_A, KEY_A));
        fail("did not throw");
    } catch (TrayException e) {
        e.getCause().getMessage().equals("something serious is wrong");
    }
    final List<TrayItem> trayItems = mProviderHelper.queryProviderSafe(getUri(MODULE_A, KEY_A));
    assertEquals(0, trayItems.size());
}
Also used : TrayException(net.grandcentrix.tray.core.TrayException) TrayItem(net.grandcentrix.tray.core.TrayItem) MockContentProvider(android.test.mock.MockContentProvider) Uri(android.net.Uri)

Example 7 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class TrayProviderHelperTest method testUpdateChanges.

public void testUpdateChanges() throws Exception {
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_A));
    final List<TrayItem> list = mProviderHelper.queryProvider(getUri(MODULE_A, KEY_A));
    assertNotNull(list);
    assertEquals(1, list.size());
    TrayItem itemA = list.get(0);
    assertNotNull(itemA.created());
    assertNotNull(itemA.updateTime());
    Thread.sleep(10);
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_B));
    final List<TrayItem> list2 = mProviderHelper.queryProvider(getUri(MODULE_A, KEY_A));
    assertNotNull(list2);
    assertEquals(1, list2.size());
    TrayItem itemB = list2.get(0);
    //Log.v("", "diff: " + (itemA.updateTime().getTime() - itemB.updateTime().getTime()));
    assertNotSame(itemA.updateTime().getTime(), itemB.updateTime().getTime());
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem)

Example 8 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class ReadDifferentFormat method testReadNullStringAsWrongType.

public void testReadNullStringAsWrongType() throws Exception {
    assertEquals("default", mPref.getString(KEY, "default"));
    mPref.put(KEY, null);
    final TrayItem pref = mPref.getPref(KEY);
    assertNotNull(pref);
    assertEquals(null, pref.value());
    assertEquals(null, mPref.getString(KEY));
    assertEquals(false, mPref.getBoolean(KEY));
    try {
        assertEquals(0.0f, mPref.getFloat(KEY));
        fail();
    } catch (WrongTypeException e) {
        assertTrue(e.getMessage().contains("Float"));
        assertTrue(e.getMessage().contains("null"));
    }
    try {
        assertEquals(0l, mPref.getLong(KEY));
        fail();
    } catch (WrongTypeException e) {
        assertTrue(e.getMessage().contains("Long"));
        assertTrue(e.getMessage().contains("null"));
    }
    try {
        assertEquals(0, mPref.getInt(KEY));
        fail();
    } catch (WrongTypeException e) {
        assertTrue(e.getMessage().contains("Integer"));
        assertTrue(e.getMessage().contains("null"));
    }
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) WrongTypeException(net.grandcentrix.tray.core.WrongTypeException)

Example 9 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class TrayItemTest method testToString.

public void testToString() throws Exception {
    SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy", Locale.US);
    final Date created = new Date();
    final Date updated = new Date();
    final TrayItem item = new TrayItem("module", "key", "migratedKey", "value", created, updated);
    final String string = item.toString();
    assertTrue(string.contains(item.key()));
    assertTrue(string.contains(item.value()));
    assertTrue(string.contains(item.migratedKey()));
    assertTrue(string.contains(item.module()));
    assertTrue(string.contains(sf.format(item.updateTime())));
    assertTrue(string.contains(sf.format(item.created())));
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 10 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class TrayItemTest method testValues.

public void testValues() throws Exception {
    final Date created = new Date();
    final Date updated = new Date();
    final TrayItem item = new TrayItem("module", "key", "migratedKey", "value", created, updated);
    assertEquals("key", item.key());
    assertEquals("value", item.value());
    assertEquals("migratedKey", item.migratedKey());
    assertEquals("module", item.module());
    assertEquals(updated, item.updateTime());
    assertEquals(created, item.created());
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) Date(java.util.Date)

Aggregations

TrayItem (net.grandcentrix.tray.core.TrayItem)26 Date (java.util.Date)5 Uri (android.net.Uri)3 ArrayList (java.util.ArrayList)3 NonNull (android.support.annotation.NonNull)2 TrayException (net.grandcentrix.tray.core.TrayException)2 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 MatrixCursor (android.database.MatrixCursor)1 HandlerThread (android.os.HandlerThread)1 Nullable (android.support.annotation.Nullable)1 MockContentProvider (android.test.mock.MockContentProvider)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OnTrayPreferenceChangeListener (net.grandcentrix.tray.core.OnTrayPreferenceChangeListener)1 TrayRuntimeException (net.grandcentrix.tray.core.TrayRuntimeException)1 WrongTypeException (net.grandcentrix.tray.core.WrongTypeException)1 TestTrayModulePreferences (net.grandcentrix.tray.mock.TestTrayModulePreferences)1