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 android.support.v7.preference.tests;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertNull;
21
22import android.content.Context;
23import android.support.test.InstrumentationRegistry;
24import android.support.test.annotation.UiThreadTest;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27import android.support.v7.preference.CheckBoxPreference;
28import android.support.v7.preference.PreferenceCategory;
29import android.support.v7.preference.PreferenceManager;
30import android.support.v7.preference.PreferenceScreen;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36@RunWith(AndroidJUnit4.class)
37@SmallTest
38public class PreferenceParentGroupTest {
39
40    private Context mContext;
41
42    @Before
43    public void setup() throws Exception {
44        mContext = InstrumentationRegistry.getTargetContext();
45    }
46
47    /**
48     * Tests that parent PreferenceGroup is correctly assigned and removed when creating preferences
49     * from code.
50     */
51    @Test
52    @UiThreadTest
53    public void parentAddRemoveTest() {
54        PreferenceManager manager = new PreferenceManager(mContext);
55
56        PreferenceScreen screen = manager.createPreferenceScreen(mContext);
57        assertNull(screen.getParent());
58
59        PreferenceCategory category = new PreferenceCategory(mContext);
60        assertNull(category.getParent());
61
62        CheckBoxPreference pref = new CheckBoxPreference(mContext);
63        assertNull(pref.getParent());
64
65        screen.addPreference(category);
66        assertEquals(screen, category.getParent());
67
68        category.addPreference(pref);
69        assertEquals(category, pref.getParent());
70
71        screen.removePreference(category);
72        assertNull(category.getParent());
73
74        category.removePreference(pref);
75        assertNull(pref.getParent());
76    }
77
78    /**
79     * Tests that parent PreferenceGroup is correctly maintained during reassignment.
80     */
81    @Test
82    @UiThreadTest
83    public void parentReassignTest() {
84        PreferenceManager manager = new PreferenceManager(mContext);
85
86        PreferenceScreen screen = manager.createPreferenceScreen(mContext);
87
88        PreferenceCategory category1 = new PreferenceCategory(mContext);
89        screen.addPreference(category1);
90        PreferenceCategory category2 = new PreferenceCategory(mContext);
91        screen.addPreference(category2);
92
93        CheckBoxPreference pref = new CheckBoxPreference(mContext);
94        assertNull(pref.getParent());
95
96        category1.addPreference(pref);
97        assertEquals(category1, pref.getParent());
98
99        category1.removePreference(pref);
100        category2.addPreference(pref);
101        assertEquals(category2, pref.getParent());
102    }
103
104    /**
105     * Adds a preference into two different groups without removing it first. This is maybe not
106     * something we want to support in the future but this makes this behavior visible.
107     */
108    @Test
109    @UiThreadTest
110    public void parentDoubleAddTest() throws InterruptedException {
111        PreferenceManager manager = new PreferenceManager(mContext);
112
113        PreferenceScreen screen = manager.createPreferenceScreen(mContext);
114
115        PreferenceCategory category1 = new PreferenceCategory(mContext);
116        screen.addPreference(category1);
117        PreferenceCategory category2 = new PreferenceCategory(mContext);
118        screen.addPreference(category2);
119
120        CheckBoxPreference pref = new CheckBoxPreference(mContext);
121        assertNull(pref.getParent());
122
123        category1.addPreference(pref);
124        category2.addPreference(pref);
125
126        assertEquals(category2, pref.getParent());
127    }
128}
129