1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.wear.widget;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.support.test.annotation.UiThreadTest;
25import android.support.test.filters.LargeTest;
26import android.support.test.filters.MediumTest;
27import android.support.test.runner.AndroidJUnit4;
28
29import androidx.swiperefreshlayout.widget.CircularProgressDrawable;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.Mock;
35
36import java.util.concurrent.TimeUnit;
37
38@MediumTest
39@RunWith(AndroidJUnit4.class)
40public class CircularProgressLayoutControllerTest {
41
42    private static final long TOTAL_TIME = TimeUnit.SECONDS.toMillis(1);
43    private static final long UPDATE_INTERVAL = TimeUnit.MILLISECONDS.toMillis(30);
44
45    private CircularProgressLayoutController mControllerUnderTest;
46
47    @Mock
48    CircularProgressDrawable mMockDrawable;
49    @Mock
50    CircularProgressLayout mMockLayout;
51    @Mock
52    CircularProgressLayout.OnTimerFinishedListener mMockListener;
53
54    @Before
55    public void setUp() {
56        mMockDrawable = mock(CircularProgressDrawable.class);
57        mMockLayout = mock(CircularProgressLayout.class);
58        mMockListener = mock(CircularProgressLayout.OnTimerFinishedListener.class);
59        when(mMockLayout.getProgressDrawable()).thenReturn(mMockDrawable);
60        when(mMockLayout.getOnTimerFinishedListener()).thenReturn(mMockListener);
61        mControllerUnderTest = new CircularProgressLayoutController(mMockLayout);
62    }
63
64    @Test
65    public void testSetIndeterminate() {
66        mControllerUnderTest.setIndeterminate(true);
67
68        assertEquals(true, mControllerUnderTest.isIndeterminate());
69        verify(mMockDrawable).start();
70    }
71
72    @Test
73    public void testIsIndeterminateAfterSetToFalse() {
74        mControllerUnderTest.setIndeterminate(true);
75        mControllerUnderTest.setIndeterminate(false);
76
77        assertEquals(false, mControllerUnderTest.isIndeterminate());
78        verify(mMockDrawable).stop();
79    }
80
81    @LargeTest
82    @Test
83    @UiThreadTest
84    public void testIsTimerRunningAfterStart() {
85        mControllerUnderTest.startTimer(TOTAL_TIME, UPDATE_INTERVAL);
86
87        assertEquals(true, mControllerUnderTest.isTimerRunning());
88    }
89
90    @Test
91    @UiThreadTest
92    public void testIsTimerRunningAfterStop() {
93        mControllerUnderTest.startTimer(TOTAL_TIME, UPDATE_INTERVAL);
94        mControllerUnderTest.stopTimer();
95
96        assertEquals(false, mControllerUnderTest.isTimerRunning());
97    }
98
99    @Test
100    @UiThreadTest
101    public void testSwitchFromIndeterminateToDeterminate() {
102        mControllerUnderTest.setIndeterminate(true);
103        mControllerUnderTest.startTimer(TOTAL_TIME, UPDATE_INTERVAL);
104
105        assertEquals(false, mControllerUnderTest.isIndeterminate());
106        assertEquals(true, mControllerUnderTest.isTimerRunning());
107        verify(mMockDrawable).stop();
108    }
109
110    @Test
111    @UiThreadTest
112    public void testSwitchFromDeterminateToIndeterminate() {
113        mControllerUnderTest.startTimer(TOTAL_TIME, UPDATE_INTERVAL);
114        mControllerUnderTest.setIndeterminate(true);
115
116        assertEquals(true, mControllerUnderTest.isIndeterminate());
117        assertEquals(false, mControllerUnderTest.isTimerRunning());
118        verify(mMockDrawable).start();
119    }
120}
121