1 /* 2 * Copyright (C) 2020 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 package com.android.server.textclassifier; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import android.net.Uri; 21 22 import androidx.test.runner.AndroidJUnit4; 23 24 import com.android.server.textclassifier.IconsUriHelper.ResourceInfo; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** 31 * Tests for {@link IconsUriHelper}. 32 */ 33 @RunWith(AndroidJUnit4.class) 34 public final class IconsUriHelperTest { 35 36 private IconsUriHelper mIconsUriHelper; 37 38 @Before setUp()39 public void setUp() { 40 mIconsUriHelper = IconsUriHelper.newInstanceForTesting(null); 41 } 42 43 @Test testGetContentUri()44 public void testGetContentUri() { 45 final IconsUriHelper iconsUriHelper = IconsUriHelper.newInstanceForTesting(() -> "pkgId"); 46 final Uri expected = new Uri.Builder() 47 .scheme("content") 48 .authority(IconsUriHelper.AUTHORITY) 49 .path("pkgId") 50 .appendPath("1234") 51 .build(); 52 53 final Uri actual = iconsUriHelper.getContentUri("com.package.name", 1234); 54 assertThat(actual).isEqualTo(expected); 55 } 56 57 @Test testGetContentUri_multiplePackages()58 public void testGetContentUri_multiplePackages() { 59 final Uri uri1 = mIconsUriHelper.getContentUri("com.package.name1", 1234); 60 final Uri uri2 = mIconsUriHelper.getContentUri("com.package.name2", 5678); 61 62 assertThat(uri1.getScheme()).isEqualTo("content"); 63 assertThat(uri2.getScheme()).isEqualTo("content"); 64 65 assertThat(uri1.getAuthority()).isEqualTo(IconsUriHelper.AUTHORITY); 66 assertThat(uri2.getAuthority()).isEqualTo(IconsUriHelper.AUTHORITY); 67 68 assertThat(uri1.getPathSegments().get(1)).isEqualTo("1234"); 69 assertThat(uri2.getPathSegments().get(1)).isEqualTo("5678"); 70 } 71 72 @Test testGetContentUri_samePackageIdForSamePackageName()73 public void testGetContentUri_samePackageIdForSamePackageName() { 74 final String packageName = "com.package.name"; 75 final Uri uri1 = mIconsUriHelper.getContentUri(packageName, 1234); 76 final Uri uri2 = mIconsUriHelper.getContentUri(packageName, 5678); 77 78 final String id1 = uri1.getPathSegments().get(0); 79 final String id2 = uri2.getPathSegments().get(0); 80 81 assertThat(id1).isEqualTo(id2); 82 } 83 84 @Test testGetResourceInfo()85 public void testGetResourceInfo() { 86 mIconsUriHelper.getContentUri("com.package.name1", 123); 87 final Uri uri = mIconsUriHelper.getContentUri("com.package.name2", 456); 88 mIconsUriHelper.getContentUri("com.package.name3", 789); 89 90 final ResourceInfo res = mIconsUriHelper.getResourceInfo(uri); 91 assertThat(res.packageName).isEqualTo("com.package.name2"); 92 assertThat(res.id).isEqualTo(456); 93 } 94 95 @Test testGetResourceInfo_unrecognizedUri()96 public void testGetResourceInfo_unrecognizedUri() { 97 final Uri uri = new Uri.Builder() 98 .scheme("content") 99 .authority(IconsUriHelper.AUTHORITY) 100 .path("unrecognized") 101 .appendPath("1234") 102 .build(); 103 assertThat(mIconsUriHelper.getResourceInfo(uri)).isNull(); 104 } 105 106 @Test testGetResourceInfo_invalidScheme()107 public void testGetResourceInfo_invalidScheme() { 108 final IconsUriHelper iconsUriHelper = IconsUriHelper.newInstanceForTesting(() -> "pkgId"); 109 iconsUriHelper.getContentUri("com.package.name", 1234); 110 111 final Uri uri = new Uri.Builder() 112 .scheme("file") 113 .authority(IconsUriHelper.AUTHORITY) 114 .path("pkgId") 115 .appendPath("1234") 116 .build(); 117 assertThat(iconsUriHelper.getResourceInfo(uri)).isNull(); 118 } 119 120 @Test testGetResourceInfo_invalidAuthority()121 public void testGetResourceInfo_invalidAuthority() { 122 final IconsUriHelper iconsUriHelper = IconsUriHelper.newInstanceForTesting(() -> "pkgId"); 123 iconsUriHelper.getContentUri("com.package.name", 1234); 124 125 final Uri uri = new Uri.Builder() 126 .scheme("content") 127 .authority("invalid.authority") 128 .path("pkgId") 129 .appendPath("1234") 130 .build(); 131 assertThat(iconsUriHelper.getResourceInfo(uri)).isNull(); 132 } 133 } 134 135