2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.core;
19 import static junit.framework.Assert.fail;
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;
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;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
38 import java.util.List;
41 @RunWith(AndroidJUnit4.class)
43 public class PreferenceControllerContractTest {
45 private Context mContext;
49 mContext = InstrumentationRegistry.getTargetContext();
54 public void controllersInSearchShouldImplementPreferenceControllerMixin() {
55 final Set<String> errorClasses = new ArraySet<>();
57 for (Class clazz : SearchIndexableResources.providerValues()) {
59 final Indexable.SearchIndexProvider provider =
60 DatabaseIndexingUtils.getSearchIndexProvider(clazz);
61 if (provider == null) {
65 final List<AbstractPreferenceController> controllers =
66 provider.getPreferenceControllers(mContext);
67 if (controllers == null) {
70 for (AbstractPreferenceController controller : controllers) {
71 if (!(controller instanceof PreferenceControllerMixin)
72 && !(controller instanceof BasePreferenceController)) {
73 errorClasses.add(controller.getClass().getName());
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");
86 fail(errorMessage.toString());