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