Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8a7546e1 authored by Diego Perez's avatar Diego Perez Committed by Jerome Gaillard
Browse files

Separate render and verify in tests

Test: Test refactoring. Tests still pass
Change-Id: Icdee3eb9ef1b2639418110583ac74a3fd5362166
(cherry picked from commit 564b00dd5201711c25a3580aa21be8b36bfaf923)
parent 5becc487
Loading
Loading
Loading
Loading
+41 −56
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@ import org.junit.runner.Description;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.pm.PackageInstaller.Session;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
@@ -61,29 +60,12 @@ import android.util.DisplayMetrics;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.CopyOption;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.google.android.collect.Lists;

@@ -453,15 +435,8 @@ public class Main {
        SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5,
                layoutLibCallback, "Theme.Material.Light.NoActionBar", false,
                RenderingMode.NORMAL, 22);
        try {
            renderAndVerify(params, "scrolled.png");
        } catch(AssertionError e) {
            // In this particular test we do not care about the image similarity.
            // TODO: Create new render method that allows to not compare images.
            if (!e.getLocalizedMessage().startsWith("Images differ")) {
                throw e;
            }
        }

        render(params, -1);

        assertTrue((Boolean)field.get(null));
        field.set(null, false);
@@ -654,22 +629,16 @@ public class Main {
        return new LayoutPullParser(APP_TEST_RES + "/layout/" + layoutPath);
    }

    /**
     * Create a new rendering session and test that rendering the given layout doesn't throw any
     * exceptions and matches the provided image.
     * <p>
     * If frameTimeNanos is >= 0 a frame will be executed during the rendering. The time indicates
     * how far in the future is.
     */
    @Nullable
    private RenderResult renderAndVerify(SessionParams params, String goldenFileName, long frameTimeNanos)
            throws ClassNotFoundException {
    @NonNull
    private static RenderResult render(SessionParams params, long frameTimeNanos) {
        // TODO: Set up action bar handler properly to test menu rendering.
        // Create session params.
        System_Delegate.setBootTimeNanos(TimeUnit.MILLISECONDS.toNanos(871732800000L));
        System_Delegate.setNanosTime(TimeUnit.MILLISECONDS.toNanos(871732800000L));
        RenderSession session = sBridge.createSession(params);

        try {

            if (frameTimeNanos != -1) {
                session.setElapsedFrameTimeNanos(frameTimeNanos);
            }
@@ -684,18 +653,34 @@ public class Main {
                getLogger().error(session.getResult().getException(),
                        session.getResult().getErrorMessage());
            }
        try {
            String goldenImagePath = APP_TEST_DIR + "/golden/" + goldenFileName;
            ImageUtils.requireSimilar(goldenImagePath, session.getImage());

            return RenderResult.getFromSession(session);
        } catch (IOException e) {
            getLogger().error(e, e.getMessage());
        } finally {
            session.dispose();
        }
    }

        return null;
    /**
     * Create a new rendering session and test that rendering the given layout doesn't throw any
     * exceptions and matches the provided image.
     * <p>
     * If frameTimeNanos is >= 0 a frame will be executed during the rendering. The time indicates
     * how far in the future is.
     */
    @Nullable
    private static RenderResult renderAndVerify(SessionParams params, String goldenFileName, long
            frameTimeNanos)
            throws ClassNotFoundException {
        RenderResult result = Main.render(params, frameTimeNanos);
        try {
            String goldenImagePath = APP_TEST_DIR + "/golden/" + goldenFileName;
            assertNotNull(result.getImage());
            ImageUtils.requireSimilar(goldenImagePath, result.getImage());
        } catch (IOException e) {
            getLogger().error(e, e.getMessage());
        }

        return result;
    }

    /**
@@ -703,9 +688,9 @@ public class Main {
     * exceptions and matches the provided image.
     */
    @Nullable
    private RenderResult renderAndVerify(SessionParams params, String goldenFileName)
    private static RenderResult renderAndVerify(SessionParams params, String goldenFileName)
            throws ClassNotFoundException {
        return renderAndVerify(params, goldenFileName, -1);
        return Main.renderAndVerify(params, goldenFileName, -1);
    }

    /**
+11 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.android.ide.common.rendering.api.ViewInfo;
import android.annotation.NonNull;
import android.annotation.Nullable;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -31,19 +32,22 @@ class RenderResult {
    private final List<ViewInfo> mRootViews;
    private final List<ViewInfo> mSystemViews;
    private final Result mRenderResult;
    private BufferedImage mImage;

    private RenderResult(@Nullable Result result, @Nullable List<ViewInfo> systemViewInfoList,
            @Nullable List<ViewInfo> rootViewInfoList) {
            @Nullable List<ViewInfo> rootViewInfoList, @Nullable BufferedImage image) {
        mSystemViews = systemViewInfoList == null ? Collections.emptyList() : systemViewInfoList;
        mRootViews = rootViewInfoList == null ? Collections.emptyList() : rootViewInfoList;
        mRenderResult = result;
        mImage = image;
    }

    @NonNull
    static RenderResult getFromSession(@NonNull RenderSession session) {
        return new RenderResult(session.getResult(),
                new ArrayList<>(session.getSystemRootViews()),
                new ArrayList<>(session.getRootViews()));
                new ArrayList<>(session.getRootViews()),
                session.getImage());
    }

    @Nullable
@@ -60,4 +64,9 @@ class RenderResult {
    public List<ViewInfo> getSystemViews() {
        return mSystemViews;
    }

    @Nullable
    public BufferedImage getImage() {
        return mImage;
    }
}