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.intensive.util;
18 
19 import com.android.ide.common.rendering.api.AssetRepository;
20 
21 import android.annotation.NonNull;
22 
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 
29 /**
30  * {@link AssetRepository} used for render tests.
31  */
32 public class TestAssetRepository extends AssetRepository {
33     private final String mAssetPath;
34 
TestAssetRepository(@onNull String assetPath)35     public TestAssetRepository(@NonNull String assetPath) {
36         mAssetPath = assetPath;
37     }
38 
open(String path)39     private static InputStream open(String path) throws FileNotFoundException {
40         File asset = new File(path);
41         if (asset.isFile()) {
42             return new FileInputStream(asset);
43         }
44 
45         return null;
46     }
47 
48     @Override
isSupported()49     public boolean isSupported() {
50         return true;
51     }
52 
53     @Override
openAsset(String path, int mode)54     public InputStream openAsset(String path, int mode) throws IOException {
55         return open(mAssetPath + path);
56     }
57 
58     @Override
openNonAsset(int cookie, String path, int mode)59     public InputStream openNonAsset(int cookie, String path, int mode) throws IOException {
60         return open(path);
61     }
62 }
63