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 import android.view.View;
28 
29 import com.android.settings.R;
30 import com.android.settings.SettingsRobolectricTestRunner;
31 import com.android.settings.TestConfig;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.annotation.Config;
38 
39 @RunWith(SettingsRobolectricTestRunner.class)
40 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
41 public class LayoutPreferenceTest {
42 
43     private Context mContext;
44     private LayoutPreference mPreference;
45     private View mRootView;
46     private PreferenceViewHolder mHolder;
47 
48     @Before
setUp()49     public void setUp() {
50         mContext = RuntimeEnvironment.application;
51         mPreference = new LayoutPreference(mContext, R.layout.app_action_buttons);
52         mRootView = mPreference.mRootView;
53         mHolder = PreferenceViewHolder.createInstanceForTests(LayoutInflater.from(mContext)
54                 .inflate(R.layout.layout_preference_frame, null, false));
55     }
56 
57     @Test
setOnClickListener_shouldAttachToRootView()58     public void setOnClickListener_shouldAttachToRootView() {
59         final OnPreferenceClickListener listener = mock(OnPreferenceClickListener.class);
60 
61         mPreference.setOnPreferenceClickListener(listener);
62         mPreference.onBindViewHolder(mHolder);
63 
64         mHolder.itemView.callOnClick();
65 
66         verify(listener).onPreferenceClick(mPreference);
67         assertThat(mHolder.itemView.isFocusable()).isTrue();
68         assertThat(mHolder.itemView.isClickable()).isTrue();
69     }
70 
71     @Test
setNonSelectable_viewShouldNotBeSelectable()72     public void setNonSelectable_viewShouldNotBeSelectable() {
73         mPreference.setSelectable(false);
74         mPreference.onBindViewHolder(mHolder);
75 
76         assertThat(mHolder.itemView.isFocusable()).isFalse();
77         assertThat(mHolder.itemView.isClickable()).isFalse();
78     }
79 
80     @Test
disableSomeView_shouldMaintainStateAfterBind()81     public void disableSomeView_shouldMaintainStateAfterBind() {
82         mPreference.findViewById(R.id.left_button).setEnabled(false);
83         mPreference.findViewById(R.id.right_button).setEnabled(true);
84 
85         mPreference.onBindViewHolder(mHolder);
86 
87         assertThat(mPreference.findViewById(R.id.left_button).isEnabled()).isFalse();
88         assertThat(mPreference.findViewById(R.id.right_button).isEnabled()).isTrue();
89     }
90 }
91