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.app; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.net.Uri; 27 import android.os.Parcel; 28 import android.service.wallpaper.WallpaperService; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.List; 38 39 /** 40 * Tests for hidden WallpaperInfo methods. 41 */ 42 @RunWith(AndroidJUnit4.class) 43 @SmallTest 44 public class WallpaperInfoTest { 45 46 @Test testSupportsAmbientMode()47 public void testSupportsAmbientMode() throws Exception { 48 Context context = InstrumentationRegistry.getTargetContext(); 49 50 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); 51 intent.setPackage("com.android.internal.tests"); 52 PackageManager pm = context.getPackageManager(); 53 List<ResolveInfo> result = pm.queryIntentServices(intent, PackageManager.GET_META_DATA); 54 assertEquals(1, result.size()); 55 ResolveInfo info = result.get(0); 56 WallpaperInfo wallpaperInfo = new WallpaperInfo(context, info); 57 58 // Defined as true in the XML 59 assertTrue("supportsAmbientMode should be true, as defined in the XML.", 60 wallpaperInfo.supportsAmbientMode()); 61 Parcel parcel = Parcel.obtain(); 62 wallpaperInfo.writeToParcel(parcel, 0 /* flags */); 63 parcel.setDataPosition(0); 64 WallpaperInfo fromParcel = WallpaperInfo.CREATOR.createFromParcel(parcel); 65 assertTrue("supportsAmbientMode should have been restored from parcelable", 66 fromParcel.supportsAmbientMode()); 67 parcel.recycle(); 68 } 69 70 @Test testGetSettingsSliceUri()71 public void testGetSettingsSliceUri() throws Exception { 72 Context context = InstrumentationRegistry.getTargetContext(); 73 74 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); 75 intent.setPackage("com.android.internal.tests"); 76 PackageManager pm = context.getPackageManager(); 77 List<ResolveInfo> result = pm.queryIntentServices(intent, PackageManager.GET_META_DATA); 78 assertEquals(1, result.size()); 79 ResolveInfo info = result.get(0); 80 WallpaperInfo wallpaperInfo = new WallpaperInfo(context, info); 81 82 // This expected Uri must be the same as that in livewallpaper.xml 83 Uri expectedUri = Uri.parse("content://com.android.internal.tests/slice"); 84 Uri settingsUri = wallpaperInfo.getSettingsSliceUri(); 85 assertEquals("The loaded URI should equal to the string in livewallpaper.xml", 86 0, expectedUri.compareTo(settingsUri)); 87 Parcel parcel = Parcel.obtain(); 88 wallpaperInfo.writeToParcel(parcel, 0 /* flags */); 89 parcel.setDataPosition(0); 90 WallpaperInfo fromParcel = WallpaperInfo.CREATOR.createFromParcel(parcel); 91 assertEquals("settingsSliceUri should be restorable from parcelable", 92 0, expectedUri.compareTo(fromParcel.getSettingsSliceUri())); 93 parcel.recycle(); 94 } 95 } 96 97