1 /*
2  * Copyright (C) 2022 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.layoutlib.bridge.android;
18 
19 import com.android.ide.common.rendering.api.SessionParams;
20 import com.android.layoutlib.bridge.Bridge;
21 import com.android.layoutlib.bridge.impl.RenderAction;
22 import com.android.layoutlib.bridge.impl.RenderActionTestUtil;
23 import com.android.layoutlib.bridge.intensive.RenderTestBase;
24 import com.android.layoutlib.bridge.intensive.setup.LayoutLibTestCallback;
25 import com.android.layoutlib.bridge.intensive.setup.LayoutPullParser;
26 import com.android.ninepatch.NinePatch;
27 
28 import org.junit.Assert;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 
34 import android.R;
35 import android.content.res.Configuration;
36 import android.graphics.Bitmap;
37 import android.graphics.BitmapFactory;
38 import android.graphics.ImageDecoder;
39 import android.graphics.ImageDecoder.Source;
40 import android.util.DisplayMetrics;
41 import android.view.BridgeInflater;
42 
43 import java.awt.image.BufferedImage;
44 import java.awt.image.DataBufferInt;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.nio.Buffer;
48 import java.nio.ByteBuffer;
49 
50 import static org.junit.Assert.assertNotNull;
51 
52 public class PlatformRenderTestCase extends RenderTestBase {
53     protected BridgeContext context;
54     private BridgeContext oldContext;
55     protected BridgeInflater bridgeInflater;
56 
57     @BeforeClass
setUp()58     public static void setUp() {
59         Bridge.prepareThread();
60     }
61 
62     @Before
setUpContext()63     public void setUpContext() throws ClassNotFoundException {
64         // Setup
65         // Create the layout pull parser for our resources (empty.xml can not be part of the test
66         // app as it won't compile).
67         LayoutPullParser parser = LayoutPullParser.createFromPath("/empty.xml");
68         // Create LayoutLibCallback.
69         LayoutLibTestCallback layoutLibCallback =
70                 new LayoutLibTestCallback(getLogger(), mDefaultClassLoader);
71         layoutLibCallback.initResources();
72         SessionParams params = getSessionParamsBuilder()
73                 .setParser(parser)
74                 .setCallback(layoutLibCallback)
75                 .setTheme("Theme.Material", false)
76                 .build();
77         DisplayMetrics metrics = new DisplayMetrics();
78         Configuration configuration = RenderAction.getConfiguration(params);
79         context = new BridgeContext(params.getProjectKey(), metrics, params.getResources(),
80                 params.getAssets(), params.getLayoutlibCallback(), configuration,
81                 params.getTargetSdkVersion(), params.isRtlSupported());
82 
83         context.initResources(params.getAssets());
84         bridgeInflater = new BridgeInflater(context, params.getLayoutlibCallback());
85         oldContext = RenderActionTestUtil.setBridgeContext(context);
86     }
87 
88     @After
recoverContext()89     public void recoverContext() {
90         RenderActionTestUtil.setBridgeContext(oldContext);
91         context.disposeResources();
92     }
93 }
94