use of com.android.tools.idea.testing.TestProjectPaths.PROJECT_WITH_DATA_BINDING in project android by JetBrains.
the class GeneratedCodeMatchTest method testGeneratedCodeMatch.
public void testGeneratedCodeMatch() throws Exception {
loadProject(PROJECT_WITH_DATA_BINDING);
// temporary fix until test model can detect dependencies properly
GradleInvocationResult assembleDebug = invokeGradleTasks(getProject(), "assembleDebug");
assertTrue(StringUtil.join(assembleDebug.getCompilerMessages(Message.Kind.ERROR), "\n"), assembleDebug.isBuildSuccessful());
GradleSyncState syncState = GradleSyncState.getInstance(getProject());
assertFalse(syncState.isSyncNeeded().toBoolean());
assertTrue(myAndroidFacet.isDataBindingEnabled());
// trigger initialization
myAndroidFacet.getModuleResources(true);
File classesOut = new File(getProject().getBaseDir().getPath(), "/app/build/intermediates/classes/debug");
//noinspection unchecked
Collection<File> classes = FileUtils.listFiles(classesOut, new String[] { "class" }, true);
assertTrue("if we cannot find any class, something is wrong with the test", classes.size() > 0);
ClassReader viewDataBindingClass = findViewDataBindingClass();
Set<String> baseClassInfo = collectDescriptionSet(viewDataBindingClass, new HashSet<>());
JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(getProject());
Set<String> missingClasses = new HashSet<>();
Map<String, ClassReader> klassMap = classes.stream().map((file) -> {
try {
return new ClassReader(FileUtils.readFileToByteArray(file));
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
return null;
}).filter(kls -> kls != null).collect(Collectors.toMap(kls -> kls.getClassName(), kls -> kls));
Map<ClassReader, ClassReader> superClassLookup = klassMap.values().stream().filter(kls -> klassMap.containsKey(kls.getSuperName())).collect(Collectors.toMap(kls -> kls, kls -> klassMap.get(kls.getSuperName())));
int verifiedClassCount = 0;
for (ClassReader classReader : klassMap.values()) {
if (!shouldVerify(viewDataBindingClass, classReader, superClassLookup)) {
continue;
}
verifiedClassCount++;
String className = classReader.getClassName();
PsiClass psiClass = javaPsiFacade.findClass(className.replace("/", "."), myAndroidFacet.getModule().getModuleWithDependenciesAndLibrariesScope(false));
if (psiClass == null) {
missingClasses.add(className);
continue;
}
assertNotNull(psiClass);
String asmInfo = collectDescriptions(classReader, baseClassInfo);
String psiInfo = collectDescriptions(psiClass);
assertEquals(className, asmInfo, psiInfo);
}
assertTrue("test sanity, should be able to find some data binding generated classes", verifiedClassCount > 3);
assertEquals("These classes are missing", "", StringUtil.join(missingClasses, "\n"));
}
Aggregations