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 android.car.test.util;
17 
18 import static com.google.common.truth.Truth.assertWithMessage;
19 
20 import android.annotation.NonNull;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Bundle;
24 
25 import java.util.Iterator;
26 import java.util.Set;
27 
28 /**
29  * Provides helper methods for core Android classes.
30  */
31 public final class AndroidHelper {
32 
33     /**
34      * Gets a string representing the intent, including its extras.
35      */
toString(@onNull Intent intent)36     public static String toString(@NonNull Intent intent) {
37         return intent.toString() + " Extras: " + toString(intent.getExtras());
38     }
39 
40     /**
41      * Gets a string representing the bundle, including its key-value pairs.
42      */
toString(@onNull Bundle bundle)43     public static String toString(@NonNull Bundle bundle) {
44         Set<String> keySet = bundle.keySet();
45         StringBuilder string = new StringBuilder("Bundle[");
46         if (keySet.isEmpty()) {
47             string.append("empty");
48         } else {
49             string.append(keySet.size()).append(" keys: ");
50             Iterator<String> iterator = keySet.iterator();
51             while (iterator.hasNext()) {
52                 String key = iterator.next();
53                 string.append(key).append('=').append(bundle.get(key));
54                 if (iterator.hasNext()) {
55                     string.append(", ");
56                 }
57             }
58         }
59         return string.append(']').toString();
60     }
61 
62     /**
63      * Asserts that the {@code intentFilter} has the {@code action}.
64      */
assertFilterHasActions(IntentFilter intentFilter, String... actions)65     public static void assertFilterHasActions(IntentFilter intentFilter, String... actions) {
66         for (String action : actions) {
67             assertWithMessage("Filter %s has action %s", intentFilter, action)
68                     .that(intentFilter.hasAction(action)).isTrue();
69         }
70     }
71 
72     /**
73      * Asserts that the {@code intentFilter} has the {@code dataScheme}.
74      */
assertFilterHasDataScheme(IntentFilter intentFilter, String dataScheme)75     public static void assertFilterHasDataScheme(IntentFilter intentFilter, String dataScheme) {
76         assertWithMessage("Filter %s has data scheme %s", intentFilter, dataScheme)
77                 .that(intentFilter.hasDataScheme(dataScheme)).isTrue();
78     }
79 
AndroidHelper()80     private AndroidHelper() {
81         throw new UnsupportedOperationException("contains only static methods");
82     }
83 }
84