1 /* 2 * Copyright (C) 2025 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 com.android.wallpaper.model; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.WallpaperManager; 22 import android.app.WallpaperManager.SetWallpaperFlags; 23 import android.content.Context; 24 import android.net.Uri; 25 import android.os.Parcel; 26 27 import androidx.test.platform.app.InstrumentationRegistry; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.robolectric.RobolectricTestRunner; 33 34 import java.util.List; 35 36 @RunWith(RobolectricTestRunner.class) 37 public class CurrentWallpaperInfoTest { 38 39 private Context mContext; 40 41 @Before setUp()42 public void setUp() { 43 mContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 44 } 45 46 @Test parcelRoundTrip_succeeds()47 public void parcelRoundTrip_succeeds() { 48 final List<String> attributions = List.of("one", "two"); 49 final String actionUrl = "http://www.bogus.com"; 50 final String collectionId = "collection1"; 51 final @SetWallpaperFlags int flag = WallpaperManager.FLAG_SYSTEM; 52 final Uri imageWallpaperUri = Uri.parse("content://my/content"); 53 CurrentWallpaperInfo saved = new CurrentWallpaperInfo(attributions, actionUrl, collectionId, 54 flag, imageWallpaperUri); 55 56 Parcel parcel = Parcel.obtain(); 57 saved.writeToParcel(parcel, 0); 58 // Reset parcel for reading 59 parcel.setDataPosition(0); 60 CurrentWallpaperInfo restored = CurrentWallpaperInfo.CREATOR.createFromParcel(parcel); 61 62 assertThat(restored.getAttributions(mContext)).containsExactlyElementsIn( 63 attributions).inOrder(); 64 assertThat(restored.getActionUrl(mContext)).isEqualTo(actionUrl); 65 assertThat(restored.getCollectionId(mContext)).isEqualTo(collectionId); 66 assertThat(restored.getWallpaperManagerFlag()).isEqualTo(flag); 67 assertThat(restored.getImageWallpaperUri()).isEqualTo(imageWallpaperUri); 68 } 69 70 @Test 71 // Same as above test, but with all nullable fields set to null parcelRoundTrip_withNulls_succeeds()72 public void parcelRoundTrip_withNulls_succeeds() { 73 final List<String> attributions = List.of("one", "two"); 74 final String actionUrl = "http://www.bogus.com"; 75 final String collectionId = "collection1"; 76 final @SetWallpaperFlags int flag = WallpaperManager.FLAG_SYSTEM; 77 final Uri imageWallpaperUri = null; 78 CurrentWallpaperInfo saved = new CurrentWallpaperInfo(attributions, actionUrl, collectionId, 79 flag, imageWallpaperUri); 80 81 Parcel parcel = Parcel.obtain(); 82 saved.writeToParcel(parcel, 0); 83 // Reset parcel for reading 84 parcel.setDataPosition(0); 85 CurrentWallpaperInfo restored = CurrentWallpaperInfo.CREATOR.createFromParcel(parcel); 86 87 assertThat(restored.getAttributions(mContext)).containsExactlyElementsIn( 88 attributions).inOrder(); 89 assertThat(restored.getActionUrl(mContext)).isEqualTo(actionUrl); 90 assertThat(restored.getCollectionId(mContext)).isEqualTo(collectionId); 91 assertThat(restored.getWallpaperManagerFlag()).isEqualTo(flag); 92 assertThat(restored.getImageWallpaperUri()).isEqualTo(imageWallpaperUri); 93 } 94 } 95