Home | History | Annotate | Download | only in res
      1 /*
      2  * Copyright (C) 2017 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 android.content.res;
     18 
     19 import com.android.resources.ResourceType;
     20 import com.android.resources.ResourceUrl;
     21 
     22 import org.junit.Test;
     23 
     24 import android.annotation.NonNull;
     25 import android.annotation.Nullable;
     26 
     27 import static org.junit.Assert.assertEquals;
     28 import static org.junit.Assert.assertNotNull;
     29 import static org.junit.Assert.assertNull;
     30 import static org.junit.Assert.fail;
     31 
     32 public class Resources_DelegateTest {
     33     private static void assertResourceUrl(@Nullable String pkg, @NonNull String name,
     34             @NonNull ResourceType type, @Nullable ResourceUrl url) {
     35         assertNotNull(url);
     36         assertEquals(type, url.type);
     37         assertEquals(pkg, url.namespace);
     38         assertEquals(name, url.name);
     39     }
     40 
     41     @Test
     42     public void resourceUrlFromName() {
     43         try {
     44             Resources_Delegate.resourceUrlFromName("pkg:name", null, null);
     45             fail("Expected IllegalArgumentException since no type was defined");
     46         } catch (IllegalArgumentException ignored) {
     47         }
     48 
     49         assertNull(Resources_Delegate.resourceUrlFromName("package:invalid/name", null, null));
     50         assertNull(Resources_Delegate.resourceUrlFromName("package:name", "invalid", null));
     51         assertResourceUrl("package", "name", ResourceType.ID,
     52                 Resources_Delegate.resourceUrlFromName("package:name", "id", null));
     53         assertResourceUrl("package", "name", ResourceType.ID,
     54                 Resources_Delegate.resourceUrlFromName("name", "id", "package"));
     55         assertResourceUrl("package", "test", ResourceType.STRING,
     56                 Resources_Delegate.resourceUrlFromName("package:string/test", null, null));
     57         assertResourceUrl(null, "test", ResourceType.STRING,
     58                 Resources_Delegate.resourceUrlFromName("string/test", null, null));
     59 
     60 
     61         // Type and package in the name take precedence over the passed defType and defPackage
     62         assertResourceUrl("p1", "r1", ResourceType.STRING,
     63                 Resources_Delegate.resourceUrlFromName("p1:string/r1", "id", "p2"));
     64         assertResourceUrl("p2", "r1", ResourceType.STRING,
     65                 Resources_Delegate.resourceUrlFromName("string/r1", "id", "p2"));
     66         assertResourceUrl("p1", "r1", ResourceType.ID,
     67                 Resources_Delegate.resourceUrlFromName("p1:r1", "id", "p2"));
     68     }
     69 }