2 * Copyright (C) 2019 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.android.settings.display.darkmode;
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;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.times;
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);
44 MockitoAnnotations.initMocks(this);
45 mContext = spy(RuntimeEnvironment.application);
46 mDarkModeObserver = new DarkModeObserver(mContext);
47 mContentObserver = mDarkModeObserver.getContentObserver();
48 mCallback = mock(Runnable.class);
52 public void callbackTest_subscribedCallbackCalled() {
53 mDarkModeObserver.subscribe(mCallback);
54 mContentObserver.onChange(false, mUri);
55 Mockito.verify(mCallback, times(2)).run();
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();