Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.app.cts;
     18 
     19 import android.app.PendingIntent;
     20 import android.app.PendingIntent.CanceledException;
     21 import android.app.stubs.MockReceiver;
     22 import android.app.stubs.MockService;
     23 import android.app.stubs.PendingIntentStubActivity;
     24 import android.content.BroadcastReceiver;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.content.IntentFilter;
     28 import android.os.Bundle;
     29 import android.os.Handler;
     30 import android.os.Looper;
     31 import android.os.Message;
     32 import android.os.Parcel;
     33 import android.os.SystemClock;
     34 import android.test.AndroidTestCase;
     35 import android.util.Log;
     36 
     37 import java.util.concurrent.CountDownLatch;
     38 import java.util.concurrent.TimeUnit;
     39 
     40 public class PendingIntentTest extends AndroidTestCase {
     41 
     42     private static final int WAIT_TIME = 10000;
     43     private PendingIntent mPendingIntent;
     44     private Intent mIntent;
     45     private Context mContext;
     46     private boolean mFinishResult;
     47     private boolean mHandleResult;
     48     private String mResultAction;
     49     private PendingIntent.OnFinished mFinish;
     50     private boolean mLooperStart;
     51     private Looper mLooper;
     52     private Handler mHandler;
     53 
     54     @Override
     55     protected void setUp() throws Exception {
     56         super.setUp();
     57         mContext = getContext();
     58         mFinish = new PendingIntent.OnFinished() {
     59             public void onSendFinished(PendingIntent pi, Intent intent, int resultCode,
     60                     String resultData, Bundle resultExtras) {
     61                 synchronized (mFinish) {
     62                     mFinishResult = true;
     63                     if (intent != null) {
     64                         mResultAction = intent.getAction();
     65                     }
     66                     mFinish.notifyAll();
     67                 }
     68             }
     69         };
     70 
     71         new Thread() {
     72             @Override
     73             public void run() {
     74                 Looper.prepare();
     75                 mLooper = Looper.myLooper();
     76                 mLooperStart = true;
     77                 Looper.loop();
     78             }
     79         }.start();
     80         while (!mLooperStart) {
     81             Thread.sleep(50);
     82         }
     83         mHandler = new Handler(mLooper) {
     84             @Override
     85             public void dispatchMessage(Message msg) {
     86                 synchronized (mFinish) {
     87                     mHandleResult = true;
     88                 }
     89                 super.dispatchMessage(msg);
     90             }
     91 
     92             @Override
     93             public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
     94                 synchronized (mFinish) {
     95                     mHandleResult = true;
     96                 }
     97                 return super.sendMessageAtTime(msg, uptimeMillis);
     98             }
     99 
    100             @Override
    101             public void handleMessage(Message msg) {
    102                 synchronized (mFinish) {
    103                     mHandleResult = true;
    104                 }
    105                 super.handleMessage(msg);
    106             }
    107         };
    108     }
    109 
    110     @Override
    111     protected void tearDown() throws Exception {
    112         super.tearDown();
    113         mLooper.quit();
    114     }
    115 
    116     private void prepareFinish() {
    117         synchronized (mFinish) {
    118             mFinishResult = false;
    119             mHandleResult = false;
    120         }
    121     }
    122 
    123     public boolean waitForFinish(long timeout) {
    124         long now = SystemClock.elapsedRealtime();
    125         final long endTime = now + timeout;
    126         synchronized (mFinish) {
    127             while (!mFinishResult && now < endTime) {
    128                 try {
    129                     mFinish.wait(endTime - now);
    130                 } catch (InterruptedException e) {
    131                 }
    132                 now = SystemClock.elapsedRealtime();
    133             }
    134             return mFinishResult;
    135         }
    136     }
    137 
    138     public void testGetActivity() throws InterruptedException, CanceledException {
    139         PendingIntentStubActivity.prepare();
    140         mPendingIntent = null;
    141         mIntent = new Intent();
    142 
    143         mIntent.setClass(mContext, PendingIntentStubActivity.class);
    144         mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    145         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    146                 PendingIntent.FLAG_CANCEL_CURRENT);
    147         assertEquals(mContext.getPackageName(), mPendingIntent.getTargetPackage());
    148 
    149         mPendingIntent.send();
    150 
    151         PendingIntentStubActivity.waitForCreate(WAIT_TIME);
    152         assertNotNull(mPendingIntent);
    153         assertEquals(PendingIntentStubActivity.status, PendingIntentStubActivity.ON_CREATE);
    154 
    155         // test getActivity return null
    156         mPendingIntent.cancel();
    157         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    158                 PendingIntent.FLAG_NO_CREATE);
    159         assertNull(mPendingIntent);
    160 
    161         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    162                 PendingIntent.FLAG_ONE_SHOT);
    163 
    164         pendingIntentSendError(mPendingIntent);
    165     }
    166 
    167     private void pendingIntentSendError(PendingIntent pendingIntent) {
    168         try {
    169             // From the doc send function will throw CanceledException if the PendingIntent
    170             // is no longer allowing more intents to be sent through it. So here call it twice then
    171             // a CanceledException should be caught.
    172             mPendingIntent.send();
    173             mPendingIntent.send();
    174             fail("CanceledException expected, but not thrown");
    175         } catch (PendingIntent.CanceledException e) {
    176             // expected
    177         }
    178     }
    179 
    180     public void testGetBroadcast() throws InterruptedException, CanceledException {
    181         MockReceiver.prepareReceive(null, 0);
    182         mIntent = new Intent(MockReceiver.MOCKACTION);
    183         mIntent.setClass(mContext, MockReceiver.class);
    184         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent,
    185                 PendingIntent.FLAG_CANCEL_CURRENT);
    186 
    187         mPendingIntent.send();
    188 
    189         MockReceiver.waitForReceive(WAIT_TIME);
    190         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    191 
    192         // test getBroadcast return null
    193         mPendingIntent.cancel();
    194         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent,
    195                 PendingIntent.FLAG_NO_CREATE);
    196         assertNull(mPendingIntent);
    197 
    198         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent,
    199                 PendingIntent.FLAG_ONE_SHOT);
    200 
    201         pendingIntentSendError(mPendingIntent);
    202     }
    203 
    204     // Local receiver for examining delivered broadcast intents
    205     private class ExtraReceiver extends BroadcastReceiver {
    206         private final String extraName;
    207 
    208         public volatile int extra = 0;
    209         public CountDownLatch latch = null;
    210 
    211         public ExtraReceiver(String name) {
    212             extraName = name;
    213         }
    214 
    215         public void onReceive(Context ctx, Intent intent) {
    216             extra = intent.getIntExtra(extraName, 0);
    217             latch.countDown();
    218         }
    219 
    220         public void reset() {
    221             extra = 0;
    222             latch = new CountDownLatch(1);
    223         }
    224 
    225         public boolean waitForReceipt() throws InterruptedException {
    226             return latch.await(WAIT_TIME, TimeUnit.MILLISECONDS);
    227         }
    228     }
    229 
    230     public void testUpdateCurrent() throws InterruptedException, CanceledException {
    231         final int EXTRA_1 = 50;
    232         final int EXTRA_2 = 38;
    233         final String EXTRA_NAME = "test_extra";
    234         final String BROADCAST_ACTION = "testUpdateCurrent_action";
    235 
    236         final Context context = getContext();
    237         final ExtraReceiver br = new ExtraReceiver(EXTRA_NAME);
    238         final IntentFilter filter = new IntentFilter(BROADCAST_ACTION);
    239         context.registerReceiver(br, filter);
    240 
    241         // Baseline: establish that we get the extra properly
    242         PendingIntent pi;
    243         Intent intent = new Intent(BROADCAST_ACTION);
    244         intent.putExtra(EXTRA_NAME, EXTRA_1);
    245 
    246         pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    247 
    248         try {
    249             br.reset();
    250             pi.send();
    251             assertTrue(br.waitForReceipt());
    252             assertTrue(br.extra == EXTRA_1);
    253 
    254             // Change the extra in the Intent
    255             intent.putExtra(EXTRA_NAME, EXTRA_2);
    256 
    257             // Repeat PendingIntent.getBroadcast() *without* UPDATE_CURRENT, so we expect
    258             // the underlying Intent to still be the initial one with EXTRA_1
    259             pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    260             br.reset();
    261             pi.send();
    262             assertTrue(br.waitForReceipt());
    263             assertTrue(br.extra == EXTRA_1);
    264 
    265             // This time use UPDATE_CURRENT, and expect to get the updated extra when the
    266             // PendingIntent is sent
    267             pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    268             br.reset();
    269             pi.send();
    270             assertTrue(br.waitForReceipt());
    271             assertTrue(br.extra == EXTRA_2);
    272         } finally {
    273             pi.cancel();
    274             context.unregisterReceiver(br);
    275         }
    276     }
    277 
    278     public void testGetService() throws InterruptedException, CanceledException {
    279         MockService.prepareStart();
    280         mIntent = new Intent();
    281         mIntent.setClass(mContext, MockService.class);
    282         mPendingIntent = PendingIntent.getService(mContext, 1, mIntent,
    283                 PendingIntent.FLAG_CANCEL_CURRENT);
    284 
    285         mPendingIntent.send();
    286 
    287         MockService.waitForStart(WAIT_TIME);
    288         assertTrue(MockService.result);
    289 
    290         // test getService return null
    291         mPendingIntent.cancel();
    292         mPendingIntent = PendingIntent.getService(mContext, 1, mIntent,
    293                 PendingIntent.FLAG_NO_CREATE);
    294         assertNull(mPendingIntent);
    295 
    296         mPendingIntent = PendingIntent.getService(mContext, 1, mIntent,
    297                 PendingIntent.FLAG_ONE_SHOT);
    298 
    299         pendingIntentSendError(mPendingIntent);
    300     }
    301 
    302     public void testStartServiceOnFinishedHandler() throws InterruptedException, CanceledException {
    303         MockService.prepareStart();
    304         prepareFinish();
    305         mIntent = new Intent();
    306         mIntent.setClass(mContext, MockService.class);
    307         mPendingIntent = PendingIntent.getService(mContext, 1, mIntent,
    308                 PendingIntent.FLAG_CANCEL_CURRENT);
    309 
    310         mPendingIntent.send(mContext, 1, null, mFinish, null);
    311 
    312         MockService.waitForStart(WAIT_TIME);
    313         waitForFinish(WAIT_TIME);
    314         assertTrue(MockService.result);
    315 
    316         assertTrue(mFinishResult);
    317         assertFalse(mHandleResult);
    318         mPendingIntent.cancel();
    319 
    320         MockService.prepareStart();
    321         prepareFinish();
    322         mIntent = new Intent();
    323         mIntent.setClass(mContext, MockService.class);
    324         mPendingIntent = PendingIntent.getService(mContext, 1, mIntent,
    325                 PendingIntent.FLAG_CANCEL_CURRENT);
    326 
    327         mPendingIntent.send(mContext, 1, null, mFinish, mHandler);
    328 
    329         MockService.waitForStart(WAIT_TIME);
    330         waitForFinish(WAIT_TIME);
    331         assertTrue(MockService.result);
    332 
    333         assertTrue(mFinishResult);
    334         assertTrue(mHandleResult);
    335         mPendingIntent.cancel();
    336 
    337     }
    338 
    339     public void testCancel() throws CanceledException {
    340         mIntent = new Intent();
    341         mIntent.setClass(mContext, MockService.class);
    342         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent,
    343                 PendingIntent.FLAG_CANCEL_CURRENT);
    344 
    345         mPendingIntent.send();
    346 
    347         mPendingIntent.cancel();
    348         pendingIntentSendShouldFail(mPendingIntent);
    349     }
    350 
    351     private void pendingIntentSendShouldFail(PendingIntent pendingIntent) {
    352         try {
    353             pendingIntent.send();
    354             fail("CanceledException expected, but not thrown");
    355         } catch (CanceledException e) {
    356             // expected
    357         }
    358     }
    359 
    360     public void testSend() throws InterruptedException, CanceledException {
    361         MockReceiver.prepareReceive(null, -1);
    362         mIntent = new Intent();
    363         mIntent.setAction(MockReceiver.MOCKACTION);
    364         mIntent.setClass(mContext, MockReceiver.class);
    365         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent,
    366                 PendingIntent.FLAG_CANCEL_CURRENT);
    367 
    368         mPendingIntent.send();
    369 
    370         MockReceiver.waitForReceive(WAIT_TIME);
    371 
    372         // send function to send default code 0
    373         assertEquals(0, MockReceiver.sResultCode);
    374         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    375         mPendingIntent.cancel();
    376 
    377         pendingIntentSendShouldFail(mPendingIntent);
    378     }
    379 
    380     public void testSendWithParamInt() throws InterruptedException, CanceledException {
    381         mIntent = new Intent(MockReceiver.MOCKACTION);
    382         mIntent.setClass(mContext, MockReceiver.class);
    383         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent,
    384                 PendingIntent.FLAG_CANCEL_CURRENT);
    385         MockReceiver.prepareReceive(null, 0);
    386         // send result code 1.
    387         mPendingIntent.send(1);
    388         MockReceiver.waitForReceive(WAIT_TIME);
    389         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    390 
    391         // assert the result code
    392         assertEquals(1, MockReceiver.sResultCode);
    393         assertEquals(mResultAction, null);
    394 
    395         MockReceiver.prepareReceive(null, 0);
    396         // send result code 2
    397         mPendingIntent.send(2);
    398         MockReceiver.waitForReceive(WAIT_TIME);
    399 
    400         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    401 
    402         // assert the result code
    403         assertEquals(2, MockReceiver.sResultCode);
    404         assertEquals(MockReceiver.sAction, MockReceiver.MOCKACTION);
    405         assertNull(mResultAction);
    406         mPendingIntent.cancel();
    407         pendingIntentSendShouldFail(mPendingIntent);
    408     }
    409 
    410     public void testSendWithParamContextIntIntent() throws InterruptedException, CanceledException {
    411         mIntent = new Intent(MockReceiver.MOCKACTION);
    412         mIntent.setClass(mContext, MockReceiver.class);
    413 
    414         MockReceiver.prepareReceive(null, 0);
    415 
    416         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    417 
    418         mPendingIntent.send(mContext, 1, null);
    419         MockReceiver.waitForReceive(WAIT_TIME);
    420 
    421         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    422         assertEquals(1, MockReceiver.sResultCode);
    423         mPendingIntent.cancel();
    424 
    425         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    426         MockReceiver.prepareReceive(null, 0);
    427 
    428         mPendingIntent.send(mContext, 2, mIntent);
    429         MockReceiver.waitForReceive(WAIT_TIME);
    430         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    431         assertEquals(2, MockReceiver.sResultCode);
    432         mPendingIntent.cancel();
    433     }
    434 
    435     public void testSendWithParamIntOnFinishedHandler() throws InterruptedException,
    436             CanceledException {
    437         mIntent = new Intent(MockReceiver.MOCKACTION);
    438         mIntent.setClass(mContext, MockReceiver.class);
    439 
    440         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    441         MockReceiver.prepareReceive(null, 0);
    442         prepareFinish();
    443 
    444         mPendingIntent.send(1, null, null);
    445         MockReceiver.waitForReceive(WAIT_TIME);
    446         assertFalse(mFinishResult);
    447         assertFalse(mHandleResult);
    448         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    449 
    450         // assert result code
    451         assertEquals(1, MockReceiver.sResultCode);
    452         mPendingIntent.cancel();
    453 
    454         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    455         MockReceiver.prepareReceive(null, 0);
    456         prepareFinish();
    457 
    458         mPendingIntent.send(2, mFinish, null);
    459         waitForFinish(WAIT_TIME);
    460         assertTrue(mFinishResult);
    461         assertFalse(mHandleResult);
    462         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    463 
    464         // assert result code
    465         assertEquals(2, MockReceiver.sResultCode);
    466         mPendingIntent.cancel();
    467 
    468         MockReceiver.prepareReceive(null, 0);
    469         prepareFinish();
    470         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    471         mPendingIntent.send(3, mFinish, mHandler);
    472         waitForFinish(WAIT_TIME);
    473         assertTrue(mHandleResult);
    474         assertTrue(mFinishResult);
    475         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    476 
    477         // assert result code
    478         assertEquals(3, MockReceiver.sResultCode);
    479         mPendingIntent.cancel();
    480     }
    481 
    482     public void testSendWithParamContextIntIntentOnFinishedHandler() throws InterruptedException,
    483             CanceledException {
    484         mIntent = new Intent(MockReceiver.MOCKACTION);
    485         mIntent.setAction(MockReceiver.MOCKACTION);
    486         mIntent.setClass(getContext(), MockReceiver.class);
    487 
    488         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    489         MockReceiver.prepareReceive(null, 0);
    490         prepareFinish();
    491         mPendingIntent.send(mContext, 1, mIntent, null, null);
    492         MockReceiver.waitForReceive(WAIT_TIME);
    493         assertFalse(mFinishResult);
    494         assertFalse(mHandleResult);
    495         assertNull(mResultAction);
    496         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    497         mPendingIntent.cancel();
    498 
    499         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    500         MockReceiver.prepareReceive(null, 0);
    501         prepareFinish();
    502         mPendingIntent.send(mContext, 1, mIntent, mFinish, null);
    503         waitForFinish(WAIT_TIME);
    504         assertTrue(mFinishResult);
    505         assertEquals(mResultAction, MockReceiver.MOCKACTION);
    506         assertFalse(mHandleResult);
    507         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    508         mPendingIntent.cancel();
    509 
    510         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    511         MockReceiver.prepareReceive(null, 0);
    512         prepareFinish();
    513         mPendingIntent.send(mContext, 1, mIntent, mFinish, mHandler);
    514         waitForFinish(WAIT_TIME);
    515         assertTrue(mHandleResult);
    516         assertEquals(mResultAction, MockReceiver.MOCKACTION);
    517         assertTrue(mFinishResult);
    518         assertEquals(MockReceiver.MOCKACTION, MockReceiver.sAction);
    519         mPendingIntent.cancel();
    520     }
    521 
    522 
    523     public void testSendNoReceiverOnFinishedHandler() throws InterruptedException,
    524             CanceledException {
    525         // This action won't match anything, so no receiver will run but we should
    526         // still get a finish result.
    527         final String BAD_ACTION = MockReceiver.MOCKACTION + "_bad";
    528         mIntent = new Intent(BAD_ACTION);
    529         mIntent.setAction(BAD_ACTION);
    530 
    531         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    532         MockReceiver.prepareReceive(null, 0);
    533         prepareFinish();
    534         mPendingIntent.send(mContext, 1, mIntent, mFinish, null);
    535         waitForFinish(WAIT_TIME);
    536         assertTrue(mFinishResult);
    537         assertEquals(mResultAction, BAD_ACTION);
    538         assertFalse(mHandleResult);
    539         assertNull(MockReceiver.sAction);
    540         mPendingIntent.cancel();
    541 
    542         mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    543         MockReceiver.prepareReceive(null, 0);
    544         prepareFinish();
    545         mPendingIntent.send(mContext, 1, mIntent, mFinish, mHandler);
    546         waitForFinish(WAIT_TIME);
    547         assertTrue(mHandleResult);
    548         assertEquals(mResultAction, BAD_ACTION);
    549         assertTrue(mFinishResult);
    550         assertNull(MockReceiver.sAction);
    551         mPendingIntent.cancel();
    552     }
    553 
    554     public void testGetTargetPackage() {
    555         mIntent = new Intent();
    556         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    557                 PendingIntent.FLAG_CANCEL_CURRENT);
    558         assertEquals(mContext.getPackageName(), mPendingIntent.getTargetPackage());
    559     }
    560 
    561     public void testEquals() {
    562         mIntent = new Intent();
    563         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    564                 PendingIntent.FLAG_CANCEL_CURRENT);
    565 
    566         PendingIntent target = PendingIntent.getActivity(mContext, 1, mIntent,
    567                 PendingIntent.FLAG_CANCEL_CURRENT);
    568 
    569         assertFalse(mPendingIntent.equals(target));
    570         assertFalse(mPendingIntent.hashCode() == target.hashCode());
    571         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent, 1);
    572 
    573         target = PendingIntent.getActivity(mContext, 1, mIntent, 1);
    574         assertTrue(mPendingIntent.equals(target));
    575 
    576         mIntent = new Intent(MockReceiver.MOCKACTION);
    577         target = PendingIntent.getBroadcast(mContext, 1, mIntent, 1);
    578         assertFalse(mPendingIntent.equals(target));
    579         assertFalse(mPendingIntent.hashCode() == target.hashCode());
    580 
    581         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent, 1);
    582         target = PendingIntent.getActivity(mContext, 1, mIntent, 1);
    583 
    584         assertTrue(mPendingIntent.equals(target));
    585         assertEquals(mPendingIntent.hashCode(), target.hashCode());
    586     }
    587 
    588     public void testDescribeContents() {
    589         mIntent = new Intent();
    590         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    591                 PendingIntent.FLAG_CANCEL_CURRENT);
    592         final int expected = 0;
    593         assertEquals(expected, mPendingIntent.describeContents());
    594     }
    595 
    596     public void testWriteToParcel() {
    597         mIntent = new Intent();
    598         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    599                 PendingIntent.FLAG_CANCEL_CURRENT);
    600         Parcel parcel = Parcel.obtain();
    601 
    602         mPendingIntent.writeToParcel(parcel, 0);
    603         parcel.setDataPosition(0);
    604         PendingIntent pendingIntent = PendingIntent.CREATOR.createFromParcel(parcel);
    605         assertTrue(mPendingIntent.equals(pendingIntent));
    606     }
    607 
    608     public void testReadAndWritePendingIntentOrNullToParcel() {
    609         mIntent = new Intent();
    610         mPendingIntent = PendingIntent.getActivity(mContext, 1, mIntent,
    611                 PendingIntent.FLAG_CANCEL_CURRENT);
    612         assertNotNull(mPendingIntent.toString());
    613 
    614         Parcel parcel = Parcel.obtain();
    615         PendingIntent.writePendingIntentOrNullToParcel(mPendingIntent, parcel);
    616         parcel.setDataPosition(0);
    617         PendingIntent target = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
    618         assertEquals(mPendingIntent, target);
    619         assertEquals(mPendingIntent.getTargetPackage(), target.getTargetPackage());
    620 
    621         mPendingIntent = null;
    622         parcel = Parcel.obtain();
    623         PendingIntent.writePendingIntentOrNullToParcel(mPendingIntent, parcel);
    624         target = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
    625         assertNull(target);
    626     }
    627 
    628 }
    629