OSDN Git Service

Fix unit test for PreferenceController
[android-x86/packages-apps-Settings.git] / tests / unit / src / com / android / settings / core / PreferenceControllerContractTest.java
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
17 package com.android.settings.core;
18
19 import static junit.framework.Assert.fail;
20
21 import android.content.Context;
22 import android.platform.test.annotations.Presubmit;
23 import android.provider.SearchIndexableResource;
24 import android.support.test.InstrumentationRegistry;
25 import android.support.test.filters.MediumTest;
26 import android.support.test.runner.AndroidJUnit4;
27 import android.util.ArraySet;
28
29 import com.android.settings.search.DatabaseIndexingUtils;
30 import com.android.settings.search.Indexable;
31 import com.android.settings.search.SearchIndexableResources;
32 import com.android.settingslib.core.AbstractPreferenceController;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37
38 import java.util.List;
39 import java.util.Set;
40
41 @RunWith(AndroidJUnit4.class)
42 @MediumTest
43 public class PreferenceControllerContractTest {
44
45     private Context mContext;
46
47     @Before
48     public void setUp() {
49         mContext = InstrumentationRegistry.getTargetContext();
50     }
51
52     @Test
53     @Presubmit
54     public void controllersInSearchShouldImplementPreferenceControllerMixin() {
55         final Set<String> errorClasses = new ArraySet<>();
56
57         for (Class clazz : SearchIndexableResources.providerValues()) {
58
59             final Indexable.SearchIndexProvider provider =
60                     DatabaseIndexingUtils.getSearchIndexProvider(clazz);
61             if (provider == null) {
62                 continue;
63             }
64
65             final List<AbstractPreferenceController> controllers =
66                     provider.getPreferenceControllers(mContext);
67             if (controllers == null) {
68                 continue;
69             }
70             for (AbstractPreferenceController controller : controllers) {
71                 if (!(controller instanceof PreferenceControllerMixin)
72                         && !(controller instanceof BasePreferenceController)) {
73                     errorClasses.add(controller.getClass().getName());
74                 }
75             }
76         }
77
78         if (!errorClasses.isEmpty()) {
79             final StringBuilder errorMessage = new StringBuilder()
80                     .append("Each preference must implement PreferenceControllerMixin ")
81                     .append("or extend BasePreferenceController, ")
82                     .append("the following classes don't:\n");
83             for (String c : errorClasses) {
84                 errorMessage.append(c).append("\n");
85             }
86             fail(errorMessage.toString());
87         }
88     }
89 }