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 17 package android.service.settings.suggestions; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.graphics.Bitmap; 25 import android.graphics.drawable.Icon; 26 import android.os.Parcel; 27 28 import androidx.test.InstrumentationRegistry; 29 import androidx.test.filters.SmallTest; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @RunWith(AndroidJUnit4.class) 37 @SmallTest 38 public class SuggestionTest { 39 private static final String TEST_ID = "id"; 40 private static final String TEST_TITLE = "title"; 41 private static final String TEST_SUMMARY = "summary"; 42 43 private Icon mIcon; 44 private PendingIntent mTestIntent; 45 46 47 @Before setUp()48 public void setUp() { 49 final Context context = InstrumentationRegistry.getContext(); 50 mTestIntent = PendingIntent.getActivity(context, 0 /* requestCode */, 51 new Intent().setPackage(context.getPackageName()), 52 PendingIntent.FLAG_MUTABLE /* flags */); 53 mIcon = Icon.createWithBitmap(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); 54 } 55 56 @Test buildSuggestion_allFieldsShouldBeSet()57 public void buildSuggestion_allFieldsShouldBeSet() { 58 final Suggestion suggestion = new Suggestion.Builder(TEST_ID) 59 .setTitle(TEST_TITLE) 60 .setSummary(TEST_SUMMARY) 61 .setIcon(mIcon) 62 .setPendingIntent(mTestIntent) 63 .build(); 64 65 assertThat(suggestion.getId()).isEqualTo(TEST_ID); 66 assertThat(suggestion.getTitle()).isEqualTo(TEST_TITLE); 67 assertThat(suggestion.getSummary()).isEqualTo(TEST_SUMMARY); 68 assertThat(suggestion.getIcon()).isEqualTo(mIcon); 69 assertThat(suggestion.getFlags()).isEqualTo(0); 70 assertThat(suggestion.getPendingIntent()).isEqualTo(mTestIntent); 71 } 72 73 @Test(expected = IllegalArgumentException.class) buildSuggestion_emptyKey_shouldCrash()74 public void buildSuggestion_emptyKey_shouldCrash() { 75 new Suggestion.Builder(null) 76 .setTitle(TEST_TITLE) 77 .setSummary(TEST_SUMMARY) 78 .setPendingIntent(mTestIntent) 79 .setIcon(mIcon) 80 .build(); 81 } 82 83 @Test buildSuggestion_fromParcelable()84 public void buildSuggestion_fromParcelable() { 85 final Parcel parcel = Parcel.obtain(); 86 final Suggestion oldSuggestion = new Suggestion.Builder(TEST_ID) 87 .setTitle(TEST_TITLE) 88 .setSummary(TEST_SUMMARY) 89 .setIcon(mIcon) 90 .setFlags(Suggestion.FLAG_HAS_BUTTON) 91 .setPendingIntent(mTestIntent) 92 .build(); 93 94 oldSuggestion.writeToParcel(parcel, 0 /* flags */); 95 parcel.setDataPosition(0); 96 final Suggestion newSuggestion = Suggestion.CREATOR.createFromParcel(parcel); 97 98 assertThat(newSuggestion.getId()).isEqualTo(TEST_ID); 99 assertThat(newSuggestion.getTitle()).isEqualTo(TEST_TITLE); 100 assertThat(newSuggestion.getSummary()).isEqualTo(TEST_SUMMARY); 101 assertThat(newSuggestion.getIcon().toString()).isEqualTo(mIcon.toString()); 102 assertThat(newSuggestion.getFlags()) 103 .isEqualTo(Suggestion.FLAG_HAS_BUTTON); 104 assertThat(newSuggestion.getPendingIntent()).isEqualTo(mTestIntent); 105 } 106 } 107