Test cases for PreferenceGroup, Preference, PreferenceFragment

Change-Id: Ib00a7824dc2acb0da84a1230ae2cc63be11d82c2
diff --git a/tests/tests/preference2/AndroidManifest.xml b/tests/tests/preference2/AndroidManifest.xml
index 5043e1f..964a698 100644
--- a/tests/tests/preference2/AndroidManifest.xml
+++ b/tests/tests/preference2/AndroidManifest.xml
@@ -30,6 +30,7 @@
         </activity>
         <activity android:name="android.preference2.cts.PreferencesFromXml" />
         <activity android:name="android.preference2.cts.PreferenceWithHeaders" />
+        <activity android:name="android.preference2.cts.FragmentPreferences" />
     </application>
 
     <!-- This is a self-instrumenting test package. -->
diff --git a/tests/tests/preference2/res/values/strings.xml b/tests/tests/preference2/res/values/strings.xml
index d43d3eb..2559456 100755
--- a/tests/tests/preference2/res/values/strings.xml
+++ b/tests/tests/preference2/res/values/strings.xml
@@ -103,4 +103,9 @@
 
     <string name="cust_title_dialog_preference">Custom Dialog preference</string>
     <string name="cust_dialogtitle_dialog_preference">Custom Dialog Title</string>
+
+    <string name="preference_group_title">Preference Group Title</string>
+    <string name="custom_preference_group_title">Custom Preference Group</string>
+
+    <string name="custom_preference">Custom Preference</string>
 </resources>
diff --git a/tests/tests/preference2/src/android/preference2/cts/CustomPreference.java b/tests/tests/preference2/src/android/preference2/cts/CustomPreference.java
new file mode 100644
index 0000000..7506bd0
--- /dev/null
+++ b/tests/tests/preference2/src/android/preference2/cts/CustomPreference.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2012 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.preference2.cts;
+
+import com.android.cts.preference2.R;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
+import android.os.Parcelable;
+import android.preference.Preference;
+import android.preference.PreferenceManager;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+
+public class CustomPreference extends Preference {
+    protected boolean mOnPrepareCalled;
+
+    public CustomPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        init(attrs);
+    }
+
+    public CustomPreference(Context context) {
+        this(context, null, 0);
+    }
+
+    public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        init(attrs);
+    }
+
+    private void init(AttributeSet attrs) {
+        TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CustPref);
+        setTitle(a.getString(R.styleable.CustPref_title));
+        setIcon(a.getDrawable(R.styleable.CustPref_icon));
+    }
+    
+    @Override
+    protected boolean callChangeListener(Object newValue) {
+        return super.callChangeListener(newValue);
+    }
+
+    @Override
+    protected Preference findPreferenceInHierarchy(String key) {
+        return super.findPreferenceInHierarchy(key);
+    }
+
+    @Override
+    protected boolean getPersistedBoolean(boolean defaultReturnValue) {
+        return super.getPersistedBoolean(defaultReturnValue);
+    }
+
+    @Override
+    protected float getPersistedFloat(float defaultReturnValue) {
+        return super.getPersistedFloat(defaultReturnValue);
+    }
+
+    @Override
+    protected int getPersistedInt(int defaultReturnValue) { 
+        return super.getPersistedInt(defaultReturnValue);
+    }
+
+    @Override
+    protected long getPersistedLong(long defaultReturnValue) {
+        return super.getPersistedLong(defaultReturnValue);
+    }
+
+    @Override
+    protected String getPersistedString(String defaultReturnValue) {
+        return super.getPersistedString(defaultReturnValue);
+    }
+
+    @Override
+    protected void notifyChanged() {
+        super.notifyChanged();
+    }
+
+    @Override
+    protected void notifyHierarchyChanged() {
+        super.notifyHierarchyChanged();
+    }
+
+    @Override
+    protected void onAttachedToActivity() {
+        super.onAttachedToActivity();
+    }
+
+    @Override
+    protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
+        super.onAttachedToHierarchy(preferenceManager);
+    }
+
+    @Override
+    protected void onBindView(View view) {
+        super.onBindView(view);
+    }
+
+    @Override
+    protected void onClick() {
+        super.onClick();
+    }
+
+    @Override
+    protected View onCreateView(ViewGroup parent) {
+        return super.onCreateView(parent);
+    }
+
+    @Override
+    protected Object onGetDefaultValue(TypedArray a, int index) {
+        return super.onGetDefaultValue(a, index);
+    }
+
+    @Override
+    protected void onPrepareForRemoval() {
+        super.onPrepareForRemoval();
+    }
+
+    @Override
+    protected void onRestoreInstanceState(Parcelable state) {
+       
+        super.onRestoreInstanceState(state);
+    }
+
+    @Override
+    protected Parcelable onSaveInstanceState() {
+        return super.onSaveInstanceState();
+    }
+
+    @Override
+    protected void onSetInitialValue(boolean restorePersistedValue,
+            Object defaultValue) {
+        super.onSetInitialValue(restorePersistedValue, defaultValue);
+    }
+
+    @Override
+    protected boolean persistBoolean(boolean value) {
+        return super.persistBoolean(value);
+    }
+
+    @Override
+    protected boolean persistFloat(float value) {
+        return super.persistFloat(value);
+    }
+
+    @Override
+    protected boolean persistInt(int value) {
+        return super.persistInt(value);
+    }
+
+    @Override
+    protected boolean persistLong(long value) {
+        return super.persistLong(value);
+    }
+
+    @Override
+    protected boolean persistString(String value) {
+        return super.persistString(value);
+    }
+
+    @Override
+    protected boolean shouldPersist() {  
+        return super.shouldPersist();
+    } 
+}
+
diff --git a/tests/tests/preference2/src/android/preference2/cts/CustomPreferenceGroup.java b/tests/tests/preference2/src/android/preference2/cts/CustomPreferenceGroup.java
new file mode 100644
index 0000000..26e1bcf
--- /dev/null
+++ b/tests/tests/preference2/src/android/preference2/cts/CustomPreferenceGroup.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 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.preference2.cts;
+
+import com.android.cts.preference2.R;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
+import android.preference.Preference;
+import android.preference.PreferenceGroup;
+import android.util.AttributeSet;
+
+public class CustomPreferenceGroup extends PreferenceGroup {
+    protected boolean mOnPrepareCalled;
+
+    public CustomPreferenceGroup(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        init(attrs);
+    }
+
+    public CustomPreferenceGroup(Context context) {
+        super(context, null, 0);
+    }
+
+    public CustomPreferenceGroup(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        init(attrs);
+    }
+
+    private void init(AttributeSet attrs) {
+        TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CustPref);
+        setTitle(a.getString(R.styleable.CustPref_title));
+        setIcon(a.getDrawable(R.styleable.CustPref_icon));
+    }
+
+    public boolean isOnSameScreenAsChildren() {
+        return super.isOnSameScreenAsChildren();
+    }
+
+    public boolean onPrepareAddPreference (Preference preference) {
+        mOnPrepareCalled = true;
+        return super.onPrepareAddPreference(preference);
+    }
+}
diff --git a/tests/tests/preference2/src/android/preference2/cts/FragmentPreferences.java b/tests/tests/preference2/src/android/preference2/cts/FragmentPreferences.java
new file mode 100644
index 0000000..dacc192
--- /dev/null
+++ b/tests/tests/preference2/src/android/preference2/cts/FragmentPreferences.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2012 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.preference2.cts;
+
+import com.android.cts.preference2.R;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.preference.PreferenceFragment;
+
+/**
+ * Demonstration of PreferenceFragment, showing a single fragment in an
+ * activity.
+ */
+public class FragmentPreferences extends Activity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        getFragmentManager().beginTransaction().replace(android.R.id.content,
+                new PrefsFragment()).commit();
+    }
+
+    public static class PrefsFragment extends PreferenceFragment {
+        @Override
+        public void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+            addPreferencesFromResource(R.xml.preferences);
+        }
+    }
+}
diff --git a/tests/tests/preference2/src/android/preference2/cts/FragmentPreferencesTest.java b/tests/tests/preference2/src/android/preference2/cts/FragmentPreferencesTest.java
new file mode 100644
index 0000000..0bef2f13
--- /dev/null
+++ b/tests/tests/preference2/src/android/preference2/cts/FragmentPreferencesTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2012 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.preference2.cts;
+
+import android.preference.Preference;
+import android.preference.PreferenceManager;
+import android.preference.PreferenceScreen;
+import android.preference2.cts.FragmentPreferences.PrefsFragment;
+import android.test.ActivityInstrumentationTestCase2;
+
+public class FragmentPreferencesTest
+        extends ActivityInstrumentationTestCase2<FragmentPreferences> {
+
+    private FragmentPreferences mActivity;
+    private PrefsFragment mPrefsFragment;
+
+    public FragmentPreferencesTest() {
+        super(FragmentPreferences.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mActivity = getActivity();
+        mPrefsFragment = (PrefsFragment) mActivity.getFragmentManager().
+                findFragmentById(android.R.id.content);
+    }
+
+    public void testGetPreferenceManager() {
+        PreferenceManager prefManager = mPrefsFragment.getPreferenceManager();
+        assertNotNull(prefManager);
+    }
+
+    public void testGetPreferenceScreen() {
+        PreferenceScreen prefScreen = mPrefsFragment.getPreferenceScreen();
+        assertNotNull(prefScreen);
+    }
+
+    public void testFindPreference() {
+        Preference pref = mPrefsFragment.findPreference("checkbox_preference");
+        assertNotNull(pref);
+    }
+
+    public void testSetPreferenceScreen() {
+        mPrefsFragment.setPreferenceScreen(null);
+        assertNull(mPrefsFragment.getPreferenceScreen());
+    }
+}
diff --git a/tests/tests/preference2/src/android/preference2/cts/PreferenceFromCodeActivity.java b/tests/tests/preference2/src/android/preference2/cts/PreferenceFromCodeActivity.java
index 94be86f..da7fcaa 100755
--- a/tests/tests/preference2/src/android/preference2/cts/PreferenceFromCodeActivity.java
+++ b/tests/tests/preference2/src/android/preference2/cts/PreferenceFromCodeActivity.java
@@ -25,6 +25,7 @@
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceCategory;
+import android.preference.PreferenceGroup;
 import android.preference.PreferenceScreen;
 import android.preference.SwitchPreference;
 
@@ -111,5 +112,15 @@
         listPref.setTitle(R.string.title_list_preference);
         listPref.setSummary(R.string.summary_list_preference);
         dialogBasedPrefCat.addPreference(listPref);
+
+        PreferenceGroup prefGroup = new PreferenceCategory(this);
+        prefGroup.setTitle(R.string.preference_group_title);
+        prefGroup.setKey("pref-group");
+        mPrefScreen.addPreference(prefGroup);
+
+        CustomPreferenceGroup customPrefGroup = new CustomPreferenceGroup(this);
+        customPrefGroup.setTitle(R.string.custom_preference_group_title);
+        customPrefGroup.setKey("custom-pref-group");
+        mPrefScreen.addPreference(customPrefGroup);
     }
 }
diff --git a/tests/tests/preference2/src/android/preference2/cts/PreferenceGroupTest.java b/tests/tests/preference2/src/android/preference2/cts/PreferenceGroupTest.java
new file mode 100644
index 0000000..109ea77
--- /dev/null
+++ b/tests/tests/preference2/src/android/preference2/cts/PreferenceGroupTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2012 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.preference2.cts;
+
+import com.android.cts.preference2.R;
+
+import android.preference.CheckBoxPreference;
+import android.preference.EditTextPreference;
+import android.preference.Preference;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceManager;
+import android.preference.PreferenceScreen;
+import android.preference2.cts.PreferenceFragmentActivity.PrefFragment;
+import android.test.ActivityInstrumentationTestCase2;
+
+public class PreferenceGroupTest
+        extends ActivityInstrumentationTestCase2<PreferenceFromCodeActivity> {
+
+    private PreferenceFromCodeActivity mActivity;
+    private PreferenceGroup mPreferenceGroup;
+
+    public PreferenceGroupTest() {
+        super(PreferenceFromCodeActivity.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mActivity = getActivity();
+        mPreferenceGroup = (PreferenceGroup) mActivity.findPreference("pref-group");
+        CheckBoxPreference checkboxPref = new CheckBoxPreference(mActivity);
+        checkboxPref.setKey("checkbox_preference_group");
+        checkboxPref.setIcon(R.drawable.ic_launcher);
+        checkboxPref.setTitle(R.string.title_checkbox_preference);
+        checkboxPref.setSummary(R.string.summary_checkbox_preference);
+        checkboxPref.setSummaryOn(R.string.summary_on_checkbox_preference);
+        checkboxPref.setSummaryOff(R.string.summary_off_checkbox_preference);
+        checkboxPref.setChecked(true);
+        mPreferenceGroup.addPreference(checkboxPref);
+    }
+
+    public void testAddPreference() {
+        Preference preference = mActivity.findPreference("pref-group");
+        assertNotNull(preference);
+    }
+
+    public void testFindPreference() {
+        Preference preference = mPreferenceGroup.findPreference("checkbox_preference_group");
+        assertNotNull(preference);
+    }
+
+    public void testPreferenceCount() {
+        assertEquals(1, mPreferenceGroup.getPreferenceCount());
+    }
+
+    public void testSetOrderingAsAdded() {
+        mPreferenceGroup.setOrderingAsAdded(false);
+        assertFalse(mPreferenceGroup.isOrderingAsAdded());
+    }
+
+    public void testRemoveAll() {
+        mPreferenceGroup.removeAll();
+        assertEquals(0, mPreferenceGroup.getPreferenceCount());
+    }
+
+    public void testRemovePreference() {
+        Preference preference = mPreferenceGroup.findPreference("checkbox_preference_group");
+        mPreferenceGroup.removePreference(preference);
+        assertEquals(0, mPreferenceGroup.getPreferenceCount());
+    }
+
+    public void testIsOnSameScreenAsChildren() {
+        CustomPreferenceGroup mCustomPreferenceGroup = (CustomPreferenceGroup) 
+                mActivity.findPreference("custom-pref-group");
+        assertTrue(mCustomPreferenceGroup.isOnSameScreenAsChildren());
+    }
+
+    public void testOnPrepareAddPreference( ) {
+        CustomPreferenceGroup mCustomPreferenceGroup = (CustomPreferenceGroup) 
+                mActivity.findPreference("custom-pref-group");
+        CheckBoxPreference checkboxPref = new CheckBoxPreference(mActivity);
+        checkboxPref.setKey("checkbox_preference-2");
+        mCustomPreferenceGroup.addPreference(checkboxPref);
+        assertTrue(mCustomPreferenceGroup.mOnPrepareCalled);
+    }
+}
diff --git a/tests/tests/preference2/src/android/preference2/cts/PreferenceTest.java b/tests/tests/preference2/src/android/preference2/cts/PreferenceTest.java
index ae3ef0a..328e074 100755
--- a/tests/tests/preference2/src/android/preference2/cts/PreferenceTest.java
+++ b/tests/tests/preference2/src/android/preference2/cts/PreferenceTest.java
@@ -19,6 +19,7 @@
 import android.graphics.drawable.Drawable;
 import android.preference.CheckBoxPreference;
 import android.preference.Preference;
+import android.preference.PreferenceGroup;
 import android.test.ActivityInstrumentationTestCase2;
 
 import com.android.cts.preference2.R;
@@ -177,5 +178,106 @@
         });
         assertFalse(mPreference.getShouldDisableView());
     }
+
+    public void testPersistInt() {
+        CustomPreference mCustomPreference = new CustomPreference(mActivity);
+        String key = "" + Math.random();
+        mCustomPreference.setKey(key);
+        PreferenceGroup mPreferenceGroup = (PreferenceGroup) mActivity.findPreference(
+                "pref-group");
+        mPreferenceGroup.addPreference(mCustomPreference);
+        try {
+            int expected = 1;
+            mCustomPreference.persistInt(expected);
+            int actual = mCustomPreference.getPersistedInt(0);
+            assertEquals(expected, actual);
+        } finally {
+            mPreferenceGroup.removePreference(mCustomPreference);
+        }
+    }
+
+    public void testPersistBoolean() {
+        CustomPreference mCustomPreference = new CustomPreference(mActivity);
+        String key = "" + Math.random();
+        mCustomPreference.setKey(key);
+        PreferenceGroup mPreferenceGroup = (PreferenceGroup) mActivity.findPreference(
+                "pref-group");
+        mPreferenceGroup.addPreference(mCustomPreference);
+        try {
+            boolean expected = true;
+            boolean result = mCustomPreference.persistBoolean(expected);
+            assertTrue(result);
+            boolean actual = mCustomPreference.getPersistedBoolean(false);
+            assertEquals(expected, actual);
+        } finally {
+            mPreferenceGroup.removePreference(mCustomPreference);
+        }
+    }
+
+    public void testPersistString() {
+        CustomPreference mCustomPreference = new CustomPreference(mActivity);
+        String key = "" + Math.random();
+        mCustomPreference.setKey(key);
+        PreferenceGroup mPreferenceGroup = (PreferenceGroup) mActivity.findPreference(
+                "pref-group");
+        mPreferenceGroup.addPreference(mCustomPreference);
+        try {
+            String expected = "a";
+            boolean result = mCustomPreference.persistString(expected);
+            assertTrue(result);
+            String actual = mCustomPreference.getPersistedString("b");
+            assertEquals(expected, actual);
+        } finally {
+            mPreferenceGroup.removePreference(mCustomPreference);
+        }
+    }
+
+    public void testPersistFloat() {
+        CustomPreference mCustomPreference = new CustomPreference(mActivity);
+        String key = "" + Math.random();
+        mCustomPreference.setKey(key);
+        PreferenceGroup mPreferenceGroup = (PreferenceGroup) mActivity.findPreference(
+                "pref-group");
+        mPreferenceGroup.addPreference(mCustomPreference);
+        try {
+            float expected = 9.999f;
+            boolean result = mCustomPreference.persistFloat(expected);
+            assertTrue(result);
+            float actual = mCustomPreference.getPersistedFloat(0.000f);
+            assertEquals(expected, actual);
+        } finally {
+            mPreferenceGroup.removePreference(mCustomPreference);
+        }
+    }
+
+    public void testPersistLong() {
+        CustomPreference mCustomPreference = new CustomPreference(mActivity);
+        String key = "" + Math.random();
+        mCustomPreference.setKey(key);
+        PreferenceGroup mPreferenceGroup = (PreferenceGroup) mActivity.findPreference(
+        "pref-group");
+        mPreferenceGroup.addPreference(mCustomPreference);
+        try {
+            long expected = 99999999l;
+            boolean result = mCustomPreference.persistLong(expected);
+            assertTrue(result);
+            long actual = mCustomPreference.getPersistedLong(10000000l);
+            assertEquals(expected, actual);
+        } finally {
+            mPreferenceGroup.removePreference(mCustomPreference);
+        }
+    }
+
+    public void testShouldCommit() {
+        CustomPreference mCustomPreference = (CustomPreference) mActivity.findPreference(
+                "custom-preference");
+        assertTrue(mCustomPreference.shouldCommit());
+    }
+
+    public void testShouldPersist() {
+        CustomPreference mCustomPreference = (CustomPreference) mActivity.findPreference(
+                "custom-preference");
+        assertTrue(mCustomPreference.shouldPersist());
+    }
 }