1/*
2 * Copyright 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
17package androidx.recyclerview.selection;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30@RunWith(AndroidJUnit4.class)
31@SmallTest
32public class OperationMonitorTest {
33
34    private OperationMonitor mMonitor;
35    private TestListener mListener;
36
37    @Before
38    public void setUp() {
39        mMonitor = new OperationMonitor();
40        mListener = new TestListener(mMonitor);
41    }
42
43    @Test
44    public void testNotStarted() {
45        assertFalse(mMonitor.isStarted());
46    }
47
48    @Test
49    public void testStarted() {
50        mMonitor.start();
51        assertTrue(mMonitor.isStarted());
52    }
53
54    @Test
55    public void testStopped() {
56        mMonitor.start();
57        mMonitor.stop();
58        assertFalse(mMonitor.isStarted());
59    }
60
61    @Test
62    public void testStartedCallsListener() {
63        mMonitor.addListener(mListener);
64        mMonitor.start();
65        mListener.assertLastState(true);
66        mMonitor.stop();
67        mListener.assertLastState(false);
68    }
69
70    @Test
71    public void testRemoveListener() {
72        mMonitor.addListener(mListener);
73        mMonitor.removeListener(mListener);
74        mMonitor.start();
75        mListener.assertLastState(false);
76    }
77
78    private static final class TestListener implements OperationMonitor.OnChangeListener {
79
80        private boolean mLastState;
81        private OperationMonitor mMonitor;
82
83        TestListener(OperationMonitor monitor) {
84            mMonitor = monitor;
85        }
86
87        @Override
88        public void onChanged() {
89            mLastState = mMonitor.isStarted();
90        }
91
92        void assertLastState(boolean expected) {
93            assertEquals(expected, mLastState);
94        }
95    }
96}
97