Merge "Fix issue for Preference.setSingleLineTitle() has no effect." into oc-support-26.0-dev
diff --git a/v7/preference/src/android/support/v7/preference/Preference.java b/v7/preference/src/android/support/v7/preference/Preference.java
index 6b302fd..cfc4311 100644
--- a/v7/preference/src/android/support/v7/preference/Preference.java
+++ b/v7/preference/src/android/support/v7/preference/Preference.java
@@ -999,6 +999,7 @@
* @attr ref R.styleable#Preference_android_singleLineTitle
*/
public void setSingleLineTitle(boolean singleLineTitle) {
+ mHasSingleLineTitleAttr = true;
mSingleLineTitle = singleLineTitle;
}
diff --git a/v7/preference/tests/src/android/support/v7/preference/tests/PreferenceSingleLineTitleTest.java b/v7/preference/tests/src/android/support/v7/preference/tests/PreferenceSingleLineTitleTest.java
new file mode 100644
index 0000000..6c1cf5f
--- /dev/null
+++ b/v7/preference/tests/src/android/support/v7/preference/tests/PreferenceSingleLineTitleTest.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2017 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 android.support.v7.preference.tests;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceViewHolder;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class PreferenceSingleLineTitleTest {
+
+ private Preference mPreference;
+
+ @Mock
+ private ViewGroup mViewGroup;
+ @Mock
+ private TextView mTitleView;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ when(mViewGroup.findViewById(android.R.id.title)).thenReturn(mTitleView);
+
+ mPreference = new Preference(InstrumentationRegistry.getTargetContext());
+ mPreference.setTitle("Test Title");
+ }
+
+ @Test
+ public void bindViewHolder_singleLineTitleNotSet_shouldNotSetSingleLine() {
+ PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mViewGroup);
+ mPreference.onBindViewHolder(holder);
+
+ verify(mTitleView, never()).setSingleLine(anyBoolean());
+ }
+
+ @Test
+ public void bindViewHolder_singleLineTitleSetToTrue_shouldSetSingleLineToTrue() {
+ PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mViewGroup);
+ mPreference.setSingleLineTitle(true);
+ mPreference.onBindViewHolder(holder);
+
+ verify(mTitleView).setSingleLine(true);
+ }
+
+ @Test
+ public void bindViewHolder_singleLineTitleSetToFalse_shouldSetSingleLineToFalse() {
+ PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mViewGroup);
+ mPreference.setSingleLineTitle(false);
+ mPreference.onBindViewHolder(holder);
+
+ verify(mTitleView).setSingleLine(false);
+ }
+
+}