1 /* 2 * Copyright (C) 2016 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.instrumentation; 18 19 import android.app.Fragment; 20 import android.util.ArraySet; 21 22 import com.android.settings.core.codeinspection.CodeInspector; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.Set; 27 28 import static com.google.common.truth.Truth.assertWithMessage; 29 30 /** 31 * {@link CodeInspector} that verifies all fragments implements Instrumentable. 32 */ 33 public class InstrumentableFragmentCodeInspector extends CodeInspector { 34 35 private final List<String> grandfather_notImplementingInstrumentable; 36 InstrumentableFragmentCodeInspector(List<Class<?>> classes)37 public InstrumentableFragmentCodeInspector(List<Class<?>> classes) { 38 super(classes); 39 grandfather_notImplementingInstrumentable = new ArrayList<>(); 40 initializeGrandfatherList(grandfather_notImplementingInstrumentable, 41 "grandfather_not_implementing_instrumentable"); 42 } 43 44 @Override run()45 public void run() { 46 final Set<String> broken = new ArraySet<>(); 47 48 for (Class clazz : mClasses) { 49 if (!isConcreteSettingsClass(clazz)) { 50 continue; 51 } 52 final String className = clazz.getName(); 53 // If it's a fragment, it must also be instrumentable. 54 final boolean whitelisted = 55 grandfather_notImplementingInstrumentable.remove(className); 56 if (Fragment.class.isAssignableFrom(clazz) 57 && !Instrumentable.class.isAssignableFrom(clazz) 58 && !whitelisted) { 59 broken.add(className); 60 } 61 } 62 final StringBuilder sb = new StringBuilder( 63 "All fragment should implement Instrumentable, but the following are not:\n"); 64 for (String c : broken) { 65 sb.append(c).append("\n"); 66 } 67 assertWithMessage(sb.toString()) 68 .that(broken.isEmpty()) 69 .isTrue(); 70 assertNoObsoleteInGrandfatherList("grandfather_not_implementing_instrumentable", 71 grandfather_notImplementingInstrumentable); 72 } 73 } 74