aboutsummaryrefslogtreecommitdiff
path: root/bridge/bridge_client/src/com/android/layoutlib/bridge/intensive/BridgeClient.java
blob: cff0e9ea4993564f8c473ebee1f6d3f9a1fe561a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.layoutlib.bridge.intensive;

import com.android.ide.common.rendering.api.ILayoutLog;
import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.Result;
import com.android.ide.common.rendering.api.SessionParams;
import com.android.ide.common.rendering.api.SessionParams.RenderingMode;
import com.android.ide.common.resources.deprecated.FrameworkResources;
import com.android.ide.common.resources.deprecated.ResourceItem;
import com.android.ide.common.resources.deprecated.ResourceRepository;
import com.android.ide.common.resources.deprecated.TestFolderWrapper;
import com.android.internal.lang.System_Delegate;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.android.RenderParamsFlags;
import com.android.layoutlib.bridge.intensive.setup.ConfigGenerator;
import com.android.layoutlib.bridge.intensive.setup.LayoutPullParser;
import com.android.layoutlib.bridge.intensive.setup.LayoutlibBridgeClientCallback;
import com.android.layoutlib.bridge.intensive.util.ImageUtils;
import com.android.layoutlib.bridge.intensive.util.ModuleClassLoader;
import com.android.layoutlib.bridge.intensive.util.SessionParamsBuilder;
import com.android.layoutlib.bridge.intensive.util.TestAssetRepository;
import com.android.utils.ILogger;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.view.Choreographer;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.google.android.collect.Lists;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
 * Base class for render tests. The render tests load all the framework resources and a project
 * checked in this test's resources. The main dependencies
 * are:
 * 1. Fonts directory.
 * 2. Framework Resources.
 * 3. App resources.
 * 4. build.prop file
 * <p>
 * These are configured by two variables set in the system properties.
 * <p>
 * 1. platform.dir: This is the directory for the current platform in the built SDK
 * (.../sdk/platforms/android-<version>).
 * <p>
 * The fonts are platform.dir/data/fonts.
 * The Framework resources are platform.dir/data/res.
 * build.prop is at platform.dir/build.prop.
 * <p>
 * 2. test_res.dir: This is the directory for the resources of the test. If not specified, this
 * falls back to getClass().getProtectionDomain().getCodeSource().getLocation()
 * <p>
 * The app resources are obtained using getAppResources() getAppTestAsset() getAppClassesLocation()
 */
public abstract class BridgeClient {

    protected static final String PLATFORM_DIR;
    private static final String ANDROID_HOST_OUT_DIR_PROPERTY = "android_host_out.dir";
    private static final String NATIVE_LIB_PATH_PROPERTY = "native.lib.path";
    private static final String FONT_DIR_PROPERTY = "font.dir";
    private static final String ICU_DATA_PATH_PROPERTY = "icu.data.path";
    private static final String KEYBOARD_DIR_PROPERTY = "keyboard.dir";
    private static final String PLATFORM_DIR_PROPERTY = "platform.dir";

    private static final String NATIVE_LIB_DIR_PATH;
    private static final String FONT_DIR;
    private static final String ICU_DATA_PATH;
    private static final String KEYBOARD_DIR;
    private static final String EMPTY_FRAME =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?> <FrameLayout "
                    + "xmlns:android=\"http://schemas.android.com/apk/res/android\" "
                    + "android:layout_width=\"match_parent\" "
                    + "android:layout_height=\"match_parent\"> </FrameLayout>";
    protected static Bridge sBridge;
    /** List of log messages generated by a render call. It can be used to find specific errors */
    protected static ArrayList<String> sRenderMessages = Lists.newArrayList();
    private static ILayoutLog sLayoutLibLog;
    private static FrameworkResources sFrameworkRepo;
    private static ResourceRepository sProjectResources;
    private static ILogger sLogger;

    static {
        // Test that System Properties are properly set.
        PLATFORM_DIR = getPlatformDir();
        if (PLATFORM_DIR == null) {
            fail(String.format("System Property %1$s not properly set. The value is %2$s",
                    PLATFORM_DIR_PROPERTY, System.getProperty(PLATFORM_DIR_PROPERTY)));
        }

        NATIVE_LIB_DIR_PATH = getNativeLibDirPath();
        FONT_DIR = getFontDir();
        ICU_DATA_PATH = getIcuDataPath();
        KEYBOARD_DIR = getKeyboardDir();
    }

    @Rule
    public TestWatcher sRenderMessageWatcher = new TestWatcher() {
        @Override
        protected void succeeded(Description description) {
            // We only check error messages if the rest of the test case was successful.
            if (!sRenderMessages.isEmpty()) {
                fail(description.getMethodName() + " render error message: " +
                        sRenderMessages.get(0));
            }
        }
    };
    @Rule
    public TestWatcher sMemoryLeakChecker = new TestWatcher() {
        @Override
        protected void succeeded(Description description) {
            for (int i = Choreographer.CALLBACK_INPUT; i <= Choreographer.CALLBACK_COMMIT; ++i) {
                if (Choreographer.getInstance().mCallbackQueues[i].mHead != null) {
                    fail("Memory leak: leftover frame callbacks are detected in Choreographer");
                }
            }
        }
    };
    protected ClassLoader mDefaultClassLoader;
    private String mPackageName;
    private String mThemeName;

    public BridgeClient(String themeName) {
        init(themeName);
    }

    public BridgeClient() {
        init("AppTheme");
    }

    private static String getNativeLibDirPath() {
        String nativeLibDirPath = System.getProperty(NATIVE_LIB_PATH_PROPERTY);
        if (nativeLibDirPath != null) {
            File nativeLibDir = new File(nativeLibDirPath);
            if (nativeLibDir.isDirectory()) {
                nativeLibDirPath = nativeLibDir.getAbsolutePath();
            } else {
                nativeLibDirPath = null;
            }
        }
        if (nativeLibDirPath == null) {
            nativeLibDirPath = PLATFORM_DIR + "/../../../../../lib64/";
        }
        return nativeLibDirPath;
    }

    private static String getFontDir() {
        String fontDir = System.getProperty(FONT_DIR_PROPERTY);
        if (fontDir == null) {
            // The fonts are built into out/host/common/obj/PACKAGING/fonts_intermediates
            // as specified in build/make/core/layoutlib_data.mk, and PLATFORM_DIR is
            // out/host/[arch]/sdk/sdk*/android-sdk*/platforms/android*
            fontDir = PLATFORM_DIR +
                    "/../../../../../../common/obj/PACKAGING/fonts_intermediates";
        }
        return fontDir;
    }

    private static String getIcuDataPath() {
        String icuDataPath = System.getProperty(ICU_DATA_PATH_PROPERTY);
        if (icuDataPath == null) {
            icuDataPath = PLATFORM_DIR + "/../../../../../com.android.i18n/etc/icu/icudt72l.dat";
        }
        return icuDataPath;
    }

    private static String getKeyboardDir() {
        String keyboardDir = System.getProperty(KEYBOARD_DIR_PROPERTY);
        if (keyboardDir == null) {
            // The keyboard files are built into
            // out/host/common/obj/PACKAGING/keyboards_intermediates
            // as specified in build/make/core/layoutlib_data.mk, and PLATFORM_DIR is
            // out/host/[arch]/sdk/sdk*/android-sdk*/platforms/android*
            keyboardDir = PLATFORM_DIR +
                    "/../../../../../../common/obj/PACKAGING/keyboards_intermediates";
        }
        return keyboardDir;
    }

    private static String getAndroidHostOutDir() {
        String androidHostOutDir = System.getProperty(ANDROID_HOST_OUT_DIR_PROPERTY);
        if (androidHostOutDir != null && !androidHostOutDir.isEmpty()
                && new File(androidHostOutDir).isDirectory()) {
            return androidHostOutDir;
        }

        androidHostOutDir = System.getenv("ANDROID_HOST_OUT");
        if (androidHostOutDir != null && !androidHostOutDir.isEmpty()
                && new File(androidHostOutDir).isDirectory()) {
            return androidHostOutDir;
        }

        return null;
    }

    private static String getPlatformDir() {
        String platformDir = System.getProperty(PLATFORM_DIR_PROPERTY);
        if (platformDir != null && !platformDir.isEmpty() && new File(platformDir).isDirectory()) {
            return platformDir;
        }
        // System Property not set. Try to find the directory in the build directory.
        String androidHostOut = getAndroidHostOutDir();
        if (androidHostOut != null) {
            platformDir = getPlatformDirFromHostOut(new File(androidHostOut));
            if (platformDir != null) {
                return platformDir;
            }
        }
        String workingDirString = System.getProperty("user.dir");
        File workingDir = new File(workingDirString);
        // Test if workingDir is android checkout root.
        platformDir = getPlatformDirFromRoot(workingDir);
        if (platformDir != null) {
            return platformDir;
        }

        // Test if workingDir is platform/frameworks/base/tools/layoutlib/bridge.
        File currentDir = workingDir;
        if (currentDir.getName().equalsIgnoreCase("bridge")) {
            currentDir = currentDir.getParentFile();
        }

        // Find frameworks/layoutlib
        while (currentDir != null && !"layoutlib".equals(currentDir.getName())) {
            currentDir = currentDir.getParentFile();
        }

        if (currentDir == null ||
                currentDir.getParentFile() == null ||
                !"frameworks".equals(currentDir.getParentFile().getName())) {
            return null;
        }

        // Test if currentDir is  platform/frameworks/layoutlib. That is, root should be
        // workingDir/../../ (2 levels up)
        for (int i = 0; i < 2; i++) {
            if (currentDir != null) {
                currentDir = currentDir.getParentFile();
            }
        }
        return currentDir == null ? null : getPlatformDirFromRoot(currentDir);
    }

    private static String getPlatformDirFromRoot(File root) {
        if (!root.isDirectory()) {
            return null;
        }
        File out = new File(root, "out");
        if (!out.isDirectory()) {
            return null;
        }
        File host = new File(out, "host");
        if (!host.isDirectory()) {
            return null;
        }
        File[] hosts = host.listFiles(path -> path.isDirectory() &&
                (path.getName().startsWith("linux-") ||
                        path.getName().startsWith("darwin-")));
        assert hosts != null;
        for (File hostOut : hosts) {
            String platformDir = getPlatformDirFromHostOut(hostOut);
            if (platformDir != null) {
                return platformDir;
            }
        }

        return null;
    }

    private static String getPlatformDirFromHostOut(File out) {
        if (!out.isDirectory()) {
            return null;
        }
        File sdkDir = new File(out, "sdk");
        if (!sdkDir.isDirectory()) {
            return null;
        }
        File[] sdkDirs = sdkDir.listFiles(path -> {
            // We need to search for $TARGET_PRODUCT (usually, sdk_phone_armv7)
            return path.isDirectory() && path.getName().startsWith("sdk");
        });
        assert sdkDirs != null;
        for (File dir : sdkDirs) {
            String platformDir = getPlatformDirFromHostOutSdkSdk(dir);
            if (platformDir != null) {
                return platformDir;
            }
        }
        return null;
    }

    private static String getPlatformDirFromHostOutSdkSdk(File sdkDir) {
        File[] possibleSdks = sdkDir.listFiles(
                path -> path.isDirectory() && path.getName().contains("android-sdk"));
        assert possibleSdks != null;
        for (File possibleSdk : possibleSdks) {
            File platformsDir = new File(possibleSdk, "platforms");
            File[] platforms = platformsDir.listFiles(
                    path -> path.isDirectory() && path.getName().startsWith("android-"));
            if (platforms == null || platforms.length == 0) {
                continue;
            }
            Arrays.sort(platforms, (o1, o2) -> {
                final int MAX_VALUE = 1000;
                String suffix1 = o1.getName().substring("android-".length());
                String suffix2 = o2.getName().substring("android-".length());
                int suff1, suff2;
                try {
                    suff1 = Integer.parseInt(suffix1);
                } catch (NumberFormatException e) {
                    suff1 = MAX_VALUE;
                }
                try {
                    suff2 = Integer.parseInt(suffix2);
                } catch (NumberFormatException e) {
                    suff2 = MAX_VALUE;
                }
                if (suff1 != MAX_VALUE || suff2 != MAX_VALUE) {
                    return suff2 - suff1;
                }
                return suffix2.compareTo(suffix1);
            });
            return platforms[0].getAbsolutePath();
        }
        return null;
    }

    /**
     * Initialize the bridge and the resource maps.
     */
    @BeforeClass
    public static void beforeClass() {
        File data_dir = new File(PLATFORM_DIR, "data");
        File res = new File(data_dir, "res");
        sFrameworkRepo = new FrameworkResources(new TestFolderWrapper(res));
        sFrameworkRepo.loadResources();
        sFrameworkRepo.loadPublicResources(getLogger());

        File fontLocation = new File(FONT_DIR);
        File buildProp = new File(PLATFORM_DIR, "build.prop");
        File attrs = new File(res, "values" + File.separator + "attrs.xml");
        String[] keyboardPaths = new String[0];
        sBridge = new Bridge();
        sBridge.init(ConfigGenerator.loadProperties(buildProp), fontLocation, NATIVE_LIB_DIR_PATH,
                ICU_DATA_PATH, keyboardPaths, ConfigGenerator.getEnumMap(attrs), getLayoutLog());
        Bridge.getLock().lock();
        try {
            Bridge.setLog(getLayoutLog());
        } finally {
            Bridge.getLock().unlock();
        }
    }

    @AfterClass
    public static void tearDown() {
        sLayoutLibLog = null;
        sFrameworkRepo = null;
        sProjectResources = null;
        sLogger = null;
        sBridge = null;
    }

    protected static ILayoutLog getLayoutLog() {
        if (sLayoutLibLog == null) {
            sLayoutLibLog = new ILayoutLog() {
                @Override
                public void warning(@Nullable String tag, @NonNull String message,
                        @Nullable Object viewCookie,
                        @Nullable Object data) {
                    System.out.println("BridgeClient: LayoutLog: Warning " + tag + ": " + message);
                    failWithMsg(message);
                }

                @Override
                public void fidelityWarning(@Nullable String tag, String message,
                        Throwable throwable, Object cookie, Object data) {

                    System.out.println("FidelityWarning " + tag + ": " + message);
                    if (throwable != null) {
                        throwable.printStackTrace();
                    }
                    failWithMsg(message == null ? "" : message);
                }

                @Override
                public void error(@Nullable String tag, @NonNull String message,
                        @Nullable Object viewCookie,
                        @Nullable Object data) {
                    System.out.println("Error " + tag + ": " + message);
                    failWithMsg(message);
                }

                @Override
                public void error(@Nullable String tag, @NonNull String message,
                        @Nullable Throwable throwable,
                        @Nullable Object viewCookie, @Nullable Object data) {
                    System.out.println("Error " + tag + ": " + message);
                    if (throwable != null) {
                        throwable.printStackTrace();
                    }
                    failWithMsg(message);
                }

                @Override
                public void logAndroidFramework(int priority, String tag, String message) {
                    System.out.println("Android framework message " + tag + ": " + message);
                }
            };
        }
        return sLayoutLibLog;
    }

    protected static void ignoreAllLogging() {
        sLayoutLibLog = new ILayoutLog() {
        };
        sLogger = new ILogger() {
            @Override
            public void error(Throwable t, String msgFormat, Object... args) {
            }

            @Override
            public void warning(String msgFormat, Object... args) {
            }

            @Override
            public void info(String msgFormat, Object... args) {
            }

            @Override
            public void verbose(String msgFormat, Object... args) {
            }
        };
    }

    protected static ILogger getLogger() {
        if (sLogger == null) {
            sLogger = new ILogger() {
                @Override
                public void error(Throwable t, @Nullable String msgFormat, Object... args) {
                    if (t != null) {
                        t.printStackTrace();
                    }
                    failWithMsg(msgFormat == null ? "" : msgFormat, args);
                }

                @Override
                public void warning(@NonNull String msgFormat, Object... args) {
                    failWithMsg(msgFormat, args);
                }

                @Override
                public void info(@NonNull String msgFormat, Object... args) {
                    // pass.
                }

                @Override
                public void verbose(@NonNull String msgFormat, Object... args) {
                    // pass.
                }
            };
        }
        return sLogger;
    }

    private static void failWithMsg(@NonNull String msgFormat, Object... args) {
        sRenderMessages.add(args == null ? msgFormat : String.format(msgFormat, args));
    }

    public abstract String getAppTestDir();

    public abstract String getAppTestRes();

    public abstract String getAppResources();

    public abstract String getAppGoldenDir();

    public abstract String getAppTestAsset();

    public abstract String getAppClassesLocation();

    protected void init(String themeName) {
        mPackageName = "NOT_INITIALIZED";
        mThemeName = themeName;
        initProjectResources();
    }

    public void setPackageName(String packageName) {
        mPackageName = packageName;
    }

    public void initProjectResources() {
        String TEST_RESOURCE_FOLDER = getAppTestRes();
        sProjectResources =
                new ResourceRepository(new TestFolderWrapper(TEST_RESOURCE_FOLDER), false) {
                    @NonNull
                    @Override
                    protected ResourceItem createResourceItem(@NonNull String name) {
                        return new ResourceItem(name);
                    }
                };
        sProjectResources.loadResources();

    }

    @NonNull
    protected RenderResult render(com.android.ide.common.rendering.api.Bridge bridge,
            SessionParams params,
            long frameTimeNanos) {
        return render(bridge, params, frameTimeNanos, null);
    }

    @NonNull
    protected RenderResult render(com.android.ide.common.rendering.api.Bridge bridge,
            SessionParams params,
            long frameTimeNanos,
            @Nullable RenderSessionListener listener) {
        // 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 = bridge.createSession(params);

        try {
            if (frameTimeNanos != -1) {
                session.setElapsedFrameTimeNanos(frameTimeNanos);
            }

            if (!session.getResult().isSuccess()) {
                getLogger().error(session.getResult().getException(),
                        session.getResult().getErrorMessage());
            } else {
                // Render the session with a timeout of 50s.
                Result renderResult = session.render(50000);
                if (!renderResult.isSuccess()) {
                    getLogger().error(session.getResult().getException(),
                            session.getResult().getErrorMessage());
                }
            }
            if (listener != null) {
                listener.beforeDisposed(session);
            }

            return RenderResult.getFromSession(session);
        } finally {
            session.dispose();
        }
    }

    /**
     * Compares the golden image with the passed image
     */
    protected void verify(@NonNull String goldenImageName, @NonNull BufferedImage image) {
        try {
            String goldenImagePath = getAppGoldenDir() + goldenImageName;
            ImageUtils.requireSimilar(goldenImagePath, image);
        } catch (IOException e) {
            getLogger().error(e, e.getMessage());
        }
    }

    /**
     * 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
    protected RenderResult renderAndVerify(SessionParams params, String goldenFileName,
            long frameTimeNanos) throws ClassNotFoundException {
        RenderResult result = render(sBridge, params, frameTimeNanos);
        assertNotNull(result.getImage());
        verify(goldenFileName, result.getImage());

        return result;
    }

    /**
     * Create a new rendering session and test that rendering the given layout doesn't throw any
     * exceptions and matches the provided image.
     */
    @Nullable
    protected RenderResult renderAndVerify(SessionParams params, String goldenFileName)
            throws ClassNotFoundException {
        return renderAndVerify(params, goldenFileName, TimeUnit.SECONDS.toNanos(2));
    }

    @Before
    public void beforeTestCase() {
        // Default class loader with access to the app classes
        mDefaultClassLoader = new ModuleClassLoader(getAppClassesLocation(),
                getClass().getClassLoader());
        sRenderMessages.clear();
    }

    @NonNull
    protected LayoutPullParser createParserFromPath(String layoutPath)
            throws FileNotFoundException {
        return LayoutPullParser.createFromPath(getAppResources() + "/layout/" + layoutPath);
    }

    @NonNull
    protected LayoutPullParser createParserFromString(String layoutStr)
            throws FileNotFoundException {
        return LayoutPullParser.createFromString(layoutStr);
    }

    /**
     * Create a new rendering session and test that rendering the given layout on nexus 5
     * doesn't throw any exceptions and matches the provided image.
     */
    @Nullable
    protected RenderResult renderAndVerify(String layoutFileName, String goldenFileName,
            boolean decoration)
            throws ClassNotFoundException, FileNotFoundException {
        return renderAndVerify(layoutFileName, goldenFileName, ConfigGenerator.NEXUS_5, decoration);
    }

    /**
     * Create a new rendering session and test that rendering the given layout on given device
     * doesn't throw any exceptions and matches the provided image.
     */
    @Nullable
    protected RenderResult renderAndVerify(String layoutFileName, String goldenFileName,
            ConfigGenerator deviceConfig, boolean decoration) throws ClassNotFoundException,
            FileNotFoundException {
        SessionParams params = createSessionParams(layoutFileName, deviceConfig);
        if (!decoration) {
            params.setForceNoDecor();
        }
        return renderAndVerify(params, goldenFileName);
    }

    protected SessionParams createSessionParams(String layoutFileName, ConfigGenerator deviceConfig)
            throws ClassNotFoundException, FileNotFoundException {
        // Create the layout pull parser.

        LayoutPullParser parser = null;
        if (layoutFileName != null) {
            parser = createParserFromPath(layoutFileName);
        } else {
            parser = createParserFromString(EMPTY_FRAME);

        }
        // Create LayoutLibCallback.
        LayoutlibBridgeClientCallback layoutLibCallback =
                new LayoutlibBridgeClientCallback(getLogger(), mDefaultClassLoader, mPackageName);
        layoutLibCallback.initResources();
        // TODO: Set up action bar handler properly to test menu rendering.
        // Create session params.
        return getSessionParamsBuilder()
                .setParser(parser)
                .setConfigGenerator(deviceConfig)
                .setCallback(layoutLibCallback)
                .setTheme(mThemeName, true)
                .build();
    }

    /**
     * Returns a pre-configured {@link SessionParamsBuilder} for target API 22, Normal rendering
     * mode, AppTheme as theme and Nexus 5.
     */
    @NonNull
    protected SessionParamsBuilder getSessionParamsBuilder() {
        return new SessionParamsBuilder()
                .setLayoutLog(getLayoutLog())
                .setFrameworkResources(sFrameworkRepo)
                .setConfigGenerator(ConfigGenerator.NEXUS_5)
                .setProjectResources(sProjectResources)
                .setTheme("AppTheme", true)
                .setRenderingMode(RenderingMode.NORMAL)
                .setTargetSdk(28)
                .setFlag(RenderParamsFlags.FLAG_DO_NOT_RENDER_ON_CREATE, true)
                .setAssetRepository(
                        new TestAssetRepository(getAppTestAsset()));
    }

    /**
     * Listener for render process.
     */
    public interface RenderSessionListener {

        /**
         * Called before session is disposed after rendering.
         */
        void beforeDisposed(RenderSession session);
    }
}