OSDN Git Service

dfa8ba118e42c2d6674d027762d15a5dd6794f02
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / display / darkmode / DarkModeObserverTest.java
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14
15 package com.android.settings.display.darkmode;
16
17 import android.content.Context;
18 import android.database.ContentObserver;
19 import android.net.Uri;
20 import android.provider.Settings;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.robolectric.RobolectricTestRunner;
27 import org.robolectric.RuntimeEnvironment;
28
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.times;
32
33
34 @RunWith(RobolectricTestRunner.class)
35 public class DarkModeObserverTest {
36     private Context mContext;
37     private ContentObserver mContentObserver;
38     private DarkModeObserver mDarkModeObserver;
39     private Runnable mCallback;
40     private Uri mUri = Settings.Secure.getUriFor(Settings.Secure.UI_NIGHT_MODE);
41
42     @Before
43     public void setUp() {
44         MockitoAnnotations.initMocks(this);
45         mContext = spy(RuntimeEnvironment.application);
46         mDarkModeObserver = new DarkModeObserver(mContext);
47         mContentObserver = mDarkModeObserver.getContentObserver();
48         mCallback = mock(Runnable.class);
49     }
50
51     @Test
52     public void callbackTest_subscribedCallbackCalled() {
53         mDarkModeObserver.subscribe(mCallback);
54         mContentObserver.onChange(false, mUri);
55         Mockito.verify(mCallback, times(2)).run();
56     }
57
58     @Test
59     public void callbackTest_unsubscribedCallNotbackCalled() {
60         mDarkModeObserver.subscribe(mCallback);
61         mContentObserver.onChange(false, mUri);
62         mDarkModeObserver.unsubscribe();
63         mContentObserver.onChange(false, mUri);
64         Mockito.verify(mCallback, times(2)).run();
65     }
66 }