Merge "Move TwoActionPreference to car-ui-lib"
diff --git a/res/layout/two_action_preference.xml b/res/layout/two_action_preference.xml
deleted file mode 100644
index 6739a11..0000000
--- a/res/layout/two_action_preference.xml
+++ /dev/null
@@ -1,79 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-    Copyright 2018 The Android Open Source Project
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
--->
-
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:background="@android:color/transparent"
-    android:gravity="center_vertical"
-    android:minHeight="?android:attr/listPreferredItemHeightSmall">
-    <LinearLayout
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?android:attr/selectableItemBackground"
-        android:clipToPadding="false"
-        android:gravity="start|center_vertical"
-        android:paddingBottom="@*android:dimen/car_preference_row_vertical_margin"
-        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
-        android:paddingTop="@*android:dimen/car_preference_row_vertical_margin">
-        <androidx.preference.internal.PreferenceImageView
-            android:id="@android:id/icon"
-            android:layout_width="@dimen/car_ui_preference_icon_size"
-            android:layout_height="@dimen/car_ui_preference_icon_size"
-            android:layout_marginEnd="@dimen/car_ui_preference_icon_margin_end"/>
-        <LinearLayout
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_centerVertical="true"
-            android:orientation="vertical">
-            <TextView
-                android:id="@android:id/title"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:singleLine="true"
-                android:textAppearance="@style/TextAppearance.CarUi.PreferenceTitle"/>
-            <TextView
-                android:id="@android:id/summary"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:textAppearance="@style/TextAppearance.CarUi.PreferenceSummary"/>
-        </LinearLayout>
-    </LinearLayout>
-    <LinearLayout
-        android:id="@+id/action_widget_container"
-        android:layout_width="wrap_content"
-        android:layout_height="match_parent">
-        <View
-            android:layout_width="@dimen/two_action_preference_divider_width"
-            android:layout_height="match_parent"
-            android:layout_marginBottom="@*android:dimen/car_preference_row_vertical_margin"
-            android:layout_marginTop="@*android:dimen/car_preference_row_vertical_margin"
-            android:background="?attr/dividerColor"/>
-        <!-- Preference should place its actual preference widget here. -->
-        <FrameLayout
-            android:id="@android:id/widget_frame"
-            android:layout_width="wrap_content"
-            android:layout_height="match_parent"
-            android:background="?android:attr/selectableItemBackground"
-            android:minWidth="?android:attr/listPreferredItemHeightSmall"
-            android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-            android:paddingStart="?android:attr/listPreferredItemPaddingStart"/>
-    </LinearLayout>
-</LinearLayout>
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index c22af54..7b17cf4 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -55,11 +55,6 @@
         <attr name="controller" format="string"/>
     </declare-styleable>
 
-    <declare-styleable name="TwoActionPreference">
-        <!-- Determines if the secondary action is initially shown -->
-        <attr name="actionShown" format="boolean"/>
-    </declare-styleable>
-
     <declare-styleable name="ProgressBarPreference">
         <attr name="min" format="integer"/>
         <attr name="max" format="integer"/>
diff --git a/src/com/android/car/settings/common/ButtonPreference.java b/src/com/android/car/settings/common/ButtonPreference.java
index 28e4ad1..60682f3 100644
--- a/src/com/android/car/settings/common/ButtonPreference.java
+++ b/src/com/android/car/settings/common/ButtonPreference.java
@@ -22,6 +22,8 @@
 
 import androidx.preference.Preference;
 
+import com.android.car.ui.preference.CarUiTwoActionPreference;
+
 /**
  * {@link Preference} with a secondary clickable button on the side.
  * {@link #setLayoutResource(int)} or the {@code widgetLayout} resource may be used to specify
@@ -29,7 +31,7 @@
  *
  * <p>Note: the button is enabled even when {@link #isEnabled()} is {@code false}.
  */
-public class ButtonPreference extends TwoActionPreference {
+public class ButtonPreference extends CarUiTwoActionPreference {
 
     /**
      * Interface definition for a callback to be invoked when the button is clicked.
diff --git a/src/com/android/car/settings/common/MasterSwitchPreference.java b/src/com/android/car/settings/common/MasterSwitchPreference.java
index 2156cce..d66b03d 100644
--- a/src/com/android/car/settings/common/MasterSwitchPreference.java
+++ b/src/com/android/car/settings/common/MasterSwitchPreference.java
@@ -23,9 +23,10 @@
 import android.widget.Switch;
 
 import com.android.car.settings.R;
+import com.android.car.ui.preference.CarUiTwoActionPreference;
 
 /** A preference that can be clicked on one side and toggled on another. */
-public class MasterSwitchPreference extends TwoActionPreference {
+public class MasterSwitchPreference extends CarUiTwoActionPreference {
 
     /**
      * Interface definition for a callback to be invoked when the switch is toggled.
diff --git a/src/com/android/car/settings/common/TwoActionPreference.java b/src/com/android/car/settings/common/TwoActionPreference.java
deleted file mode 100644
index f5f4929..0000000
--- a/src/com/android/car/settings/common/TwoActionPreference.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.car.settings.common;
-
-import android.content.Context;
-import android.content.res.TypedArray;
-import android.util.AttributeSet;
-import android.view.View;
-
-import androidx.preference.PreferenceViewHolder;
-
-import com.android.car.settings.R;
-import com.android.car.ui.preference.CarUiPreference;
-
-/**
- * A preference which can perform two actions. The secondary action is shown by default.
- * {@link #showAction(boolean)} may be used to manually set the visibility of the action.
- */
-public abstract class TwoActionPreference extends CarUiPreference {
-
-    private boolean mIsActionShown;
-
-    public TwoActionPreference(Context context, AttributeSet attrs,
-            int defStyleAttr, int defStyleRes) {
-        super(context, attrs, defStyleAttr, defStyleRes);
-        init(attrs);
-    }
-
-    public TwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr) {
-        super(context, attrs, defStyleAttr);
-        init(attrs);
-    }
-
-    public TwoActionPreference(Context context, AttributeSet attrs) {
-        super(context, attrs);
-        init(attrs);
-    }
-
-    public TwoActionPreference(Context context) {
-        super(context);
-        init(/* attrs= */ null);
-    }
-
-    private void init(AttributeSet attrs) {
-        setLayoutResource(R.layout.two_action_preference);
-        TypedArray preferenceAttributes = getContext().obtainStyledAttributes(attrs,
-                R.styleable.TwoActionPreference);
-        mIsActionShown = preferenceAttributes.getBoolean(
-                R.styleable.TwoActionPreference_actionShown, true);
-        setShowChevron(false);
-    }
-
-    /**
-     * Sets whether the secondary action is visible in the preference.
-     *
-     * @param isShown {@code true} if the secondary action should be shown.
-     */
-    public void showAction(boolean isShown) {
-        mIsActionShown = isShown;
-        notifyChanged();
-    }
-
-    /** Returns {@code true} if action is shown. */
-    public boolean isActionShown() {
-        return mIsActionShown;
-    }
-
-    @Override
-    public void onBindViewHolder(PreferenceViewHolder holder) {
-        super.onBindViewHolder(holder);
-        View actionContainer = holder.findViewById(R.id.action_widget_container);
-        View widgetFrame = holder.findViewById(android.R.id.widget_frame);
-        if (mIsActionShown) {
-            actionContainer.setVisibility(View.VISIBLE);
-            onBindWidgetFrame(widgetFrame);
-        } else {
-            actionContainer.setVisibility(View.GONE);
-        }
-    }
-
-    /**
-     * Binds the created View for the second action.
-     *
-     * <p>This is a good place to set properties on any custom view.
-     *
-     * @param widgetFrame The widget frame which controls the 2nd action.
-     */
-    protected abstract void onBindWidgetFrame(View widgetFrame);
-}
diff --git a/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreference.java b/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreference.java
index a59c02c..c7ae2e5 100644
--- a/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreference.java
+++ b/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreference.java
@@ -21,7 +21,6 @@
 
 import androidx.preference.PreferenceViewHolder;
 
-import com.android.car.settings.R;
 import com.android.car.settings.common.PasswordEditTextPreference;
 
 /**
@@ -40,7 +39,7 @@
     }
 
     private void init() {
-        setLayoutResource(R.layout.two_action_preference);
+        setTwoActionLayout();
     }
 
     /**
@@ -61,13 +60,13 @@
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
-        View actionConatiner = holder.findViewById(R.id.action_widget_container);
+        View actionContainer = getWidgetActionContainer(holder);
         View widgetFrame = holder.findViewById(android.R.id.widget_frame);
         if (mIsButtonShown) {
-            actionConatiner.setVisibility(View.VISIBLE);
+            actionContainer.setVisibility(View.VISIBLE);
             widgetFrame.setOnClickListener(v -> performButtonClick());
         } else {
-            actionConatiner.setVisibility(View.GONE);
+            actionContainer.setVisibility(View.GONE);
         }
     }
 
diff --git a/tests/robotests/res/xml/two_action_preference_action_shown_not_specified.xml b/tests/robotests/res/xml/two_action_preference_action_shown_not_specified.xml
deleted file mode 100644
index bbf93fc..0000000
--- a/tests/robotests/res/xml/two_action_preference_action_shown_not_specified.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-    Copyright 2019 The Android Open Source Project
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
--->
-
-<PreferenceScreen
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:settings="http://schemas.android.com/apk/res-auto">
-    <!-- Note that action_shown is not specified -->
-    <com.android.car.settings.common.ButtonPreference
-        android:key="@string/tpk_two_action_preference"
-        android:widgetLayout="@layout/details_preference_widget"
-        settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController"/>
-</PreferenceScreen>
diff --git a/tests/robotests/res/xml/two_action_preference_hidden.xml b/tests/robotests/res/xml/two_action_preference_hidden.xml
deleted file mode 100644
index b703345..0000000
--- a/tests/robotests/res/xml/two_action_preference_hidden.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-    Copyright 2019 The Android Open Source Project
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
--->
-
-<PreferenceScreen
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:settings="http://schemas.android.com/apk/res-auto">
-    <com.android.car.settings.common.ButtonPreference
-        android:key="@string/tpk_two_action_preference"
-        android:widgetLayout="@layout/details_preference_widget"
-        settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController"
-        settings:actionShown="false"/>
-</PreferenceScreen>
diff --git a/tests/robotests/res/xml/two_action_preference_shown.xml b/tests/robotests/res/xml/two_action_preference_shown.xml
deleted file mode 100644
index 428519b..0000000
--- a/tests/robotests/res/xml/two_action_preference_shown.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-    Copyright 2019 The Android Open Source Project
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
--->
-
-<PreferenceScreen
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:settings="http://schemas.android.com/apk/res-auto">
-    <com.android.car.settings.common.ButtonPreference
-        android:key="@string/tpk_two_action_preference"
-        android:widgetLayout="@layout/details_preference_widget"
-        settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController"
-        settings:actionShown="true"/>
-</PreferenceScreen>
diff --git a/tests/robotests/src/com/android/car/settings/common/ButtonPreferenceTest.java b/tests/robotests/src/com/android/car/settings/common/ButtonPreferenceTest.java
index eaa2bd6..8ec3252 100644
--- a/tests/robotests/src/com/android/car/settings/common/ButtonPreferenceTest.java
+++ b/tests/robotests/src/com/android/car/settings/common/ButtonPreferenceTest.java
@@ -46,9 +46,9 @@
         Context context = RuntimeEnvironment.application;
         Context themedContext = new ContextThemeWrapper(context, R.style.CarSettingTheme);
 
-        View rootView = View.inflate(themedContext, R.layout.two_action_preference, null);
-        mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
         mButtonPreference = new ButtonPreference(context);
+        View rootView = View.inflate(themedContext, mButtonPreference.getLayoutResource(), null);
+        mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
     }
 
     @Test
diff --git a/tests/robotests/src/com/android/car/settings/common/MasterSwitchPreferenceTest.java b/tests/robotests/src/com/android/car/settings/common/MasterSwitchPreferenceTest.java
index 1862066..4bb51ff 100644
--- a/tests/robotests/src/com/android/car/settings/common/MasterSwitchPreferenceTest.java
+++ b/tests/robotests/src/com/android/car/settings/common/MasterSwitchPreferenceTest.java
@@ -52,12 +52,13 @@
         MockitoAnnotations.initMocks(this);
         Context context = RuntimeEnvironment.application;
         Context themedContext = new ContextThemeWrapper(context, R.style.CarSettingTheme);
-        View rootView = View.inflate(themedContext, R.layout.two_action_preference, null);
+
+        mMasterSwitchPreference = new MasterSwitchPreference(context);
+        View rootView = View.inflate(themedContext, mMasterSwitchPreference.getLayoutResource(),
+                null);
         View.inflate(themedContext, R.layout.master_switch_widget,
                 rootView.findViewById(android.R.id.widget_frame));
         mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
-        mMasterSwitchPreference = new MasterSwitchPreference(context);
-
         mMasterSwitchPreference.onBindViewHolder(mViewHolder);
         mMasterSwitchPreference.setSwitchChecked(false);
         mMasterSwitchPreference.setSwitchToggleListener(mListener);
diff --git a/tests/robotests/src/com/android/car/settings/common/TwoActionPreferenceTest.java b/tests/robotests/src/com/android/car/settings/common/TwoActionPreferenceTest.java
deleted file mode 100644
index ef9a68d..0000000
--- a/tests/robotests/src/com/android/car/settings/common/TwoActionPreferenceTest.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.car.settings.common;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import android.content.Context;
-import android.os.Bundle;
-import android.view.ContextThemeWrapper;
-import android.view.View;
-
-import androidx.annotation.XmlRes;
-import androidx.preference.PreferenceViewHolder;
-
-import com.android.car.settings.R;
-import com.android.car.settings.testutils.FragmentController;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-
-@RunWith(RobolectricTestRunner.class)
-public class TwoActionPreferenceTest {
-
-    private static class TestTwoActionPreference extends TwoActionPreference {
-
-        TestTwoActionPreference(Context context) {
-            super(context);
-        }
-
-        @Override
-        protected void onBindWidgetFrame(View actionContainer) {
-            // Intentionally empty.
-        }
-    }
-
-    private Context mContext;
-    private PreferenceViewHolder mViewHolder;
-    private TestTwoActionPreference mTestTwoActionPreference;
-
-    @Before
-    public void setUp() {
-        mContext = RuntimeEnvironment.application;
-        Context themedContext = new ContextThemeWrapper(mContext, R.style.CarSettingTheme);
-        View rootView = View.inflate(themedContext, R.layout.two_action_preference,
-                null);
-        mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
-        mTestTwoActionPreference = new TestTwoActionPreference(mContext);
-    }
-
-    @Test
-    public void showAction_true_buttonVisible() {
-        mTestTwoActionPreference.showAction(true);
-        mTestTwoActionPreference.onBindViewHolder(mViewHolder);
-
-        assertThat(mViewHolder.findViewById(
-                R.id.action_widget_container).getVisibility()).isEqualTo(View.VISIBLE);
-    }
-
-    @Test
-    public void showAction_false_buttonGone() {
-        mTestTwoActionPreference.showAction(false);
-        mTestTwoActionPreference.onBindViewHolder(mViewHolder);
-
-        assertThat(mViewHolder.findViewById(
-                R.id.action_widget_container).getVisibility()).isEqualTo(View.GONE);
-    }
-
-    @Test
-    public void isActionShown_true() {
-        mTestTwoActionPreference.showAction(true);
-        assertThat(mTestTwoActionPreference.isActionShown()).isTrue();
-    }
-
-    @Test
-    public void isActionShown_false() {
-        mTestTwoActionPreference.showAction(false);
-        assertThat(mTestTwoActionPreference.isActionShown()).isFalse();
-    }
-
-    @Test
-    public void preferenceConstructed_attrDefined_actionShown() {
-        TestSettingsFragment fragment = TestSettingsFragment.newInstance(
-                R.xml.two_action_preference_shown);
-        FragmentController.of(fragment).setup();
-
-        TwoActionPreference preference =
-                (TwoActionPreference) fragment.getPreferenceScreen().findPreference(
-                        mContext.getString(R.string.tpk_two_action_preference));
-
-        assertThat(preference.isActionShown()).isTrue();
-    }
-
-    @Test
-    public void preferenceConstructed_defaultValue_actionShown() {
-        TestSettingsFragment fragment = TestSettingsFragment.newInstance(
-                R.xml.two_action_preference_action_shown_not_specified);
-        FragmentController.of(fragment).setup();
-
-        TwoActionPreference preference =
-                (TwoActionPreference) fragment.getPreferenceScreen().findPreference(
-                        mContext.getString(R.string.tpk_two_action_preference));
-
-        assertThat(preference.isActionShown()).isTrue();
-    }
-
-    @Test
-    public void preferenceConstructed_attrDefined_actionHidden() {
-        TestSettingsFragment fragment = TestSettingsFragment.newInstance(
-                R.xml.two_action_preference_hidden);
-        FragmentController.of(fragment).setup();
-
-        TwoActionPreference preference =
-                (TwoActionPreference) fragment.getPreferenceScreen().findPreference(
-                        mContext.getString(R.string.tpk_two_action_preference));
-
-        assertThat(preference.isActionShown()).isFalse();
-    }
-
-    /**
-     * Test settings fragment which creates the xml screen provided via {@link #newInstance(int)}.
-     */
-    public static class TestSettingsFragment extends SettingsFragment {
-
-        private static final String ARG_SCREEN_RES = "arg_screen_res";
-
-        @XmlRes
-        private int mScreenResId;
-
-        public static TestSettingsFragment newInstance(@XmlRes int screenResId) {
-            Bundle arguments = new Bundle();
-            arguments.putInt(ARG_SCREEN_RES, screenResId);
-
-            TestSettingsFragment fragment = new TestSettingsFragment();
-            fragment.setArguments(arguments);
-            return fragment;
-        }
-
-        @Override
-        public void onAttach(Context context) {
-            super.onAttach(context);
-            mScreenResId = getArguments().getInt(ARG_SCREEN_RES);
-        }
-
-        @Override
-        @XmlRes
-        protected int getPreferenceScreenResId() {
-            return mScreenResId;
-        }
-    }
-}
diff --git a/tests/robotests/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreferenceTest.java b/tests/robotests/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreferenceTest.java
index 15fa064..a8d9297 100644
--- a/tests/robotests/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreferenceTest.java
+++ b/tests/robotests/src/com/android/car/settings/wifi/ButtonPasswordEditTextPreferenceTest.java
@@ -44,10 +44,10 @@
     public void setUp() {
         Context context = RuntimeEnvironment.application;
         Context themedContext = new ContextThemeWrapper(context, R.style.CarSettingTheme);
-        View rootView = View.inflate(themedContext, R.layout.two_action_preference, /* root= */
-                null);
+
+        mButtonPreference = new ButtonPasswordEditTextPreference(context);
+        View rootView = View.inflate(themedContext, mButtonPreference.getLayoutResource(), null);
         mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
-        mButtonPreference = new ButtonPasswordEditTextPreference(RuntimeEnvironment.application);
     }
 
     @Test
@@ -58,7 +58,6 @@
         mButtonPreference.setOnButtonClickListener(listener);
 
         mViewHolder.findViewById(android.R.id.widget_frame).performClick();
-
         verify(listener).onButtonClick(mButtonPreference);
     }