Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package com.android.server.pm; |
| 18 | |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
| 20 | import android.content.Intent; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 21 | import android.content.pm.ShortcutInfo; |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Icon; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Parcel; |
| 25 | import android.os.PersistableBundle; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 26 | import android.test.AndroidTestCase; |
| 27 | |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 28 | import com.android.internal.util.Preconditions; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 29 | import com.android.server.testutis.TestUtils; |
| 30 | |
| 31 | /** |
| 32 | * Tests for {@link ShortcutInfo}. |
| 33 | |
| 34 | m FrameworksServicesTests && |
| 35 | adb install \ |
| 36 | -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && |
| 37 | adb shell am instrument -e class com.android.server.pm.ShortcutInfoTest \ |
| 38 | -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 39 | |
| 40 | */ |
| 41 | public class ShortcutInfoTest extends AndroidTestCase { |
| 42 | |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 43 | public void testMissingMandatoryFields() { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 44 | TestUtils.assertExpectException( |
| 45 | IllegalArgumentException.class, |
| 46 | "ID must be provided", |
| 47 | () -> new ShortcutInfo.Builder(mContext).build()); |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 48 | TestUtils.assertExpectException( |
| 49 | IllegalArgumentException.class, |
| 50 | "title must be provided", |
| 51 | () -> new ShortcutInfo.Builder(mContext).setId("id").build() |
| 52 | .enforceMandatoryFields()); |
| 53 | TestUtils.assertExpectException( |
| 54 | NullPointerException.class, |
| 55 | "Intent must be provided", |
| 56 | () -> new ShortcutInfo.Builder(mContext).setId("id").setTitle("x").build() |
| 57 | .enforceMandatoryFields()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 60 | private ShortcutInfo parceled(ShortcutInfo si) { |
| 61 | Parcel p = Parcel.obtain(); |
| 62 | p.writeParcelable(si, 0); |
| 63 | p.setDataPosition(0); |
| 64 | ShortcutInfo si2 = p.readParcelable(getClass().getClassLoader()); |
| 65 | p.recycle(); |
| 66 | return si2; |
| 67 | } |
| 68 | |
| 69 | private Intent makeIntent(String action, Object... bundleKeysAndValues) { |
| 70 | final Intent intent = new Intent(action); |
| 71 | intent.replaceExtras(ShortcutManagerTest.makeBundle(bundleKeysAndValues)); |
| 72 | return intent; |
| 73 | } |
| 74 | |
| 75 | public void testParcel() { |
| 76 | ShortcutInfo si = parceled(new ShortcutInfo.Builder(getContext()) |
| 77 | .setId("id") |
| 78 | .setTitle("title") |
| 79 | .setIntent(makeIntent("action")) |
| 80 | .build()); |
| 81 | assertEquals(getContext().getPackageName(), si.getPackageName()); |
| 82 | assertEquals("id", si.getId()); |
| 83 | assertEquals("title", si.getTitle()); |
| 84 | assertEquals("action", si.getIntent().getAction()); |
| 85 | |
| 86 | PersistableBundle pb = new PersistableBundle(); |
| 87 | pb.putInt("k", 1); |
| 88 | |
| 89 | si = new ShortcutInfo.Builder(getContext()) |
| 90 | .setId("id") |
| 91 | .setActivityComponent(new ComponentName("a", "b")) |
| 92 | .setIcon(Icon.createWithContentUri("content://a.b.c/")) |
| 93 | .setTitle("title") |
| 94 | .setText("text") |
| 95 | .setIntent(makeIntent("action", "key", "val")) |
| 96 | .setWeight(123) |
| 97 | .setExtras(pb) |
| 98 | .build(); |
| 99 | si.addFlags(ShortcutInfo.FLAG_PINNED); |
| 100 | si.setBitmapPath("abc"); |
| 101 | si.setIconResourceId(456); |
| 102 | |
| 103 | si = parceled(si); |
| 104 | |
| 105 | assertEquals(getContext().getPackageName(), si.getPackageName()); |
| 106 | assertEquals("id", si.getId()); |
| 107 | assertEquals(new ComponentName("a", "b"), si.getActivityComponent()); |
| 108 | assertEquals("content://a.b.c/", si.getIcon().getUriString()); |
| 109 | assertEquals("title", si.getTitle()); |
| 110 | assertEquals("text", si.getText()); |
| 111 | assertEquals("action", si.getIntent().getAction()); |
| 112 | assertEquals("val", si.getIntent().getStringExtra("key")); |
| 113 | assertEquals(123, si.getWeight()); |
| 114 | assertEquals(1, si.getExtras().getInt("k")); |
| 115 | |
| 116 | assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags()); |
| 117 | assertEquals("abc", si.getBitmapPath()); |
| 118 | assertEquals(456, si.getIconResourceId()); |
| 119 | } |
| 120 | |
| 121 | public void testClone() { |
| 122 | PersistableBundle pb = new PersistableBundle(); |
| 123 | pb.putInt("k", 1); |
| 124 | ShortcutInfo sorig = new ShortcutInfo.Builder(getContext()) |
| 125 | .setId("id") |
| 126 | .setActivityComponent(new ComponentName("a", "b")) |
| 127 | .setIcon(Icon.createWithContentUri("content://a.b.c/")) |
| 128 | .setTitle("title") |
| 129 | .setText("text") |
| 130 | .setIntent(makeIntent("action", "key", "val")) |
| 131 | .setWeight(123) |
| 132 | .setExtras(pb) |
| 133 | .build(); |
| 134 | sorig.addFlags(ShortcutInfo.FLAG_PINNED); |
| 135 | sorig.setBitmapPath("abc"); |
| 136 | sorig.setIconResourceId(456); |
| 137 | |
| 138 | ShortcutInfo si = sorig.clone(/* clone flags*/ 0); |
| 139 | |
| 140 | assertEquals(getContext().getPackageName(), si.getPackageName()); |
| 141 | assertEquals("id", si.getId()); |
| 142 | assertEquals(new ComponentName("a", "b"), si.getActivityComponent()); |
| 143 | assertEquals("content://a.b.c/", si.getIcon().getUriString()); |
| 144 | assertEquals("title", si.getTitle()); |
| 145 | assertEquals("text", si.getText()); |
| 146 | assertEquals("action", si.getIntent().getAction()); |
| 147 | assertEquals("val", si.getIntent().getStringExtra("key")); |
| 148 | assertEquals(123, si.getWeight()); |
| 149 | assertEquals(1, si.getExtras().getInt("k")); |
| 150 | |
| 151 | assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags()); |
| 152 | assertEquals("abc", si.getBitmapPath()); |
| 153 | assertEquals(456, si.getIconResourceId()); |
| 154 | |
| 155 | si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_CREATOR); |
| 156 | |
| 157 | assertEquals(getContext().getPackageName(), si.getPackageName()); |
| 158 | assertEquals("id", si.getId()); |
| 159 | assertEquals(new ComponentName("a", "b"), si.getActivityComponent()); |
| 160 | assertEquals(null, si.getIcon()); |
| 161 | assertEquals("title", si.getTitle()); |
| 162 | assertEquals("text", si.getText()); |
| 163 | assertEquals("action", si.getIntent().getAction()); |
| 164 | assertEquals("val", si.getIntent().getStringExtra("key")); |
| 165 | assertEquals(123, si.getWeight()); |
| 166 | assertEquals(1, si.getExtras().getInt("k")); |
| 167 | |
| 168 | assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags()); |
| 169 | assertEquals(null, si.getBitmapPath()); |
| 170 | assertEquals(0, si.getIconResourceId()); |
| 171 | |
| 172 | si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER); |
| 173 | |
| 174 | assertEquals(getContext().getPackageName(), si.getPackageName()); |
| 175 | assertEquals("id", si.getId()); |
| 176 | assertEquals(new ComponentName("a", "b"), si.getActivityComponent()); |
| 177 | assertEquals(null, si.getIcon()); |
| 178 | assertEquals("title", si.getTitle()); |
| 179 | assertEquals("text", si.getText()); |
| 180 | assertEquals(null, si.getIntent()); |
| 181 | assertEquals(123, si.getWeight()); |
| 182 | assertEquals(1, si.getExtras().getInt("k")); |
| 183 | |
| 184 | assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags()); |
| 185 | assertEquals(null, si.getBitmapPath()); |
| 186 | assertEquals(0, si.getIconResourceId()); |
| 187 | |
| 188 | si = sorig.clone(ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO); |
| 189 | |
| 190 | assertEquals(getContext().getPackageName(), si.getPackageName()); |
| 191 | assertEquals("id", si.getId()); |
| 192 | assertEquals(null, si.getActivityComponent()); |
| 193 | assertEquals(null, si.getIcon()); |
| 194 | assertEquals(null, si.getTitle()); |
| 195 | assertEquals(null, si.getText()); |
| 196 | assertEquals(null, si.getIntent()); |
| 197 | assertEquals(0, si.getWeight()); |
| 198 | assertEquals(null, si.getExtras()); |
| 199 | |
| 200 | assertEquals(ShortcutInfo.FLAG_PINNED | ShortcutInfo.FLAG_KEY_FIELDS_ONLY, si.getFlags()); |
| 201 | assertEquals(null, si.getBitmapPath()); |
| 202 | assertEquals(0, si.getIconResourceId()); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | public void testCopyNonNullFieldsFrom() { |
| 207 | PersistableBundle pb = new PersistableBundle(); |
| 208 | pb.putInt("k", 1); |
| 209 | ShortcutInfo sorig = new ShortcutInfo.Builder(getContext()) |
| 210 | .setId("id") |
| 211 | .setActivityComponent(new ComponentName("a", "b")) |
| 212 | .setIcon(Icon.createWithContentUri("content://a.b.c/")) |
| 213 | .setTitle("title") |
| 214 | .setText("text") |
| 215 | .setIntent(makeIntent("action", "key", "val")) |
| 216 | .setWeight(123) |
| 217 | .setExtras(pb) |
| 218 | .build(); |
| 219 | sorig.addFlags(ShortcutInfo.FLAG_PINNED); |
| 220 | sorig.setBitmapPath("abc"); |
| 221 | sorig.setIconResourceId(456); |
| 222 | |
| 223 | ShortcutInfo si; |
| 224 | |
| 225 | si = sorig.clone(/* flags=*/ 0); |
| 226 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 227 | .setActivityComponent(new ComponentName("x", "y")).build()); |
| 228 | assertEquals(new ComponentName("x", "y"), si.getActivityComponent()); |
| 229 | |
| 230 | si = sorig.clone(/* flags=*/ 0); |
| 231 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 232 | .setIcon(Icon.createWithContentUri("content://x.y.z/")).build()); |
| 233 | assertEquals("content://x.y.z/", si.getIcon().getUriString()); |
| 234 | |
| 235 | si = sorig.clone(/* flags=*/ 0); |
| 236 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 237 | .setTitle("xyz").build()); |
| 238 | assertEquals("xyz", si.getTitle()); |
| 239 | |
| 240 | si = sorig.clone(/* flags=*/ 0); |
| 241 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 242 | .setText("xxx").build()); |
| 243 | assertEquals("xxx", si.getText()); |
| 244 | |
| 245 | si = sorig.clone(/* flags=*/ 0); |
| 246 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 247 | .setIntent(makeIntent("action2")).build()); |
| 248 | assertEquals("action2", si.getIntent().getAction()); |
| 249 | assertEquals(null, si.getIntent().getStringExtra("key")); |
| 250 | |
| 251 | si = sorig.clone(/* flags=*/ 0); |
| 252 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 253 | .setIntent(makeIntent("action3", "key", "x")).build()); |
| 254 | assertEquals("action3", si.getIntent().getAction()); |
| 255 | assertEquals("x", si.getIntent().getStringExtra("key")); |
| 256 | |
| 257 | si = sorig.clone(/* flags=*/ 0); |
| 258 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 259 | .setWeight(999).build()); |
| 260 | assertEquals(999, si.getWeight()); |
| 261 | |
| 262 | |
| 263 | PersistableBundle pb2 = new PersistableBundle(); |
| 264 | pb2.putInt("x", 99); |
| 265 | |
| 266 | si = sorig.clone(/* flags=*/ 0); |
| 267 | si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getContext()).setId("id") |
| 268 | .setExtras(pb2).build()); |
| 269 | assertEquals(99, si.getExtras().getInt("x")); |
| 270 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 271 | } |