• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.build;
6 
7 import android.content.Context;
8 import android.content.pm.ApplicationInfo;
9 import android.content.pm.PackageManager;
10 import android.content.pm.PackageManager.NameNotFoundException;
11 
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.robolectric.RobolectricTestRunner;
16 import org.robolectric.RuntimeEnvironment;
17 
18 import java.io.IOException;
19 import java.io.InputStream;
20 
21 /**
22  * Checks that Robolectric tests can use android assets.
23  */
24 @RunWith(RobolectricTestRunner.class)
25 public class AndroidAssetsTest {
26     private static final String TEST_ASSET_NAME = "AndroidAssetsTest.java";
27 
readTestAsset()28     public String readTestAsset() throws IOException {
29         try (InputStream stream =
30                         RuntimeEnvironment.getApplication().getAssets().open(TEST_ASSET_NAME)) {
31             byte[] buffer = new byte[stream.available()];
32             stream.read(buffer);
33             return new String(buffer);
34         }
35     }
36 
37     @Test
testAssetsExist()38     public void testAssetsExist() throws IOException {
39         String myselfAsAssetData = readTestAsset();
40         Assert.assertTrue("asset not correct. It had length=" + myselfAsAssetData.length(),
41                 myselfAsAssetData.contains("String myselfAsAssetData = "));
42     }
43 
44     @Test
testResourcesExist()45     public void testResourcesExist() {
46         String actual = RuntimeEnvironment.getApplication().getString(R.string.test_string);
47         Assert.assertEquals("Hello World", actual);
48     }
49 
50     @Test
testManifestMerged()51     public void testManifestMerged() throws NameNotFoundException {
52         Context context = RuntimeEnvironment.getApplication();
53         ApplicationInfo info = context.getPackageManager().getApplicationInfo(
54                 context.getPackageName(), PackageManager.GET_META_DATA);
55         String actual = info.metaData.getString("test-metadata");
56         Assert.assertEquals("Hello World", actual);
57     }
58 }
59