• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.applications;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 
23 import android.content.Context;
24 import android.support.v7.preference.Preference.OnPreferenceClickListener;
25 import android.support.v7.preference.PreferenceViewHolder;
26 import android.view.LayoutInflater;
27 
28 import com.android.settings.R;
29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RuntimeEnvironment;
35 
36 @RunWith(SettingsRobolectricTestRunner.class)
37 public class LayoutPreferenceTest {
38 
39     private Context mContext;
40     private LayoutPreference mPreference;
41     private PreferenceViewHolder mHolder;
42 
43     @Before
setUp()44     public void setUp() {
45         mContext = RuntimeEnvironment.application;
46         mPreference = new LayoutPreference(mContext, R.layout.two_action_buttons);
47         mHolder = PreferenceViewHolder.createInstanceForTests(LayoutInflater.from(mContext)
48                 .inflate(R.layout.layout_preference_frame, null, false));
49     }
50 
51     @Test
setOnClickListener_shouldAttachToRootView()52     public void setOnClickListener_shouldAttachToRootView() {
53         final OnPreferenceClickListener listener = mock(OnPreferenceClickListener.class);
54 
55         mPreference.setOnPreferenceClickListener(listener);
56         mPreference.onBindViewHolder(mHolder);
57 
58         mHolder.itemView.callOnClick();
59 
60         verify(listener).onPreferenceClick(mPreference);
61         assertThat(mHolder.itemView.isFocusable()).isTrue();
62         assertThat(mHolder.itemView.isClickable()).isTrue();
63     }
64 
65     @Test
setNonSelectable_viewShouldNotBeSelectable()66     public void setNonSelectable_viewShouldNotBeSelectable() {
67         mPreference.setSelectable(false);
68         mPreference.onBindViewHolder(mHolder);
69 
70         assertThat(mHolder.itemView.isFocusable()).isFalse();
71         assertThat(mHolder.itemView.isClickable()).isFalse();
72     }
73 
74     @Test
disableSomeView_shouldMaintainStateAfterBind()75     public void disableSomeView_shouldMaintainStateAfterBind() {
76         mPreference.findViewById(R.id.button1_positive).setEnabled(false);
77         mPreference.findViewById(R.id.button2_positive).setEnabled(true);
78 
79         mPreference.onBindViewHolder(mHolder);
80 
81         assertThat(mPreference.findViewById(R.id.button1_positive).isEnabled()).isFalse();
82         assertThat(mPreference.findViewById(R.id.button2_positive).isEnabled()).isTrue();
83     }
84 }
85