1 /*
2  * Copyright (C) 2019 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.settings.gestures;
18 
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 
22 import android.content.Context;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.widget.LinearLayout;
26 import android.widget.SeekBar;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.settings.widget.LabeledSeekBarPreference;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class LabeledSeekBarPreferenceTest {
42 
43     private Context mContext;
44     private SeekBar mSeekBar;
45     private LabeledSeekBarPreference mSeekBarPreference;
46 
47     @Mock
48     private Preference.OnPreferenceChangeListener mListener;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53 
54         mContext = RuntimeEnvironment.application;
55         mSeekBarPreference = new LabeledSeekBarPreference(mContext, null);
56         LayoutInflater inflater = LayoutInflater.from(mContext);
57         final View view =
58                 inflater.inflate(mSeekBarPreference.getLayoutResource(),
59                         new LinearLayout(mContext), false);
60         mSeekBar = view.findViewById(com.android.internal.R.id.seekbar);
61     }
62 
63     @Test
seekBarPreferenceOnStopTrackingTouch_callsListener()64     public void seekBarPreferenceOnStopTrackingTouch_callsListener() {
65         mSeekBar.setProgress(2);
66 
67         mSeekBarPreference.setOnPreferenceChangeStopListener(mListener);
68         mSeekBarPreference.onStopTrackingTouch(mSeekBar);
69 
70         verify(mListener, times(1)).onPreferenceChange(mSeekBarPreference, 2);
71     }
72 }
73