Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2016 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.documentsui.testing;
     17 
     18 import static android.content.Intent.EXTRA_INTENT;
     19 import static junit.framework.Assert.assertNotNull;
     20 import static junit.framework.Assert.assertTrue;
     21 import static org.junit.Assert.assertEquals;
     22 
     23 import android.content.Intent;
     24 import android.net.Uri;
     25 import android.os.Parcelable;
     26 
     27 import junit.framework.Assert;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 /**
     33  * Handy-dandy Junit asserts covering Intents.
     34  */
     35 public final class IntentAsserts {
     36 
     37     private IntentAsserts() {}
     38 
     39     public static void assertHasAction(Intent intent, String expected) {
     40         assertEquals(expected, intent.getAction());
     41     }
     42 
     43     public static void assertTargetsComponent(Intent intent, Class<?> expected) {
     44         assertEquals(expected.getName(), intent.getComponent().getClassName());
     45     }
     46 
     47     public static void assertHasExtra(Intent intent, String key) {
     48         assertTrue(intent.getExtras().containsKey(key));
     49     }
     50 
     51     public static Intent assertHasExtraIntent(Intent intent) {
     52         Intent extra = (Intent) intent.getExtra(EXTRA_INTENT);
     53         assertNotNull(extra);
     54         return extra;
     55     }
     56 
     57     public static Uri assertHasExtraUri(Intent intent, String key) {
     58         Object value = intent.getExtra(key);
     59         assertNotNull(value);
     60         assertTrue(value instanceof Uri);
     61         return (Uri) value;
     62     }
     63 
     64     public static List<Parcelable> assertHasExtraList(Intent intent, String key) {
     65         ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(key);
     66         assertNotNull(list);
     67         return list;
     68     }
     69 
     70     public static List<Parcelable> assertHasExtraList(Intent intent, String key, int size) {
     71         List<Parcelable> list = assertHasExtraList(intent, key);
     72         Assert.assertEquals(size, list.size());
     73         return list;
     74     }
     75 
     76     public static void assertHasData(Intent intent, Uri expected) {
     77         assertEquals(expected, intent.getData());
     78     }
     79 }
     80