1bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt/*
2bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * Copyright (C) 2006 The Android Open Source Project
3bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt *
4bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
5bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * you may not use this file except in compliance with the License.
6bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * You may obtain a copy of the License at
7bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt *
8bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
9bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt *
10bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
11bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
12bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * See the License for the specific language governing permissions and
14bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt * limitations under the License.
15bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt */
16bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
17bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltpackage com.android.internal.telephony;
18bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
19bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.content.Context;
2026c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport android.net.ConnectivityManager;
2126c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport android.net.NetworkCapabilities;
2226c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport android.net.NetworkRequest;
2326c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport android.net.StringNetworkSpecifier;
24bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.os.AsyncResult;
25bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.os.Binder;
26bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.os.Handler;
27bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.os.HandlerThread;
28bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.os.Looper;
29bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.os.Message;
3026c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport android.telephony.Rlog;
31bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.test.AndroidTestCase;
32bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport android.test.suitebuilder.annotation.SmallTest;
33bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
3426c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport com.android.internal.telephony.mocks.ConnectivityServiceMock;
3526c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport com.android.internal.telephony.mocks.SubscriptionControllerMock;
3626c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport com.android.internal.telephony.mocks.TelephonyRegistryMock;
3726c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colittiimport com.android.internal.telephony.test.SimulatedCommands;
38bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
39bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport java.util.concurrent.atomic.AtomicInteger;
40bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltimport java.util.concurrent.atomic.AtomicReference;
41bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
42bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwaltpublic class PhoneSwitcherTest extends AndroidTestCase {
43bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private final static String LOG_TAG = "PhoneSwitcherTest";
44bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
45bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    static void failAndStack(String str) {
46bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        fail(str + "\n" + SubscriptionMonitorTest.stack());
47bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
48bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
49bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    static String stack() {
50bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        StringBuilder sb = new StringBuilder();
51bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        for(StackTraceElement e : Thread.currentThread().getStackTrace()) {
52bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            sb.append(e.toString()).append("\n");
53bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
54bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        return sb.toString();
55bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
56bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
57bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private static class TestHandler extends Handler {
58bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public final static int ACTIVE_PHONE_SWITCH = 1;
59bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public final static int IN_IDLE = 2;
60bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
61bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        HandlerThread handlerThread;
62bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
63bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public TestHandler(Looper looper) {
64bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            super(looper);
65bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
66bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
67bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public void die() {
68bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            if(handlerThread != null) {
69bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                handlerThread.quit();
70bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                handlerThread = null;
71bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            }
72bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
73bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
74bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public void blockTilIdle() {
75bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            Object lock = new Object();
76bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            synchronized (lock) {
77bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                Message msg = this.obtainMessage(IN_IDLE, lock);
78bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                msg.sendToTarget();
79bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                try {
80bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    lock.wait();
81bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                } catch (InterruptedException e) {}
82bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            }
83bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
84bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
85bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public static TestHandler makeHandler() {
86bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            final HandlerThread handlerThread = new HandlerThread("TestHandler");
87bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            handlerThread.start();
88bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            final TestHandler result = new TestHandler(handlerThread.getLooper());
89bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            result.handlerThread = handlerThread;
90bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            return result;
91bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
92bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
93bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        private boolean objectEquals(Object o1, Object o2) {
94bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            if (o1 == null) return (o2 == null);
95bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            return o1.equals(o2);
96bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
97bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
98bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        private void failAndStack(String str) {
99bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            SubscriptionMonitorTest.failAndStack(str);
100bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
101bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
102bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        @Override
103bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public void handleMessage(Message msg) {
104bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            switch (msg.what) {
105bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                case ACTIVE_PHONE_SWITCH: {
106bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    AsyncResult ar = (AsyncResult)(msg.obj);
107bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    if (objectEquals(ar.userObj, mActivePhoneSwitchObject.get()) == false) {
108bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                        failAndStack("Active Phone Switch object is incorrect!");
109bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    }
110bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    int count = mActivePhoneSwitchCount.incrementAndGet();
111bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    Rlog.d(LOG_TAG, "ACTIVE_PHONE_SWITCH, inc to " + count);
112bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    break;
113bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                }
114bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                case IN_IDLE: {
115bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    Object lock = msg.obj;
116bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    synchronized (lock) {
117bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                        lock.notify();
118bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    }
119bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    break;
120bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                }
121bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            }
122bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
123bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
124bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        private final AtomicInteger mActivePhoneSwitchCount = new AtomicInteger(0);
125bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        private final AtomicReference<Object> mActivePhoneSwitchObject =
126bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                new AtomicReference<Object>();
127bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
128bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public void reset() {
129bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            mActivePhoneSwitchCount.set(0);
130bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            mActivePhoneSwitchObject.set(null);
131bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
132bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
133bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public void setActivePhoneSwitchObject(Object o) {
134bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            mActivePhoneSwitchObject.set(o);
135bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
136bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
137bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        public int getActivePhoneSwitchCount() {
138bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            return mActivePhoneSwitchCount.get();
139bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
140bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
141bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
142bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private void waitABit() {
143bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        try {
144bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            Thread.sleep(250);
145bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        } catch (Exception e) {}
146bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
147bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
148bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private String mTestName = "";
149bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
150bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private void log(String str) {
151bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        Rlog.d(LOG_TAG + " " + mTestName, str);
152bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
153bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
154bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private NetworkRequest makeSubSpecificDefaultRequest(ConnectivityServiceMock cs, int subId) {
155bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        NetworkCapabilities netCap = (new NetworkCapabilities()).
156bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).
157bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED).
158bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
15926c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colitti        netCap.setNetworkSpecifier(new StringNetworkSpecifier(Integer.toString(subId)));
160bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        return cs.requestNetwork(netCap, null, 0, new Binder(), -1);
161bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
162bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
163bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private NetworkRequest makeSubSpecificMmsRequest(ConnectivityServiceMock cs, int subId) {
164bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        NetworkCapabilities netCap = (new NetworkCapabilities()).
165bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                addCapability(NetworkCapabilities.NET_CAPABILITY_MMS).
166bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED).
167bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
16826c3198e70345a70e5dfa8ee422c7c4e60916cbeLorenzo Colitti        netCap.setNetworkSpecifier(new StringNetworkSpecifier(Integer.toString(subId)));
169bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        return cs.requestNetwork(netCap, null, 0, new Binder(), -1);
170bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
171bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
172bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    private Context makeContext() {
173bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ContextFixture contextFixture = new ContextFixture();
174bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        String[] networkConfigString = getContext().getResources().getStringArray(
175bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                com.android.internal.R.array.networkAttributes);
176bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        contextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
177bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                networkConfigString);
178bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        return contextFixture.getTestDouble();
179bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
180bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
181bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    /**
182bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * Test that a single phone case results in our phone being active and the RIL called
183bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     */
184bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    @SmallTest
185bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    public void testRegister() throws Exception {
186bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        mTestName = "testRegister";
187bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final int numPhones = 2;
188bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final int maxActivePhones = 1;
189bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final HandlerThread handlerThread = new HandlerThread("PhoneSwitcherTestThread");
190bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        handlerThread.start();
191bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ContextFixture contextFixture = new ContextFixture();
192bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        String[] networkConfigString = getContext().getResources().getStringArray(
193bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                com.android.internal.R.array.networkAttributes);
194bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        contextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
195bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                networkConfigString);
196bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final Context contextMock = contextFixture.getTestDouble();
197bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ConnectivityServiceMock connectivityServiceMock =
198bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                new ConnectivityServiceMock(contextMock);
199bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ConnectivityManager cm =
200bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                new ConnectivityManager(contextMock, connectivityServiceMock);
201bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        contextFixture.setSystemService(Context.CONNECTIVITY_SERVICE, cm);
202bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ITelephonyRegistry.Stub telRegistryMock = new TelephonyRegistryMock();
203bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final SubscriptionControllerMock subControllerMock =
204bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                new SubscriptionControllerMock(contextMock, telRegistryMock, numPhones);
205bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final SimulatedCommands[] commandsInterfaces = new SimulatedCommands[numPhones];
206bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final PhoneMock[] phones = new PhoneMock[numPhones];
207bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        for (int i = 0; i < numPhones; i++) {
208bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            commandsInterfaces[i] = new SimulatedCommands();
209bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        //    phones[i] = new PhoneMock(contextMock, commandsInterfaces[i]);
210bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
211bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
212bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        PhoneSwitcher phoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones,
213bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                contextMock, subControllerMock, handlerThread.getLooper(), telRegistryMock,
214bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                commandsInterfaces, phones);
215bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
216bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // verify nothing has been done while there are no inputs
217bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed initially");
218bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (phoneSwitcher.isPhoneActive(0))        fail("phone active initially");
219bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
220bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        connectivityServiceMock.addDefaultRequest();
221bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
222bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
223bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed after request");
224bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (phoneSwitcher.isPhoneActive(0))        fail("phone active after request");
225bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
226bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        TestHandler testHandler = TestHandler.makeHandler();
227bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        Object activePhoneSwitchObject = new Object();
228bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.setActivePhoneSwitchObject(activePhoneSwitchObject);
229bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
230bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.blockTilIdle();
231bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
232bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // not registered yet - shouldn't inc
233bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 0) {
234bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("pretest of ActivePhoneSwitchCount");
235bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
236bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        boolean threw = false;
237bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        try {
238bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            // should throw
239bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            phoneSwitcher.registerForActivePhoneSwitch(2, testHandler,
240bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    TestHandler.ACTIVE_PHONE_SWITCH, activePhoneSwitchObject);
241bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        } catch (IllegalArgumentException e) {
242bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            threw = true;
243bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
244bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (threw == false) fail("register with bad phoneId didn't throw");
245bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
246bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        phoneSwitcher.registerForActivePhoneSwitch(0, testHandler,
247bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                TestHandler.ACTIVE_PHONE_SWITCH,
248bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                activePhoneSwitchObject);
249bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.blockTilIdle();
250bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
251bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 1) {
252bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("post register of ActivePhoneSwitchCount not 1!");
253bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
254bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
255bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setDefaultDataSubId(0);
256bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.blockTilIdle();
257bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
258bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 1) {
259bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after set of default to 0, ActivePhoneSwitchCount not 1!");
260bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
261bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
262bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
263bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(0, 0);
264bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
265bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
266bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 2) {
267bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after mapping of 0 to 0, ActivePhoneSwitchCount not 2!");
268bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
269bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
270bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
271bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // now try various things that should cause the active phone to switch:
272bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 1 lose default via default sub change
273bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 2 gain default via default sub change
274bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 3 lose default via sub->phone change
275bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 4 gain default via sub->phone change
276bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 5 lose default network request
277bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 6 gain subscription-specific request
278bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 7 lose via sub->phone change
279bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 8 gain via sub->phone change
280bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 9 lose subscription-specific request
281bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 10 don't switch phones when in emergency mode
282bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
283bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 1 lose default via default sub change
284bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setDefaultDataSubId(1);
285bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
286bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 3) {
287bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after set of default to 1, ActivePhoneSwitchCount not 3!");
288bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
289bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
290bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
291bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(1, 1);
292bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
293bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 3) {
294bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after mapping of 1 to 1, ActivePhoneSwitchCount not 3!");
295bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
296bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
297bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed() == false) fail("data not allowed");
298bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
299bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 2 gain default via default sub change
300bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setDefaultDataSubId(0);
301bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
302bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 4) {
303bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after set of default to 0, ActivePhoneSwitchCount not 4!");
304bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
305bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
306bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
307bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
308bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 3 lose default via sub->phone change
309bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(0, 2);
310bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
311bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
312bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 5) {
313bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after mapping of 0 to 2, ActivePhoneSwitchCount not 5!");
314bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
315bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
316bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
317bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
318bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 4 gain default via sub->phone change
319bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(0, 0);
320bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
321bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 6) {
322bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after mapping of 0 to 0, ActivePhoneSwitchCount not 6!");
323bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
324bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
325bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
326bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
327bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 5 lose default network request
328bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        connectivityServiceMock.removeDefaultRequest();
329bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
330bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 7) {
331bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after loss of network request, ActivePhoneSwitchCount not 7!");
332bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
333bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
334bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
335bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
336bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 6 gain subscription-specific request
337bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        NetworkRequest request = makeSubSpecificDefaultRequest(connectivityServiceMock, 0);
338bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
339bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 8) {
340bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after gain of network request, ActivePhoneSwitchCount not 8!");
341bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
342bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
343bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
344bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
345bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 7 lose via sub->phone change
346bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(0, 1);
347bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
348bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 9) {
349bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after loss of request due to subId map change, ActivePhoneSwitchCount not 9!");
350bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
351bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
352bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
353bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
354bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 8 gain via sub->phone change
355bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(0, 0);
356bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
357bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 10) {
358bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after gain of request due to subId map change, ActivePhoneSwitchCount not 10!");
359bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
360bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
361bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
362bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
363bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 9 lose subscription-specific request
364bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        connectivityServiceMock.releaseNetworkRequest(request);
365bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
366bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 11) {
367bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after release of request, ActivePhoneSwitchCount not 11!");
368bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
369bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
370bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
371bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
372bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // 10 don't switch phones when in emergency mode
373bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // not ready yet - Phone turns out to be hard to stub out
374bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        phones[0].setInEmergencyCall(true);
375bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        connectivityServiceMock.addDefaultRequest();
376bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        waitABit();
377bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        if (testHandler.getActivePhoneSwitchCount() != 11) {
378bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//            fail("after release of request, ActivePhoneSwitchCount not 11!");
379bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        }
380bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
381bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
382bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//
383bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        phones[0].setInEmergencyCall(false);
384bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        connectivityServiceMock.addDefaultRequest();
385bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        waitABit();
386bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        if (testHandler.getActivePhoneSwitchCount() != 12) {
387bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//            fail("after release of request, ActivePhoneSwitchCount not 11!");
388bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        }
389bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
390bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt//        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
391ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu
392ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        for (int i = 0; i < numPhones; i++) {
393ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu            commandsInterfaces[i].dispose();
394ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        }
395ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu
396ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        connectivityServiceMock.die();
397bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.die();
398bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        handlerThread.quit();
399bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
400bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
401bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    /**
402bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * Test a multi-sim case with limited active phones:
403bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - lose default via default sub change
404bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - lose default via sub->phone change
405bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - gain default via sub->phone change
406bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - gain default via default sub change
407bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - lose default network request
408bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - gain subscription-specific request
409bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - lose via sub->phone change
410bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - gain via sub->phone change
411bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - lose subscription-specific request
412bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - tear down low priority phone when new request comes in
413bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - tear down low priority phone when sub change causes split
414bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - bring up low priority phone when sub change causes join
415bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - don't switch phones when in emergency mode
416bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     */
417bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    @SmallTest
418bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    public void testPrioritization() throws Exception {
419bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        mTestName = "testPrioritization";
420bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final int numPhones = 2;
421bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final int maxActivePhones = 1;
422bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final HandlerThread handlerThread = new HandlerThread("PhoneSwitcherTestThread");
423bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        handlerThread.start();
424bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ContextFixture contextFixture = new ContextFixture();
425bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        String[] networkConfigString = getContext().getResources().getStringArray(
426bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                com.android.internal.R.array.networkAttributes);
427bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        contextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
428bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                networkConfigString);
429bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final Context contextMock = contextFixture.getTestDouble();
430bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ConnectivityServiceMock connectivityServiceMock =
431bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            new ConnectivityServiceMock(contextMock);
432bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ConnectivityManager cm =
433bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                new ConnectivityManager(contextMock, connectivityServiceMock);
434bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        contextFixture.setSystemService(Context.CONNECTIVITY_SERVICE, cm);
435bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final ITelephonyRegistry.Stub telRegistryMock = new TelephonyRegistryMock();
436bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final SubscriptionControllerMock subControllerMock =
437bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                new SubscriptionControllerMock(contextMock, telRegistryMock, numPhones);
438bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final SimulatedCommands[] commandsInterfaces = new SimulatedCommands[numPhones];
439bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        final PhoneMock[] phones = new PhoneMock[numPhones];
440bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        for (int i = 0; i < numPhones; i++) {
441bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            commandsInterfaces[i] = new SimulatedCommands();
442bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
443bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
444bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        PhoneSwitcher phoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones,
445bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                contextMock, subControllerMock, handlerThread.getLooper(), telRegistryMock,
446bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                commandsInterfaces, phones);
447bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
448bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        TestHandler testHandler = TestHandler.makeHandler();
449bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        Object activePhoneSwitchObject = new Object();
450bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.setActivePhoneSwitchObject(activePhoneSwitchObject);
451bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
452bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        connectivityServiceMock.addDefaultRequest();
453bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(0, 0);
454bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setSlotSubId(1, 1);
455bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        subControllerMock.setDefaultDataSubId(0);
456bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
457bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        phoneSwitcher.registerForActivePhoneSwitch(0, testHandler, TestHandler.ACTIVE_PHONE_SWITCH,
458bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                activePhoneSwitchObject);
459bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
460bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // verify initial conditions
461bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 1) {
462bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("Initial conditions not met: ActivePhoneSwitchCount not 1! " +
463bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt                    testHandler.getActivePhoneSwitchCount());
464bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
465bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
466bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
467bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
468bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        // now start a higher priority conneciton on the other sub
469bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        NetworkRequest request = makeSubSpecificMmsRequest(connectivityServiceMock, 1);
470bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        waitABit();
471bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (testHandler.getActivePhoneSwitchCount() != 2) {
472bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt            fail("after gain of network request, ActivePhoneSwitchCount not 2!");
473bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        }
474bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
475bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        if (commandsInterfaces[1].isDataAllowed() == false) fail("data not allowed");
476bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
477ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        for (int i = 0; i < numPhones; i++) {
478ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu            commandsInterfaces[i].dispose();
479ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        }
480ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu
481ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        connectivityServiceMock.die();
4823baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        testHandler.die();
4833baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        handlerThread.quit();
4843baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt    }
485bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
4863baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt    /**
4873baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt     * Verify we don't send spurious DATA_ALLOWED calls when another NetworkFactory
4883baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt     * wins (ie, switch to wifi).
4893baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt     */
4903baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt    @SmallTest
4913baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt    public void testHigherPriorityDefault() throws Exception {
4923baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        mTestName = "testPrioritization";
4933baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final int numPhones = 2;
4943baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final int maxActivePhones = 1;
4953baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final HandlerThread handlerThread = new HandlerThread("PhoneSwitcherTestThread");
4963baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        handlerThread.start();
4973baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final ContextFixture contextFixture = new ContextFixture();
4983baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        String[] networkConfigString = getContext().getResources().getStringArray(
4993baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                com.android.internal.R.array.networkAttributes);
5003baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        contextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
5013baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                networkConfigString);
5023baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final Context contextMock = contextFixture.getTestDouble();
5033baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final ConnectivityServiceMock connectivityServiceMock =
5043baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                new ConnectivityServiceMock(contextMock);
5053baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final ConnectivityManager cm =
5063baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                new ConnectivityManager(contextMock, connectivityServiceMock);
5073baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        contextFixture.setSystemService(Context.CONNECTIVITY_SERVICE, cm);
5083baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final ITelephonyRegistry.Stub telRegistryMock = new TelephonyRegistryMock();
5093baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final SubscriptionControllerMock subControllerMock =
5103baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                new SubscriptionControllerMock(contextMock, telRegistryMock, numPhones);
5113baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final SimulatedCommands[] commandsInterfaces = new SimulatedCommands[numPhones];
5123baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        final PhoneMock[] phones = new PhoneMock[numPhones];
5133baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        for (int i = 0; i < numPhones; i++) {
5143baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt            commandsInterfaces[i] = new SimulatedCommands();
5153baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        }
5163baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt
5173baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        PhoneSwitcher phoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones,
5183baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                contextMock, subControllerMock, handlerThread.getLooper(), telRegistryMock,
5193baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                commandsInterfaces, phones);
5203baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt
5213baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        TestHandler testHandler = TestHandler.makeHandler();
5223baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        Object activePhoneSwitchObject = new Object();
5233baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        testHandler.setActivePhoneSwitchObject(activePhoneSwitchObject);
5243baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt
5253baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        connectivityServiceMock.addDefaultRequest();
5263baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        subControllerMock.setSlotSubId(0, 0);
5273baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        subControllerMock.setSlotSubId(1, 1);
5283baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        subControllerMock.setDefaultDataSubId(0);
5293baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        waitABit();
5303baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt
5313baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        // Phone 0 should be active
5323baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
5333baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
5343baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt
5353baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        connectivityServiceMock.setCurrentScoreForRequest(connectivityServiceMock.defaultRequest,
5363baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt                100);
5373baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        waitABit();
5383baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt
5393baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        // should be no change
5403baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
5413baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
542bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
5433baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        connectivityServiceMock.setCurrentScoreForRequest(connectivityServiceMock.defaultRequest,
5446ffa56f54d0318c10526b8a64f85ed0841de5380Robert Greenwalt                0);
5453baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        waitABit();
546bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
5473baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        // should be no change
5483baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
5493baedf2ead743a2c67dcac03733cec52bcef5636Robert Greenwalt        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
550bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
551ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        for (int i = 0; i < numPhones; i++) {
552ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu            commandsInterfaces[i].dispose();
553ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        }
554ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu
555ae6e8f99bdb297f906d6dcdc0a385ba27eee47fdJack Yu        connectivityServiceMock.die();
556bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        testHandler.die();
557bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt        handlerThread.quit();
558bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    }
559bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
560bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt    /**
561bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * Test MSMA testing prioritiziation
562bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - leave multiple on (up to the limit)
563bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - tear down lowest priority phone when new request comes in
564bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - tear down low priority phone when sub change causes split
565bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - bring up low priority phone when sub change causes join
566bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     * - don't switch phones when in emergency mode
567bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt     */
568bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt
569bc2eba9036e430ea40a47ab708c0386fa567ec9cRobert Greenwalt}
570