1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package android.testing; 16 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertNotEquals; 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertNull; 21 import static org.junit.Assert.fail; 22 23 import android.content.res.Resources; 24 import android.test.suitebuilder.annotation.SmallTest; 25 26 import androidx.test.InstrumentationRegistry; 27 28 import com.android.testables.R; 29 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @SmallTest 35 @RunWith(AndroidTestingRunner.class) 36 public class TestableResourcesTest { 37 38 @Rule 39 public TestableContext mContext = new TestableContext(InstrumentationRegistry.getContext()); 40 41 @Test testLazyInit()42 public void testLazyInit() { 43 Resources before = mContext.getResources(); 44 mContext.ensureTestableResources(); 45 Resources after = mContext.getResources(); 46 assertNotEquals(before, after); 47 } 48 49 @Test testAddingResource()50 public void testAddingResource() { 51 final int nonExistentId = 3; // Ids don't go this low. 52 53 try { 54 mContext.getColor(nonExistentId); 55 fail("Should throw NotFoundException"); 56 } catch (Resources.NotFoundException e) { 57 } 58 mContext.getOrCreateTestableResources().addOverride(nonExistentId, 0xffffff); 59 60 assertEquals(0xffffff, mContext.getColor(nonExistentId)); 61 } 62 63 @Test testClearingResource()64 public void testClearingResource() { 65 final int nonExistentId = 3; // Ids don't go this low. 66 67 mContext.getOrCreateTestableResources().addOverride(nonExistentId, 0xffffff); 68 assertEquals(0xffffff, mContext.getColor(nonExistentId)); 69 mContext.getOrCreateTestableResources().removeOverride(nonExistentId); 70 try { 71 mContext.getColor(nonExistentId); 72 fail("Should throw NotFoundException"); 73 } catch (Resources.NotFoundException e) { 74 } 75 } 76 77 @Test testOverrideExisting()78 public void testOverrideExisting() { 79 int existentId = R.string.test_string; 80 81 assertNotNull(mContext.getString(existentId)); 82 mContext.getOrCreateTestableResources().addOverride(existentId, "Other strings"); 83 84 assertEquals("Other strings", mContext.getString(existentId)); 85 } 86 87 @Test(expected = Resources.NotFoundException.class) testNonExistentException()88 public void testNonExistentException() { 89 int existentId = R.string.test_string; 90 91 assertNotNull(mContext.getString(existentId)); 92 mContext.getOrCreateTestableResources().addOverride(existentId, null); 93 94 assertNull(mContext.getString(existentId)); 95 } 96 } 97