Home | History | Annotate | Download | only in testing
      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.support.test.InstrumentationRegistry;
     25 import android.test.suitebuilder.annotation.SmallTest;
     26 
     27 import com.android.testables.R;
     28 
     29 import org.junit.Rule;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 @SmallTest
     34 @RunWith(AndroidTestingRunner.class)
     35 public class TestableResourcesTest {
     36 
     37     @Rule
     38     public TestableContext mContext = new TestableContext(InstrumentationRegistry.getContext());
     39 
     40     @Test
     41     public void testLazyInit() {
     42         Resources before = mContext.getResources();
     43         mContext.ensureTestableResources();
     44         Resources after = mContext.getResources();
     45         assertNotEquals(before, after);
     46     }
     47 
     48     @Test
     49     public void testAddingResource() {
     50         final int nonExistentId = 3; // Ids don't go this low.
     51 
     52         try {
     53             mContext.getColor(nonExistentId);
     54             fail("Should throw NotFoundException");
     55         } catch (Resources.NotFoundException e) {
     56         }
     57         mContext.getOrCreateTestableResources().addOverride(nonExistentId, 0xffffff);
     58 
     59         assertEquals(0xffffff, mContext.getColor(nonExistentId));
     60     }
     61 
     62     @Test
     63     public void testClearingResource() {
     64         final int nonExistentId = 3; // Ids don't go this low.
     65 
     66         mContext.getOrCreateTestableResources().addOverride(nonExistentId, 0xffffff);
     67         assertEquals(0xffffff, mContext.getColor(nonExistentId));
     68         mContext.getOrCreateTestableResources().removeOverride(nonExistentId);
     69         try {
     70             mContext.getColor(nonExistentId);
     71             fail("Should throw NotFoundException");
     72         } catch (Resources.NotFoundException e) {
     73         }
     74     }
     75 
     76     @Test
     77     public void testOverrideExisting() {
     78         int existentId = R.string.test_string;
     79 
     80         assertNotNull(mContext.getString(existentId));
     81         mContext.getOrCreateTestableResources().addOverride(existentId, "Other strings");
     82 
     83         assertEquals("Other strings", mContext.getString(existentId));
     84     }
     85 
     86     @Test(expected = Resources.NotFoundException.class)
     87     public void testNonExistentException() {
     88         int existentId = R.string.test_string;
     89 
     90         assertNotNull(mContext.getString(existentId));
     91         mContext.getOrCreateTestableResources().addOverride(existentId, null);
     92 
     93         assertNull(mContext.getString(existentId));
     94     }
     95 }
     96