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 17 package com.android.documentsui.prefs; 18 19 import android.content.SharedPreferences; 20 21 import androidx.test.InstrumentationRegistry; 22 import androidx.test.filters.SmallTest; 23 import androidx.test.runner.AndroidJUnit4; 24 25 import com.android.documentsui.testing.TestConsumer; 26 27 import org.junit.After; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.concurrent.TimeUnit; 33 34 @RunWith(AndroidJUnit4.class) 35 @SmallTest 36 public class PreferencesMonitorTest { 37 38 private static final String LOCAL_PREFERENCE = "rootViewMode-testRoot"; 39 40 private SharedPreferences mPrefs; 41 private PreferencesMonitor mMonitor; 42 private TestConsumer<String> mConsumer; 43 44 @Before setUp()45 public void setUp() { 46 mConsumer = new TestConsumer<>(); 47 mPrefs = InstrumentationRegistry.getContext().getSharedPreferences("MonitorTest0", 0); 48 mMonitor = new PreferencesMonitor("Poodles", mPrefs, mConsumer); 49 mMonitor.start(); 50 } 51 52 @After tearDown()53 public void tearDown() { 54 mMonitor.stop(); 55 } 56 57 @Test testReportsChangesToListener()58 public void testReportsChangesToListener() throws Exception { 59 mPrefs.edit().putInt(LOCAL_PREFERENCE, 1).apply(); 60 // internally the monitor waits for notification of changes. 61 mConsumer.waitForCall(100, TimeUnit.MILLISECONDS); 62 mConsumer.assertLastArgument(LOCAL_PREFERENCE); 63 } 64 } 65