Search in sources :

Example 1 with PROJECT_WITH_DATA_BINDING

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"));
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) SdkConstants(com.android.SdkConstants) StringUtil(com.intellij.openapi.util.text.StringUtil) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ContainerUtil(com.intellij.util.containers.ContainerUtil) AndroidLibrary(com.android.builder.model.AndroidLibrary) Collectors(java.util.stream.Collectors) File(java.io.File) Predicate(com.google.common.base.Predicate) PROJECT_WITH_DATA_BINDING(com.android.tools.idea.testing.TestProjectPaths.PROJECT_WITH_DATA_BINDING) Message(com.android.ide.common.blame.Message) org.jetbrains.org.objectweb.asm(org.jetbrains.org.objectweb.asm) GradleInvocationResult(com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult) AndroidGradleTestCase(com.android.tools.idea.testing.AndroidGradleTestCase) com.intellij.psi(com.intellij.psi) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) NotNull(org.jetbrains.annotations.NotNull) ZipEntry(java.util.zip.ZipEntry) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) Joiner(com.google.common.base.Joiner) IOException(java.io.IOException) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) GradleInvocationResult(com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

SdkConstants (com.android.SdkConstants)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 Message (com.android.ide.common.blame.Message)1 GradleInvocationResult (com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 GradleSyncState (com.android.tools.idea.gradle.project.sync.GradleSyncState)1 AndroidGradleTestCase (com.android.tools.idea.testing.AndroidGradleTestCase)1 PROJECT_WITH_DATA_BINDING (com.android.tools.idea.testing.TestProjectPaths.PROJECT_WITH_DATA_BINDING)1 Joiner (com.google.common.base.Joiner)1 Predicate (com.google.common.base.Predicate)1 Iterables (com.google.common.collect.Iterables)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 com.intellij.psi (com.intellij.psi)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 File (java.io.File)1 IOException (java.io.IOException)1 java.util (java.util)1 JarFile (java.util.jar.JarFile)1 Collectors (java.util.stream.Collectors)1 ZipEntry (java.util.zip.ZipEntry)1