Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae50661c authored by Chuck Liao's avatar Chuck Liao
Browse files

Add and modify tests for ClockManager.

Increase the code coverage of ClockManager to 100%.

Change-Id: I0242c8583f75754092541d4607a02aa46cbea25e
parent dc51fc56
Loading
Loading
Loading
Loading
+46 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import static junit.framework.TestCase.fail;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.provider.Settings.Secure;
@@ -43,11 +44,14 @@ public class ClockManagerTest {

    private static final String CLOCK_ID = "id";
    private static final String CLOCK_FIELD = "clock";
    private static final String CLOCK_FACE_SETTING = "fake_clock_face_setting";

    @Mock ClockProvider mProvider;
    @Mock ThemesUserEventLogger mLogger;
    private ContentResolver mContentResolver;
    private ClockManager mManager;
    @Mock private Clockface mMockClockface;
    @Mock private Callback mMockCallback;

    @Before
    public void setUp() {
@@ -81,10 +85,48 @@ public class ClockManagerTest {
    }

    @Test
    public void testGetCurrentClock() {
        // GIVEN that secure settings contains a clock id
        Secure.putString(mContentResolver, ClockManager.CLOCK_FACE_SETTING, CLOCK_ID);
        // THEN the current clock is that id
    public void testApply_whenJSONExceptionOccurs_callsOnError() {
        when(mMockClockface.getId()).thenThrow(JSONException.class);

        mManager.apply(mMockClockface, mMockCallback);

        verify(mMockCallback).onError(null);
    }

    @Test
    public void testGetCurrentClock_returnsClockId() throws JSONException {
        // Secure settings contains a clock id
        JSONObject json = new JSONObject().put(CLOCK_FIELD, CLOCK_ID);
        Secure.putString(mContentResolver, ClockManager.CLOCK_FACE_SETTING, json.toString());

        // The current clock is that id
        assertEquals(CLOCK_ID, mManager.getCurrentClock());
    }

    @Test
    public void testGetCurrentClock_whenJSONExceptionOccurs_returnsClockFaceSetting() {
        // Secure settings contains a clock face setting with invalid format to cause JSONException.
        Secure.putString(mContentResolver, ClockManager.CLOCK_FACE_SETTING, CLOCK_FACE_SETTING);

        // The current clock is the clock face setting which is saved in secure settings.
        assertEquals(CLOCK_FACE_SETTING, mManager.getCurrentClock());
    }

    @Test
    public void testGetCurrentClock_withNullIdInSecureSettings_returnsNullId() {
        // Secure settings contains a null clock id
        Secure.putString(mContentResolver, ClockManager.CLOCK_FACE_SETTING, /* value= */ null);

        // The current clock is null
        assertEquals(null, mManager.getCurrentClock());
    }

    @Test
    public void testGetCurrentClock_withEmptyIdInSecureSettings_returnsEmptyId() {
        // Secure settings contains an empty clock id
        Secure.putString(mContentResolver, ClockManager.CLOCK_FACE_SETTING, /* value= */ "");

        // The current clock is empty
        assertEquals("", mManager.getCurrentClock());
    }
}