191b14c4a26c89e02a7e6322188089360bf346081Scott Su/*
291b14c4a26c89e02a7e6322188089360bf346081Scott Su * Copyright (C) 2008 The Android Open Source Project
391b14c4a26c89e02a7e6322188089360bf346081Scott Su *
491b14c4a26c89e02a7e6322188089360bf346081Scott Su * Licensed under the Apache License, Version 2.0 (the "License");
591b14c4a26c89e02a7e6322188089360bf346081Scott Su * you may not use this file except in compliance with the License.
691b14c4a26c89e02a7e6322188089360bf346081Scott Su * You may obtain a copy of the License at
791b14c4a26c89e02a7e6322188089360bf346081Scott Su *
891b14c4a26c89e02a7e6322188089360bf346081Scott Su *      http://www.apache.org/licenses/LICENSE-2.0
991b14c4a26c89e02a7e6322188089360bf346081Scott Su *
1091b14c4a26c89e02a7e6322188089360bf346081Scott Su * Unless required by applicable law or agreed to in writing, software
1191b14c4a26c89e02a7e6322188089360bf346081Scott Su * distributed under the License is distributed on an "AS IS" BASIS,
1291b14c4a26c89e02a7e6322188089360bf346081Scott Su * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1391b14c4a26c89e02a7e6322188089360bf346081Scott Su * See the License for the specific language governing permissions and
1491b14c4a26c89e02a7e6322188089360bf346081Scott Su * limitations under the License.
1591b14c4a26c89e02a7e6322188089360bf346081Scott Su */
1691b14c4a26c89e02a7e6322188089360bf346081Scott Su
1791b14c4a26c89e02a7e6322188089360bf346081Scott Supackage android.app.cts;
1891b14c4a26c89e02a7e6322188089360bf346081Scott Su
1991b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.app.Activity;
2091b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.content.BroadcastReceiver;
2191b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.content.ComponentName;
2291b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.content.Context;
2391b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.content.Intent;
2491b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.content.IntentFilter;
2591b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.Binder;
2691b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.Bundle;
2791b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.Handler;
2891b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.IBinder;
2991b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.Message;
3091b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.Parcel;
3191b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.os.Parcelable;
3291b14c4a26c89e02a7e6322188089360bf346081Scott Suimport android.test.PerformanceTestCase;
33f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsuimport android.util.Log;
34f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
35f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsuimport java.util.ArrayList;
36f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsuimport java.util.HashMap;
37f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsuimport java.util.List;
38f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsuimport java.util.Map;
3991b14c4a26c89e02a7e6322188089360bf346081Scott Su
4091b14c4a26c89e02a7e6322188089360bf346081Scott Suclass MyBadParcelable implements Parcelable {
4191b14c4a26c89e02a7e6322188089360bf346081Scott Su    public MyBadParcelable() {
4291b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
4391b14c4a26c89e02a7e6322188089360bf346081Scott Su
4491b14c4a26c89e02a7e6322188089360bf346081Scott Su    public void writeToParcel(Parcel out, int flags) {
4591b14c4a26c89e02a7e6322188089360bf346081Scott Su        out.writeString("I am bad");
4691b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
4791b14c4a26c89e02a7e6322188089360bf346081Scott Su
4891b14c4a26c89e02a7e6322188089360bf346081Scott Su    public int describeContents() {
4991b14c4a26c89e02a7e6322188089360bf346081Scott Su        return 0;
5091b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
5191b14c4a26c89e02a7e6322188089360bf346081Scott Su
528151e7cd7f60ed986a2132dd0aec271e5fd77b40Phil Dubach    public static final Parcelable.Creator<MyBadParcelable> CREATOR =
5391b14c4a26c89e02a7e6322188089360bf346081Scott Su        new Parcelable.Creator<MyBadParcelable>() {
5491b14c4a26c89e02a7e6322188089360bf346081Scott Su        public MyBadParcelable createFromParcel(Parcel in) {
5591b14c4a26c89e02a7e6322188089360bf346081Scott Su            return new MyBadParcelable(in);
5691b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
5791b14c4a26c89e02a7e6322188089360bf346081Scott Su
5891b14c4a26c89e02a7e6322188089360bf346081Scott Su        public MyBadParcelable[] newArray(int size) {
5991b14c4a26c89e02a7e6322188089360bf346081Scott Su            return new MyBadParcelable[size];
6091b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
6191b14c4a26c89e02a7e6322188089360bf346081Scott Su    };
6291b14c4a26c89e02a7e6322188089360bf346081Scott Su
6391b14c4a26c89e02a7e6322188089360bf346081Scott Su    public MyBadParcelable(Parcel in) {
6491b14c4a26c89e02a7e6322188089360bf346081Scott Su        in.readString();
6591b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
6691b14c4a26c89e02a7e6322188089360bf346081Scott Su}
6791b14c4a26c89e02a7e6322188089360bf346081Scott Su
6891b14c4a26c89e02a7e6322188089360bf346081Scott Supublic class LaunchpadActivity extends Activity {
6991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public interface CallingTest extends PerformanceTestCase.Intermediates {
7091b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void startTiming(boolean realTime);
7191b14c4a26c89e02a7e6322188089360bf346081Scott Su
7291b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void addIntermediate(String name);
7391b14c4a26c89e02a7e6322188089360bf346081Scott Su
7491b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void addIntermediate(String name, long timeInNS);
7591b14c4a26c89e02a7e6322188089360bf346081Scott Su
7691b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void finishTiming(boolean realTime);
7791b14c4a26c89e02a7e6322188089360bf346081Scott Su
7891b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void activityFinished(int resultCode, Intent data, RuntimeException where);
7991b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
8091b14c4a26c89e02a7e6322188089360bf346081Scott Su
8191b14c4a26c89e02a7e6322188089360bf346081Scott Su    // Also used as the Binder interface descriptor string in these tests
8291b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String LAUNCH = "android.app.cts.activity.LAUNCH";
8391b14c4a26c89e02a7e6322188089360bf346081Scott Su
8491b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String FORWARD_RESULT = "android.app.cts.activity.FORWARD_RESULT";
8591b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String RETURNED_RESULT = "android.app.cts.activity.RETURNED_RESULT";
8691b14c4a26c89e02a7e6322188089360bf346081Scott Su
8791b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BAD_PARCELABLE = "android.app.cts.activity.BAD_PARCELABLE";
8891b14c4a26c89e02a7e6322188089360bf346081Scott Su
8991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final int LAUNCHED_RESULT = 1;
9091b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final int FORWARDED_RESULT = 2;
9191b14c4a26c89e02a7e6322188089360bf346081Scott Su
9291b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String LIFECYCLE_BASIC = "android.app.cts.activity.LIFECYCLE_BASIC";
9391b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String LIFECYCLE_SCREEN = "android.app.cts.activity.LIFECYCLE_SCREEN";
9491b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String LIFECYCLE_DIALOG = "android.app.cts.activity.LIFECYCLE_DIALOG";
9591b14c4a26c89e02a7e6322188089360bf346081Scott Su
9691b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_REGISTERED = "android.app.cts.activity.BROADCAST_REGISTERED";
9791b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_LOCAL = "android.app.cts.activity.BROADCAST_LOCAL";
9891b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_REMOTE = "android.app.cts.activity.BROADCAST_REMOTE";
9991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_ALL = "android.app.cts.activity.BROADCAST_ALL";
10091b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_REPEAT = "android.app.cts.activity.BROADCAST_REPEAT";
10191b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_MULTI = "android.app.cts.activity.BROADCAST_MULTI";
10291b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_ABORT = "android.app.cts.activity.BROADCAST_ABORT";
10391b14c4a26c89e02a7e6322188089360bf346081Scott Su
10491b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String EXPANDLIST_SELECT = "EXPANDLIST_SELECT";
10591b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String EXPANDLIST_VIEW = "EXPANDLIST_VIEW";
10691b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String EXPANDLIST_CALLBACK = "EXPANDLIST_CALLBACK";
10791b14c4a26c89e02a7e6322188089360bf346081Scott Su
10891b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_STICKY1 = "android.app.cts.activity.BROADCAST_STICKY1";
10991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String BROADCAST_STICKY2 = "android.app.cts.activity.BROADCAST_STICKY2";
11091b14c4a26c89e02a7e6322188089360bf346081Scott Su
11191b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String ALIAS_ACTIVITY = "android.app.cts.activity.ALIAS_ACTIVITY";
11291b14c4a26c89e02a7e6322188089360bf346081Scott Su
11391b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String RECEIVER_REG = "receiver-reg";
11491b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String RECEIVER_LOCAL = "receiver-local";
11591b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String RECEIVER_REMOTE = "receiver-remote";
11691b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String RECEIVER_ABORT = "receiver-abort";
11791b14c4a26c89e02a7e6322188089360bf346081Scott Su
11891b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String DATA_1 = "one";
11991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String DATA_2 = "two";
12091b14c4a26c89e02a7e6322188089360bf346081Scott Su
12191b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String ON_START = "onStart";
12291b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String ON_RESTART = "onRestart";
12391b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String ON_RESUME = "onResume";
12491b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String ON_FREEZE = "onSaveInstanceState";
12591b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String ON_PAUSE = "onPause";
126f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
127f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    // ON_STOP and ON_DESTROY are not tested because they may not be called.
12891b14c4a26c89e02a7e6322188089360bf346081Scott Su
12991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String DO_FINISH = "finish";
13091b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String DO_LOCAL_SCREEN = "local-screen";
13191b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static final String DO_LOCAL_DIALOG = "local-dialog";
13291b14c4a26c89e02a7e6322188089360bf346081Scott Su
133f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private static final String TAG = "LaunchpadActivity";
134f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
13591b14c4a26c89e02a7e6322188089360bf346081Scott Su    private boolean mBadParcelable = false;
13691b14c4a26c89e02a7e6322188089360bf346081Scott Su
13791b14c4a26c89e02a7e6322188089360bf346081Scott Su    private boolean mStarted = false;
13891b14c4a26c89e02a7e6322188089360bf346081Scott Su
13991b14c4a26c89e02a7e6322188089360bf346081Scott Su    private int mResultCode = RESULT_CANCELED;
14091b14c4a26c89e02a7e6322188089360bf346081Scott Su    private Intent mData = new Intent().setAction("No result received");
14191b14c4a26c89e02a7e6322188089360bf346081Scott Su    private RuntimeException mResultStack = null;
14291b14c4a26c89e02a7e6322188089360bf346081Scott Su
143f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    /** Index into the {@link #mNextLifecycle} array. */
14491b14c4a26c89e02a7e6322188089360bf346081Scott Su    private int mNextLifecycle;
14591b14c4a26c89e02a7e6322188089360bf346081Scott Su
146f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    /** Current lifecycle expected to be followed. */
147f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private String[] mExpectedLifecycle;
148f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
149f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    /** Other possible lifecycles. Never includes the current {@link #mExpectedLifecycle}. */
150f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private List<String[]> mOtherPossibleLifecycles = new ArrayList<String[]>(2);
151f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
152f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    /** Map from lifecycle arrays to debugging log names. */
153f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private Map<String[], String> mLifecycleNames = new HashMap<String[], String>(2);
154f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
15591b14c4a26c89e02a7e6322188089360bf346081Scott Su    private String[] mExpectedReceivers = null;
15691b14c4a26c89e02a7e6322188089360bf346081Scott Su    private int mNextReceiver;
15791b14c4a26c89e02a7e6322188089360bf346081Scott Su
15891b14c4a26c89e02a7e6322188089360bf346081Scott Su    private String[] mExpectedData = null;
15991b14c4a26c89e02a7e6322188089360bf346081Scott Su    private boolean[] mReceivedData = null;
16091b14c4a26c89e02a7e6322188089360bf346081Scott Su
16191b14c4a26c89e02a7e6322188089360bf346081Scott Su    boolean mReceiverRegistered = false;
16291b14c4a26c89e02a7e6322188089360bf346081Scott Su
16391b14c4a26c89e02a7e6322188089360bf346081Scott Su    private static CallingTest sCallingTest = null;
16491b14c4a26c89e02a7e6322188089360bf346081Scott Su
16591b14c4a26c89e02a7e6322188089360bf346081Scott Su    public static void setCallingTest(CallingTest ct) {
16691b14c4a26c89e02a7e6322188089360bf346081Scott Su        sCallingTest = ct;
16791b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
16891b14c4a26c89e02a7e6322188089360bf346081Scott Su
16991b14c4a26c89e02a7e6322188089360bf346081Scott Su    public LaunchpadActivity() {
17091b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
17191b14c4a26c89e02a7e6322188089360bf346081Scott Su
17291b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
17391b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onCreate(Bundle icicle) {
17491b14c4a26c89e02a7e6322188089360bf346081Scott Su        super.onCreate(icicle);
175f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
176f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        resetLifecycles();
177f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
178f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        // ON_STOP and ON_DESTROY are not tested because they may not be called.
179f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
18091b14c4a26c89e02a7e6322188089360bf346081Scott Su        final String action = getIntent().getAction();
18191b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (LIFECYCLE_BASIC.equals(action)) {
182f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            addPossibleLifecycle(LIFECYCLE_BASIC, new String[] {
183f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    ON_START, ON_RESUME, DO_FINISH, ON_PAUSE
18491b14c4a26c89e02a7e6322188089360bf346081Scott Su            });
18591b14c4a26c89e02a7e6322188089360bf346081Scott Su        } else if (LIFECYCLE_SCREEN.equals(action)) {
186f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            addPossibleLifecycle(LIFECYCLE_SCREEN + "_RESTART", new String[] {
18733f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu                    ON_START, ON_RESUME, DO_LOCAL_SCREEN, ON_PAUSE,
188f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    ON_RESTART, ON_START, ON_RESUME, DO_FINISH, ON_PAUSE
189f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            });
190f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            addPossibleLifecycle(LIFECYCLE_SCREEN + "_RESUME", new String[] {
19133f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu                    ON_START, ON_RESUME, DO_LOCAL_SCREEN, ON_PAUSE,
192f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    ON_RESUME, DO_FINISH, ON_PAUSE
19391b14c4a26c89e02a7e6322188089360bf346081Scott Su            });
19491b14c4a26c89e02a7e6322188089360bf346081Scott Su        } else if (LIFECYCLE_DIALOG.equals(action)) {
195f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            addPossibleLifecycle(LIFECYCLE_DIALOG + "_RESTART", new String[] {
19633f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu                    ON_START, ON_RESUME, DO_LOCAL_DIALOG, ON_PAUSE,
197f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    ON_RESTART, ON_START, ON_RESUME, DO_FINISH, ON_PAUSE
19891b14c4a26c89e02a7e6322188089360bf346081Scott Su            });
199f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            addPossibleLifecycle(LIFECYCLE_DIALOG + "_RESUME", new String[] {
20033f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu                    ON_START, ON_RESUME, DO_LOCAL_DIALOG, ON_PAUSE,
201f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    ON_RESUME, DO_FINISH, ON_PAUSE
20291b14c4a26c89e02a7e6322188089360bf346081Scott Su            });
20391b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
20491b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
20591b14c4a26c89e02a7e6322188089360bf346081Scott Su
206f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private void resetLifecycles() {
207f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mNextLifecycle = 0;
208f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mExpectedLifecycle = null;
209f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mOtherPossibleLifecycles.clear();
210f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mLifecycleNames.clear();
211f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    }
212f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
213f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    /**
214f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * Add a potential lifecycle that this activity may follow, since there
215f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * are usually multiple valid lifecycles. For instance, sometimes onPause
216f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * will lead to onResume rather than onStop when another activity is
217f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * raised over the current one.
218f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     *
219f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * @param debugName for the lifecycle shown in the logs
220f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * @param lifecycle array containing tokens indicating the expected lifecycle
221f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     */
222f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private void addPossibleLifecycle(String debugName, String[] lifecycle) {
223f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mLifecycleNames.put(lifecycle, debugName);
224f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        if (mExpectedLifecycle == null) {
225f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            mExpectedLifecycle = lifecycle;
226f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        } else {
227f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            mOtherPossibleLifecycles.add(lifecycle);
228f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        }
229f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    }
230f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
231f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    /**
232f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * Switch to the next possible lifecycle and return if switching was
233f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * successful. Call this method when mExpectedLifecycle doesn't match
234f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * the current lifecycle and you need to check another possible lifecycle.
235f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     *
236f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     * @return whether on not there was a lifecycle to switch to
237f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu     */
238f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    private boolean switchToNextPossibleLifecycle() {
239f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        if (!mOtherPossibleLifecycles.isEmpty()) {
240f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            String[] newLifecycle = mOtherPossibleLifecycles.remove(0);
241f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            Log.w(TAG, "Switching expected lifecycles from "
242f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    + mLifecycleNames.get(mExpectedLifecycle) + " to "
243f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    + mLifecycleNames.get(newLifecycle));
244f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            mExpectedLifecycle = newLifecycle;
245f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            return true;
246f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        } else {
247f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            Log.w(TAG, "No more lifecycles after "
248f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    + mLifecycleNames.get(mExpectedLifecycle));
249f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            mExpectedLifecycle = null;
250f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            return false;
251f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        }
252f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu    }
253f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
25491b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
25591b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onStart() {
25691b14c4a26c89e02a7e6322188089360bf346081Scott Su        super.onStart();
25791b14c4a26c89e02a7e6322188089360bf346081Scott Su        checkLifecycle(ON_START);
25891b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
25991b14c4a26c89e02a7e6322188089360bf346081Scott Su
26091b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
26191b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onRestart() {
26291b14c4a26c89e02a7e6322188089360bf346081Scott Su        super.onStart();
26391b14c4a26c89e02a7e6322188089360bf346081Scott Su        checkLifecycle(ON_RESTART);
26491b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
26591b14c4a26c89e02a7e6322188089360bf346081Scott Su
26691b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
26791b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onResume() {
26891b14c4a26c89e02a7e6322188089360bf346081Scott Su        super.onResume();
26991b14c4a26c89e02a7e6322188089360bf346081Scott Su
27091b14c4a26c89e02a7e6322188089360bf346081Scott Su        checkLifecycle(ON_RESUME);
27191b14c4a26c89e02a7e6322188089360bf346081Scott Su
27291b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (!mStarted) {
27391b14c4a26c89e02a7e6322188089360bf346081Scott Su            mStarted = true;
27491b14c4a26c89e02a7e6322188089360bf346081Scott Su
2751da4b3ce9249c5ec2713d1ad31913e1fcdae0d7eKeun young Park            mHandler.postDelayed(mTimeout, 10 * 1000);
27691b14c4a26c89e02a7e6322188089360bf346081Scott Su
27791b14c4a26c89e02a7e6322188089360bf346081Scott Su            final String action = getIntent().getAction();
27891b14c4a26c89e02a7e6322188089360bf346081Scott Su
27991b14c4a26c89e02a7e6322188089360bf346081Scott Su            sCallingTest.startTiming(true);
28091b14c4a26c89e02a7e6322188089360bf346081Scott Su
28191b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (LAUNCH.equals(action)) {
28291b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
28391b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
28491b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setComponent((ComponentName) intent.getParcelableExtra("component"));
28591b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, LAUNCHED_RESULT);
28691b14c4a26c89e02a7e6322188089360bf346081Scott Su
28791b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (FORWARD_RESULT.equals(action)) {
28891b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
28991b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
29091b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setClass(this, LocalScreen.class);
29191b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, FORWARDED_RESULT);
29291b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BAD_PARCELABLE.equals(action)) {
29391b14c4a26c89e02a7e6322188089360bf346081Scott Su                mBadParcelable = true;
29491b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
29591b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
29691b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setClass(this, LocalScreen.class);
29791b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, LAUNCHED_RESULT);
29891b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_REGISTERED.equals(action)) {
29991b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
30091b14c4a26c89e02a7e6322188089360bf346081Scott Su                    RECEIVER_REG
30191b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
30291b14c4a26c89e02a7e6322188089360bf346081Scott Su                registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED));
30391b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("after-register");
30491b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendBroadcast(makeBroadcastIntent(BROADCAST_REGISTERED));
30591b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_LOCAL.equals(action)) {
30691b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
30791b14c4a26c89e02a7e6322188089360bf346081Scott Su                    RECEIVER_LOCAL
30891b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
30991b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendBroadcast(makeBroadcastIntent(BROADCAST_LOCAL));
31091b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_REMOTE.equals(action)) {
31191b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
31291b14c4a26c89e02a7e6322188089360bf346081Scott Su                    RECEIVER_REMOTE
31391b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
31491b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendBroadcast(makeBroadcastIntent(BROADCAST_REMOTE));
31591b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_ALL.equals(action)) {
31691b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
31791b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REMOTE, RECEIVER_REG, RECEIVER_LOCAL
31891b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
31991b14c4a26c89e02a7e6322188089360bf346081Scott Su                registerMyReceiver(new IntentFilter(BROADCAST_ALL));
32091b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("after-register");
32191b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
32291b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_MULTI.equals(action)) {
32391b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
32491b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REMOTE, RECEIVER_REG, RECEIVER_LOCAL, RECEIVER_REMOTE,
32591b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REG, RECEIVER_LOCAL, RECEIVER_REMOTE, RECEIVER_REG,
32691b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_LOCAL, RECEIVER_LOCAL, RECEIVER_REMOTE, RECEIVER_LOCAL,
32791b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REMOTE, RECEIVER_REMOTE, RECEIVER_REG, RECEIVER_LOCAL,
32891b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REMOTE, RECEIVER_REG, RECEIVER_LOCAL, RECEIVER_REMOTE,
32991b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REG, RECEIVER_LOCAL, RECEIVER_REMOTE, RECEIVER_LOCAL,
33091b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REMOTE, RECEIVER_LOCAL
33191b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
33291b14c4a26c89e02a7e6322188089360bf346081Scott Su                registerMyReceiver(new IntentFilter(BROADCAST_ALL));
33391b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("after-register");
33491b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
33591b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
33691b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
33791b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_LOCAL), null);
33891b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_REMOTE), null);
33991b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_LOCAL), null);
34091b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_REMOTE), null);
34191b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
34291b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
34391b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ALL), null);
34491b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_REPEAT), null);
34591b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_ABORT.equals(action)) {
34691b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
34791b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REMOTE, RECEIVER_ABORT
34891b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
34991b14c4a26c89e02a7e6322188089360bf346081Scott Su                registerMyReceiver(new IntentFilter(BROADCAST_ABORT));
35091b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("after-register");
35191b14c4a26c89e02a7e6322188089360bf346081Scott Su                sendOrderedBroadcast(makeBroadcastIntent(BROADCAST_ABORT), null);
35291b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_STICKY1.equals(action)) {
35391b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
35491b14c4a26c89e02a7e6322188089360bf346081Scott Su                    RECEIVER_REG
35591b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
35691b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedData(new String[] {
35791b14c4a26c89e02a7e6322188089360bf346081Scott Su                    DATA_1
35891b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
35991b14c4a26c89e02a7e6322188089360bf346081Scott Su                registerMyReceiver(new IntentFilter(BROADCAST_STICKY1));
36091b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("after-register");
36191b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (BROADCAST_STICKY2.equals(action)) {
36291b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedReceivers(new String[] {
36391b14c4a26c89e02a7e6322188089360bf346081Scott Su                        RECEIVER_REG, RECEIVER_REG
36491b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
36591b14c4a26c89e02a7e6322188089360bf346081Scott Su                setExpectedData(new String[] {
36691b14c4a26c89e02a7e6322188089360bf346081Scott Su                        DATA_1, DATA_2
36791b14c4a26c89e02a7e6322188089360bf346081Scott Su                });
36891b14c4a26c89e02a7e6322188089360bf346081Scott Su                final IntentFilter filter = new IntentFilter(BROADCAST_STICKY1);
36991b14c4a26c89e02a7e6322188089360bf346081Scott Su                filter.addAction(BROADCAST_STICKY2);
37091b14c4a26c89e02a7e6322188089360bf346081Scott Su                registerMyReceiver(filter);
37191b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("after-register");
37291b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (ALIAS_ACTIVITY.equals(action)) {
37391b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
37491b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
37591b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setClass(this, AliasActivityStub.class);
37691b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, LAUNCHED_RESULT);
37791b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (EXPANDLIST_SELECT.equals(action)) {
37891b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
37991b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
38091b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setAction(action);
38191b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setComponent((ComponentName) intent.getParcelableExtra("component"));
38291b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, LAUNCHED_RESULT);
38391b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (EXPANDLIST_VIEW.equals(action)) {
38491b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
38591b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
38691b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setAction(action);
38791b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setComponent((ComponentName) intent.getParcelableExtra("component"));
38891b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, LAUNCHED_RESULT);
38991b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (EXPANDLIST_CALLBACK.equals(action)) {
39091b14c4a26c89e02a7e6322188089360bf346081Scott Su                final Intent intent = getIntent();
39191b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setFlags(0);
39291b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setAction(action);
39391b14c4a26c89e02a7e6322188089360bf346081Scott Su                intent.setComponent((ComponentName) intent.getParcelableExtra("component"));
39491b14c4a26c89e02a7e6322188089360bf346081Scott Su                startActivityForResult(intent, LAUNCHED_RESULT);
39591b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
39691b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
39791b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
39891b14c4a26c89e02a7e6322188089360bf346081Scott Su
39991b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
40091b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onSaveInstanceState(Bundle icicle) {
40191b14c4a26c89e02a7e6322188089360bf346081Scott Su        super.onSaveInstanceState(icicle);
40291b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (mBadParcelable) {
40391b14c4a26c89e02a7e6322188089360bf346081Scott Su            icicle.putParcelable("baddy", new MyBadParcelable());
40491b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
40591b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
40691b14c4a26c89e02a7e6322188089360bf346081Scott Su
40791b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
40891b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onPause() {
40991b14c4a26c89e02a7e6322188089360bf346081Scott Su        super.onPause();
41091b14c4a26c89e02a7e6322188089360bf346081Scott Su        checkLifecycle(ON_PAUSE);
41191b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
41291b14c4a26c89e02a7e6322188089360bf346081Scott Su
41391b14c4a26c89e02a7e6322188089360bf346081Scott Su    @Override
41491b14c4a26c89e02a7e6322188089360bf346081Scott Su    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
41591b14c4a26c89e02a7e6322188089360bf346081Scott Su        switch (requestCode) {
41691b14c4a26c89e02a7e6322188089360bf346081Scott Su            case LAUNCHED_RESULT:
41791b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.finishTiming(true);
41891b14c4a26c89e02a7e6322188089360bf346081Scott Su                finishWithResult(resultCode, data);
41991b14c4a26c89e02a7e6322188089360bf346081Scott Su                break;
42091b14c4a26c89e02a7e6322188089360bf346081Scott Su            case FORWARDED_RESULT:
42191b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.finishTiming(true);
42291b14c4a26c89e02a7e6322188089360bf346081Scott Su                if (RETURNED_RESULT.equals(data.getAction())) {
42391b14c4a26c89e02a7e6322188089360bf346081Scott Su                    finishWithResult(resultCode, data);
42491b14c4a26c89e02a7e6322188089360bf346081Scott Su                } else {
42591b14c4a26c89e02a7e6322188089360bf346081Scott Su                    finishWithResult(RESULT_CANCELED, new Intent().setAction("Bad data returned: "
42691b14c4a26c89e02a7e6322188089360bf346081Scott Su                            + data));
42791b14c4a26c89e02a7e6322188089360bf346081Scott Su                }
42891b14c4a26c89e02a7e6322188089360bf346081Scott Su                break;
42991b14c4a26c89e02a7e6322188089360bf346081Scott Su            default:
43091b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.finishTiming(true);
43191b14c4a26c89e02a7e6322188089360bf346081Scott Su                finishWithResult(RESULT_CANCELED, new Intent()
43291b14c4a26c89e02a7e6322188089360bf346081Scott Su                        .setAction("Unexpected request code: " + requestCode));
43391b14c4a26c89e02a7e6322188089360bf346081Scott Su                break;
43491b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
43591b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
43691b14c4a26c89e02a7e6322188089360bf346081Scott Su
43791b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void checkLifecycle(String where) {
438f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        String action = getIntent().getAction();
439f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
44091b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (mExpectedLifecycle == null) {
44191b14c4a26c89e02a7e6322188089360bf346081Scott Su            return;
44291b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
44391b14c4a26c89e02a7e6322188089360bf346081Scott Su
44491b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (mNextLifecycle >= mExpectedLifecycle.length) {
445f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            finishBad("Activity lifecycle for " + action + " incorrect: received " + where
44691b14c4a26c89e02a7e6322188089360bf346081Scott Su                    + " but don't expect any more calls");
44791b14c4a26c89e02a7e6322188089360bf346081Scott Su            mExpectedLifecycle = null;
44891b14c4a26c89e02a7e6322188089360bf346081Scott Su            return;
44991b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
450f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
451f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        do {
452f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            if (mExpectedLifecycle[mNextLifecycle].equals(where)) {
45333f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu                Log.w(TAG, "Matched: " + where);
454f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                break;
45533f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu            } else {
45633f825300d0827e8eb8ff7bffe81954fd4e12359Brian Muramatsu                Log.w(TAG, "Expected " + mExpectedLifecycle[mNextLifecycle] + " but got " + where);
457f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            }
458f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        } while (switchToNextPossibleLifecycle());
459f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
460f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        if (mExpectedLifecycle == null) {
461f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            finishBad("Activity lifecycle for " + action + " incorrect: received " + where
462f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu                    + " at " + mNextLifecycle);
46391b14c4a26c89e02a7e6322188089360bf346081Scott Su            return;
46491b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
46591b14c4a26c89e02a7e6322188089360bf346081Scott Su
46691b14c4a26c89e02a7e6322188089360bf346081Scott Su        mNextLifecycle++;
46791b14c4a26c89e02a7e6322188089360bf346081Scott Su
46891b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (mNextLifecycle >= mExpectedLifecycle.length) {
469f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu            finishGood();
47091b14c4a26c89e02a7e6322188089360bf346081Scott Su            return;
47191b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
47291b14c4a26c89e02a7e6322188089360bf346081Scott Su
47391b14c4a26c89e02a7e6322188089360bf346081Scott Su        final String next = mExpectedLifecycle[mNextLifecycle];
474f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        if (next.equals(DO_FINISH)) {
47591b14c4a26c89e02a7e6322188089360bf346081Scott Su            mNextLifecycle++;
47691b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (mNextLifecycle >= mExpectedLifecycle.length) {
47791b14c4a26c89e02a7e6322188089360bf346081Scott Su                setTestResult(RESULT_OK, null);
47891b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
47991b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (!isFinishing()) {
48091b14c4a26c89e02a7e6322188089360bf346081Scott Su                finish();
48191b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
48291b14c4a26c89e02a7e6322188089360bf346081Scott Su        } else if (next.equals(DO_LOCAL_SCREEN)) {
48391b14c4a26c89e02a7e6322188089360bf346081Scott Su            mNextLifecycle++;
48491b14c4a26c89e02a7e6322188089360bf346081Scott Su            final Intent intent = new Intent(TestedScreen.WAIT_BEFORE_FINISH);
48591b14c4a26c89e02a7e6322188089360bf346081Scott Su            intent.setClass(this, LocalScreen.class);
48691b14c4a26c89e02a7e6322188089360bf346081Scott Su            startActivity(intent);
48791b14c4a26c89e02a7e6322188089360bf346081Scott Su        } else if (next.equals(DO_LOCAL_DIALOG)) {
48891b14c4a26c89e02a7e6322188089360bf346081Scott Su            mNextLifecycle++;
48991b14c4a26c89e02a7e6322188089360bf346081Scott Su            final Intent intent = new Intent(TestedScreen.WAIT_BEFORE_FINISH);
49091b14c4a26c89e02a7e6322188089360bf346081Scott Su            intent.setClass(this, LocalDialog.class);
49191b14c4a26c89e02a7e6322188089360bf346081Scott Su            startActivity(intent);
49291b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
49391b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
49491b14c4a26c89e02a7e6322188089360bf346081Scott Su
49591b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void setExpectedReceivers(String[] receivers) {
49691b14c4a26c89e02a7e6322188089360bf346081Scott Su        mExpectedReceivers = receivers;
49791b14c4a26c89e02a7e6322188089360bf346081Scott Su        mNextReceiver = 0;
49891b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
49991b14c4a26c89e02a7e6322188089360bf346081Scott Su
50091b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void setExpectedData(String[] data) {
50191b14c4a26c89e02a7e6322188089360bf346081Scott Su        mExpectedData = data;
50291b14c4a26c89e02a7e6322188089360bf346081Scott Su        mReceivedData = new boolean[data.length];
50391b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
50491b14c4a26c89e02a7e6322188089360bf346081Scott Su
50591b14c4a26c89e02a7e6322188089360bf346081Scott Su    @SuppressWarnings("deprecation")
50691b14c4a26c89e02a7e6322188089360bf346081Scott Su    private Intent makeBroadcastIntent(String action) {
50791b14c4a26c89e02a7e6322188089360bf346081Scott Su        final Intent intent = new Intent(action, null);
50891b14c4a26c89e02a7e6322188089360bf346081Scott Su        intent.putExtra("caller", mCallTarget);
50991b14c4a26c89e02a7e6322188089360bf346081Scott Su        return intent;
51091b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
51191b14c4a26c89e02a7e6322188089360bf346081Scott Su
51291b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void finishGood() {
51391b14c4a26c89e02a7e6322188089360bf346081Scott Su        finishWithResult(RESULT_OK, null);
51491b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
51591b14c4a26c89e02a7e6322188089360bf346081Scott Su
51691b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void finishBad(String error) {
51791b14c4a26c89e02a7e6322188089360bf346081Scott Su        finishWithResult(RESULT_CANCELED, new Intent().setAction(error));
51891b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
51991b14c4a26c89e02a7e6322188089360bf346081Scott Su
52091b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void finishWithResult(int resultCode, Intent data) {
52191b14c4a26c89e02a7e6322188089360bf346081Scott Su        setTestResult(resultCode, data);
52291b14c4a26c89e02a7e6322188089360bf346081Scott Su        finish();
523f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu
524f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        // Member fields set by calling setTestResult above...
525f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        sCallingTest.activityFinished(mResultCode, mData, mResultStack);
52691b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
52791b14c4a26c89e02a7e6322188089360bf346081Scott Su
52891b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void setTestResult(int resultCode, Intent data) {
52991b14c4a26c89e02a7e6322188089360bf346081Scott Su        mHandler.removeCallbacks(mTimeout);
53091b14c4a26c89e02a7e6322188089360bf346081Scott Su        unregisterMyReceiver();
53191b14c4a26c89e02a7e6322188089360bf346081Scott Su        mResultCode = resultCode;
53291b14c4a26c89e02a7e6322188089360bf346081Scott Su        mData = data;
53391b14c4a26c89e02a7e6322188089360bf346081Scott Su        mResultStack = new RuntimeException("Original error was here");
53491b14c4a26c89e02a7e6322188089360bf346081Scott Su        mResultStack.fillInStackTrace();
53591b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
53691b14c4a26c89e02a7e6322188089360bf346081Scott Su
53791b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void registerMyReceiver(IntentFilter filter) {
53891b14c4a26c89e02a7e6322188089360bf346081Scott Su        mReceiverRegistered = true;
53991b14c4a26c89e02a7e6322188089360bf346081Scott Su        registerReceiver(mReceiver, filter);
54091b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
54191b14c4a26c89e02a7e6322188089360bf346081Scott Su
54291b14c4a26c89e02a7e6322188089360bf346081Scott Su    private void unregisterMyReceiver() {
54391b14c4a26c89e02a7e6322188089360bf346081Scott Su        if (mReceiverRegistered) {
54491b14c4a26c89e02a7e6322188089360bf346081Scott Su            mReceiverRegistered = false;
54591b14c4a26c89e02a7e6322188089360bf346081Scott Su            unregisterReceiver(mReceiver);
54691b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
54791b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
54891b14c4a26c89e02a7e6322188089360bf346081Scott Su
54991b14c4a26c89e02a7e6322188089360bf346081Scott Su    private final Handler mHandler = new Handler() {
55091b14c4a26c89e02a7e6322188089360bf346081Scott Su        @Override
55191b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void handleMessage(Message msg) {
55291b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
55391b14c4a26c89e02a7e6322188089360bf346081Scott Su    };
55491b14c4a26c89e02a7e6322188089360bf346081Scott Su
55591b14c4a26c89e02a7e6322188089360bf346081Scott Su    static final int GOT_RECEIVE_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION;
55691b14c4a26c89e02a7e6322188089360bf346081Scott Su    static final int ERROR_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 1;
55791b14c4a26c89e02a7e6322188089360bf346081Scott Su
55891b14c4a26c89e02a7e6322188089360bf346081Scott Su    private final Binder mCallTarget = new Binder() {
55991b14c4a26c89e02a7e6322188089360bf346081Scott Su        @Override
56091b14c4a26c89e02a7e6322188089360bf346081Scott Su        public boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
56191b14c4a26c89e02a7e6322188089360bf346081Scott Su            data.setDataPosition(0);
56291b14c4a26c89e02a7e6322188089360bf346081Scott Su            data.enforceInterface(LaunchpadActivity.LAUNCH);
56391b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (code == GOT_RECEIVE_TRANSACTION) {
56491b14c4a26c89e02a7e6322188089360bf346081Scott Su                final String name = data.readString();
56591b14c4a26c89e02a7e6322188089360bf346081Scott Su                gotReceive(name, null);
56691b14c4a26c89e02a7e6322188089360bf346081Scott Su                return true;
56791b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (code == ERROR_TRANSACTION) {
56891b14c4a26c89e02a7e6322188089360bf346081Scott Su                finishBad(data.readString());
56991b14c4a26c89e02a7e6322188089360bf346081Scott Su                return true;
57091b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
57191b14c4a26c89e02a7e6322188089360bf346081Scott Su            return false;
57291b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
57391b14c4a26c89e02a7e6322188089360bf346081Scott Su    };
57491b14c4a26c89e02a7e6322188089360bf346081Scott Su
57591b14c4a26c89e02a7e6322188089360bf346081Scott Su    private final void gotReceive(String name, Intent intent) {
57691b14c4a26c89e02a7e6322188089360bf346081Scott Su        synchronized (this) {
57791b14c4a26c89e02a7e6322188089360bf346081Scott Su
57891b14c4a26c89e02a7e6322188089360bf346081Scott Su            sCallingTest.addIntermediate(mNextReceiver + "-" + name);
57991b14c4a26c89e02a7e6322188089360bf346081Scott Su
58091b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (mExpectedData != null) {
58191b14c4a26c89e02a7e6322188089360bf346081Scott Su                final int n = mExpectedData.length;
58291b14c4a26c89e02a7e6322188089360bf346081Scott Su                int i;
58391b14c4a26c89e02a7e6322188089360bf346081Scott Su                boolean prev = false;
58491b14c4a26c89e02a7e6322188089360bf346081Scott Su                for (i = 0; i < n; i++) {
58591b14c4a26c89e02a7e6322188089360bf346081Scott Su                    if (mExpectedData[i].equals(intent.getStringExtra("test"))) {
58691b14c4a26c89e02a7e6322188089360bf346081Scott Su                        if (mReceivedData[i]) {
58791b14c4a26c89e02a7e6322188089360bf346081Scott Su                            prev = true;
58891b14c4a26c89e02a7e6322188089360bf346081Scott Su                            continue;
58991b14c4a26c89e02a7e6322188089360bf346081Scott Su                        }
59091b14c4a26c89e02a7e6322188089360bf346081Scott Su                        mReceivedData[i] = true;
59191b14c4a26c89e02a7e6322188089360bf346081Scott Su                        break;
59291b14c4a26c89e02a7e6322188089360bf346081Scott Su                    }
59391b14c4a26c89e02a7e6322188089360bf346081Scott Su                }
59491b14c4a26c89e02a7e6322188089360bf346081Scott Su                if (i >= n) {
59591b14c4a26c89e02a7e6322188089360bf346081Scott Su                    if (prev) {
59691b14c4a26c89e02a7e6322188089360bf346081Scott Su                        finishBad("Receive got data too many times: "
59791b14c4a26c89e02a7e6322188089360bf346081Scott Su                                + intent.getStringExtra("test"));
59891b14c4a26c89e02a7e6322188089360bf346081Scott Su                    } else {
59991b14c4a26c89e02a7e6322188089360bf346081Scott Su                        finishBad("Receive got unexpected data: " + intent.getStringExtra("test"));
60091b14c4a26c89e02a7e6322188089360bf346081Scott Su                    }
60191b14c4a26c89e02a7e6322188089360bf346081Scott Su                    return;
60291b14c4a26c89e02a7e6322188089360bf346081Scott Su                }
60391b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
60491b14c4a26c89e02a7e6322188089360bf346081Scott Su
60591b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (mNextReceiver >= mExpectedReceivers.length) {
60691b14c4a26c89e02a7e6322188089360bf346081Scott Su                finishBad("Got too many onReceiveIntent() calls!");
60791b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else if (!mExpectedReceivers[mNextReceiver].equals(name)) {
60891b14c4a26c89e02a7e6322188089360bf346081Scott Su                finishBad("Receive out of order: got " + name + " but expected "
60991b14c4a26c89e02a7e6322188089360bf346081Scott Su                        + mExpectedReceivers[mNextReceiver] + " at " + mNextReceiver);
61091b14c4a26c89e02a7e6322188089360bf346081Scott Su            } else {
61191b14c4a26c89e02a7e6322188089360bf346081Scott Su                mNextReceiver++;
61291b14c4a26c89e02a7e6322188089360bf346081Scott Su                if (mNextReceiver == mExpectedReceivers.length) {
61391b14c4a26c89e02a7e6322188089360bf346081Scott Su                    mHandler.post(mUnregister);
61491b14c4a26c89e02a7e6322188089360bf346081Scott Su                }
61591b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
61691b14c4a26c89e02a7e6322188089360bf346081Scott Su
61791b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
61891b14c4a26c89e02a7e6322188089360bf346081Scott Su    }
61991b14c4a26c89e02a7e6322188089360bf346081Scott Su
62091b14c4a26c89e02a7e6322188089360bf346081Scott Su    private final Runnable mUnregister = new Runnable() {
62191b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void run() {
62291b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (mReceiverRegistered) {
62391b14c4a26c89e02a7e6322188089360bf346081Scott Su                sCallingTest.addIntermediate("before-unregister");
62491b14c4a26c89e02a7e6322188089360bf346081Scott Su                unregisterMyReceiver();
62591b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
62691b14c4a26c89e02a7e6322188089360bf346081Scott Su            sCallingTest.finishTiming(true);
62791b14c4a26c89e02a7e6322188089360bf346081Scott Su            finishGood();
62891b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
62991b14c4a26c89e02a7e6322188089360bf346081Scott Su    };
63091b14c4a26c89e02a7e6322188089360bf346081Scott Su
63191b14c4a26c89e02a7e6322188089360bf346081Scott Su    private final Runnable mTimeout = new Runnable() {
63291b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void run() {
6331da4b3ce9249c5ec2713d1ad31913e1fcdae0d7eKeun young Park            Log.i(TAG, "timeout");
63491b14c4a26c89e02a7e6322188089360bf346081Scott Su            String msg = "Timeout";
63591b14c4a26c89e02a7e6322188089360bf346081Scott Su            if (mExpectedReceivers != null && mNextReceiver < mExpectedReceivers.length) {
63691b14c4a26c89e02a7e6322188089360bf346081Scott Su                msg = msg + " waiting for " + mExpectedReceivers[mNextReceiver];
63791b14c4a26c89e02a7e6322188089360bf346081Scott Su            }
63891b14c4a26c89e02a7e6322188089360bf346081Scott Su            finishBad(msg);
63991b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
64091b14c4a26c89e02a7e6322188089360bf346081Scott Su    };
64191b14c4a26c89e02a7e6322188089360bf346081Scott Su
64291b14c4a26c89e02a7e6322188089360bf346081Scott Su    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
64391b14c4a26c89e02a7e6322188089360bf346081Scott Su        @Override
64491b14c4a26c89e02a7e6322188089360bf346081Scott Su        public void onReceive(Context context, Intent intent) {
64591b14c4a26c89e02a7e6322188089360bf346081Scott Su            gotReceive(RECEIVER_REG, intent);
64691b14c4a26c89e02a7e6322188089360bf346081Scott Su        }
64791b14c4a26c89e02a7e6322188089360bf346081Scott Su    };
64891b14c4a26c89e02a7e6322188089360bf346081Scott Su}
649