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.content.Context; 21 import android.graphics.drawable.Drawable; 22 import android.graphics.drawable.Icon; 23 import android.net.Uri; 24 25 import androidx.test.core.app.ApplicationProvider; 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 /** 32 * Validity test for {@link IconsContentProvider}. 33 */ 34 @RunWith(AndroidJUnit4.class) 35 public final class IconsContentProviderTest { 36 37 @Test testLoadResource()38 public void testLoadResource() { 39 final Context context = ApplicationProvider.getApplicationContext(); 40 // Testing with the android package name because this is the only package name 41 // that returns the same uri across multiple classloaders. 42 final String packageName = "android"; 43 final int resId = android.R.drawable.btn_star; 44 final Uri uri = IconsUriHelper.getInstance().getContentUri(packageName, resId); 45 46 final Drawable expected = Icon.createWithResource(packageName, resId).loadDrawable(context); 47 // Ensure we are testing with a non-empty image. 48 assertThat(expected.getIntrinsicWidth()).isGreaterThan(0); 49 assertThat(expected.getIntrinsicHeight()).isGreaterThan(0); 50 51 final Drawable actual = Icon.createWithContentUri(uri).loadDrawable(context); 52 assertThat(actual).isNotNull(); 53 assertThat(IconsContentProvider.sameIcon(actual, expected)).isTrue(); 54 } 55 56 @Test testLoadResource_badUri()57 public void testLoadResource_badUri() { 58 final Uri badUri = new Uri.Builder() 59 .scheme("content") 60 .authority(IconsUriHelper.AUTHORITY) 61 .path("badPackageId") 62 .appendPath("1234") 63 .build(); 64 65 final Context context = ApplicationProvider.getApplicationContext(); 66 assertThat(Icon.createWithContentUri(badUri).loadDrawable(context)).isNull(); 67 } 68 } 69 70