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
17package com.android.settings.applications;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.verify;
22
23import android.content.Context;
24import android.support.v7.preference.Preference.OnPreferenceClickListener;
25import android.support.v7.preference.PreferenceViewHolder;
26import android.view.LayoutInflater;
27import android.view.View;
28
29import com.android.settings.R;
30import com.android.settings.testutils.SettingsRobolectricTestRunner;
31import com.android.settings.TestConfig;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.robolectric.RuntimeEnvironment;
37import org.robolectric.annotation.Config;
38
39@RunWith(SettingsRobolectricTestRunner.class)
40@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
41public class LayoutPreferenceTest {
42
43    private Context mContext;
44    private LayoutPreference mPreference;
45    private View mRootView;
46    private PreferenceViewHolder mHolder;
47
48    @Before
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
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
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
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