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