1 /*
2  * Copyright (C) 2019 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 package com.android.customization.model.clock;
17 
18 import static junit.framework.TestCase.assertTrue;
19 import static junit.framework.TestCase.fail;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Matchers.anyBoolean;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import androidx.annotation.Nullable;
28 
29 import com.android.customization.model.CustomizationManager.Callback;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class BaseClockManagerTest {
40 
41     private static final String CURRENT_CLOCK = "current_clock";
42 
43     @Mock ClockProvider mProvider;
44     private TestClockManager mManager;
45 
46     @Before
setUp()47     public void setUp() {
48         MockitoAnnotations.initMocks(this);
49         mManager = new TestClockManager(mProvider);
50     }
51 
52     @Test
testIsAvailable()53     public void testIsAvailable() {
54         // GIVEN that the ClockProvider is available
55         when(mProvider.isAvailable()).thenReturn(true);
56         // THEN the BaseClockManager is true
57         assertTrue(mManager.isAvailable());
58     }
59 
60     @Test
testApply()61     public void testApply() {
62         final String id = "id";
63         Clockface clock = new Clockface.Builder().setId(id).build();
64 
65         mManager.apply(clock, new Callback() {
66             @Override
67             public void onSuccess() {
68                 //Nothing to do here, the test passed
69             }
70             @Override
71             public void onError(@Nullable Throwable throwable) {
72                 fail("onError was called when grid had been applied successfully");
73             }
74         });
75 
76         assertEquals(id, mManager.getClockId());
77     }
78 
79     @Test
testFetch()80     public void testFetch() {
81         mManager.fetchOptions(null, false);
82         verify(mProvider).fetch(eq(null), anyBoolean());
83     }
84 
85     /**
86      * Testable BaseClockManager that provides basic implementations of abstract methods.
87      */
88     private static final class TestClockManager extends BaseClockManager {
89 
90         private String mClockId;
91 
TestClockManager(ClockProvider provider)92         TestClockManager(ClockProvider provider) {
93             super(provider);
94         }
95 
getClockId()96         String getClockId() {
97             return mClockId;
98         }
99 
100         @Override
handleApply(Clockface option, Callback callback)101         protected void handleApply(Clockface option, Callback callback) {
102             mClockId = option.getId();
103             callback.onSuccess();
104         }
105 
106         @Override
lookUpCurrentClock()107         protected String lookUpCurrentClock() {
108             return CURRENT_CLOCK;
109         }
110     }
111 }
112