1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony;
18
19import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
20
21import com.android.internal.telephony.MccTable;
22import com.android.internal.telephony.mocks.SubscriptionControllerMock;
23import com.android.internal.telephony.mocks.TelephonyRegistryMock;
24
25import android.content.Context;
26import android.os.AsyncResult;
27import android.os.Handler;
28import android.os.HandlerThread;
29import android.os.Looper;
30import android.os.Message;
31
32import android.test.AndroidTestCase;
33import android.test.suitebuilder.annotation.SmallTest;
34
35import android.telephony.Rlog;
36
37import java.util.concurrent.atomic.AtomicInteger;
38import java.util.concurrent.atomic.AtomicReference;
39
40public class SubscriptionMonitorTest extends AndroidTestCase {
41    private final static String LOG_TAG = "SubscriptionMonitorTest";
42
43    static void failAndStack(String str) {
44        fail(str + "\n" + SubscriptionMonitorTest.stack());
45    }
46
47    static String stack() {
48        StringBuilder sb = new StringBuilder();
49        for(StackTraceElement e : Thread.currentThread().getStackTrace()) {
50            sb.append(e.toString()).append("\n");
51        }
52        return sb.toString();
53    }
54
55    private static class TestHandler extends Handler {
56        public final static int SUBSCRIPTION_CHANGED = 1;
57        public final static int DEFAULT_SUBSCRIPTION_CHANGED = 2;
58        public final static int IN_IDLE = 3;
59
60        HandlerThread handlerThread;
61
62        public TestHandler(Looper looper) {
63            super(looper);
64        }
65
66        public void die() {
67            if(handlerThread != null) {
68                handlerThread.quit();
69                handlerThread = null;
70            }
71        }
72
73        public void blockTilIdle() {
74            Object lock = new Object();
75            synchronized (lock) {
76                Message msg = this.obtainMessage(IN_IDLE, lock);
77                msg.sendToTarget();
78                try {
79                    lock.wait();
80                } catch (InterruptedException e) {}
81            }
82        }
83
84        public static TestHandler makeHandler() {
85            final HandlerThread handlerThread = new HandlerThread("TestHandler");
86            handlerThread.start();
87            final TestHandler result = new TestHandler(handlerThread.getLooper());
88            result.handlerThread = handlerThread;
89            return result;
90        }
91
92        private boolean objectEquals(Object o1, Object o2) {
93            if (o1 == null) return (o2 == null);
94            return o1.equals(o2);
95        }
96
97        private void failAndStack(String str) {
98            SubscriptionMonitorTest.failAndStack(str);
99        }
100
101        @Override
102        public void handleMessage(Message msg) {
103            switch (msg.what) {
104                case SUBSCRIPTION_CHANGED: {
105                    AsyncResult ar = (AsyncResult)(msg.obj);
106                    if (objectEquals(ar.userObj, mSubscriptionChangedObject.get()) == false) {
107                        failAndStack("Subscription Changed object is incorrect!");
108                    }
109                    mSubscriptionChangedCount.incrementAndGet();
110                    Rlog.d(LOG_TAG, "SUBSCRIPTION_CHANGED, inc to " +
111                            mSubscriptionChangedCount.get());
112                    break;
113                }
114                case DEFAULT_SUBSCRIPTION_CHANGED: {
115                    AsyncResult ar = (AsyncResult)(msg.obj);
116                    if (objectEquals(ar.userObj,
117                            mDefaultSubscriptionChangedObject.get()) == false) {
118                        failAndStack("Default Subscription Changed object is incorrect!");
119                    }
120                    mDefaultSubscriptionChangedCount.incrementAndGet();
121                    Rlog.d(LOG_TAG, "DEFAULT_SUBSCRIPTION_CHANGED, inc to " +
122                            mDefaultSubscriptionChangedCount.get());
123                    break;
124                }
125                case IN_IDLE: {
126                    Object lock = msg.obj;
127                    synchronized (lock) {
128                        lock.notify();
129                    }
130                    break;
131                }
132            }
133        }
134
135        private final AtomicInteger mSubscriptionChangedCount = new AtomicInteger(0);
136        private final AtomicReference<Object> mSubscriptionChangedObject =
137                new AtomicReference<Object>();
138
139        private final AtomicInteger mDefaultSubscriptionChangedCount = new AtomicInteger(0);
140        private final AtomicReference<Object> mDefaultSubscriptionChangedObject =
141                new AtomicReference<Object>();
142
143        public void reset() {
144            mSubscriptionChangedCount.set(0);
145            mSubscriptionChangedObject.set(null);
146
147            mDefaultSubscriptionChangedCount.set(0);
148            mDefaultSubscriptionChangedObject.set(null);
149        }
150
151        public void setSubscriptionChangedObject(Object o) {
152            mSubscriptionChangedObject.set(o);
153        }
154        public void setDefaultSubscriptionChangedObject(Object o) {
155            mDefaultSubscriptionChangedObject.set(o);
156        }
157
158        public int getSubscriptionChangedCount() {
159            return mSubscriptionChangedCount.get();
160        }
161        public int getDefaultSubscriptionChangedCount() {
162            return mDefaultSubscriptionChangedCount.get();
163        }
164    }
165
166    /**
167     * Register and unregister normally.
168     * Verify register worked by causing an event.
169     * Verify unregister by causing another event.
170     */
171    @SmallTest
172    public void testRegister() throws Exception {
173        final int numPhones = 2;
174        final ContextFixture contextFixture = new ContextFixture();
175        final Context context = contextFixture.getTestDouble();
176        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
177        SubscriptionControllerMock subController =
178                new SubscriptionControllerMock(context, telRegistry, numPhones);
179
180        SubscriptionMonitor testedSubMonitor =
181                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
182
183        TestHandler testHandler = TestHandler.makeHandler();
184        Object subChangedObject = new Object();
185        testHandler.setSubscriptionChangedObject(subChangedObject);
186
187        Object defaultSubChangedObject = new Object();
188        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
189
190        // try events before registering
191        subController.setDefaultDataSubId(0);
192        subController.setSlotSubId(0, 0);
193
194        if (testHandler.getSubscriptionChangedCount() != 0) {
195            fail("pretest of SubscriptionChangedCount");
196        }
197        if (testHandler.getDefaultSubscriptionChangedCount() != 0) {
198            fail("pretest of DefaultSubscriptionChangedCount");
199        }
200
201        testedSubMonitor.registerForSubscriptionChanged(0, testHandler,
202                  TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
203        testHandler.blockTilIdle();
204
205        if (testHandler.getSubscriptionChangedCount() != 1) {
206            fail("test1 of SubscriptionChangedCount");
207        }
208        if (testHandler.getDefaultSubscriptionChangedCount() != 0) {
209            fail("test1 of DefaultSubscriptionChangedCount");
210        }
211
212        testedSubMonitor.registerForDefaultDataSubscriptionChanged(0, testHandler,
213                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
214        testHandler.blockTilIdle();
215
216        if (testHandler.getSubscriptionChangedCount() != 1) {
217            fail("test2 of SubscriptionChangedCount");
218        }
219        if (testHandler.getDefaultSubscriptionChangedCount() != 1) {
220            fail("test2 of DefaultSubscriptionChangedCount");
221        }
222
223        subController.setDefaultDataSubId(1);
224        testHandler.blockTilIdle();
225
226        if (testHandler.getSubscriptionChangedCount() != 1) {
227            fail("test3 of SubscriptionChangedCount, " +
228                    testHandler.getSubscriptionChangedCount() + " vs 1");
229        }
230        if (testHandler.getDefaultSubscriptionChangedCount() != 2) {
231            fail("test3 of DefaultSubscriptionChangedCount, " +
232                    testHandler.getDefaultSubscriptionChangedCount() + " vs 2");
233        }
234
235        subController.setSlotSubId(0, 1);
236        testHandler.blockTilIdle();
237
238        if (testHandler.getSubscriptionChangedCount() != 2) {
239            fail("test4 of SubscriptionChangedCount");
240        }
241        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
242            fail("test4 of DefaultSubscriptionChangedCount");
243        }
244
245        testedSubMonitor.unregisterForDefaultDataSubscriptionChanged(0, testHandler);
246        subController.setSlotSubId(0, 0);
247        testHandler.blockTilIdle();
248
249        if (testHandler.getSubscriptionChangedCount() != 3) {
250            fail("test5 of SubscriptionChangedCount, " +
251                    testHandler.getSubscriptionChangedCount() + " vs 3");
252        }
253        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
254            fail("test5 of DefaultSubscriptionChangedCount, " +
255                    testHandler.getDefaultSubscriptionChangedCount() + " vs 3");
256        }
257
258        testedSubMonitor.unregisterForSubscriptionChanged(0, testHandler);
259
260        subController.setSlotSubId(0, 1);
261        subController.setDefaultDataSubId(0);
262        testHandler.blockTilIdle();
263
264        if (testHandler.getSubscriptionChangedCount() != 3) {
265            fail("test6 of SubscriptionChangedCount, " +
266                    testHandler.getSubscriptionChangedCount() + " vs 3");
267        }
268        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
269            fail("test6 of DefaultSubscriptionChangedCount, " +
270                    testHandler.getDefaultSubscriptionChangedCount() + " vs 3");
271        }
272
273        testHandler.die();
274    }
275
276    /**
277     * Bad register/unregisters
278     *
279     * Try phoneId that doesn't exist.
280     * Cause an event and verify don't get notified.
281     * Try to unregister multiple times.
282     */
283    @SmallTest
284    public void testBadRegister() throws Exception {
285        final int numPhones = 2;
286        final ContextFixture contextFixture = new ContextFixture();
287        final Context context = contextFixture.getTestDouble();
288        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
289        SubscriptionControllerMock subController =
290                new SubscriptionControllerMock(context, telRegistry, numPhones);
291
292        SubscriptionMonitor testedSubMonitor =
293                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
294
295        TestHandler testHandler = TestHandler.makeHandler();
296        Object subChangedObject = new Object();
297        testHandler.setSubscriptionChangedObject(subChangedObject);
298
299        Object defaultSubChangedObject = new Object();
300        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
301
302        try {
303            testedSubMonitor.registerForSubscriptionChanged(-1, testHandler,
304                      TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
305            fail("IllegalArgumentException expected with bad phoneId");
306        } catch (IllegalArgumentException e) {}
307        try {
308            testedSubMonitor.registerForDefaultDataSubscriptionChanged(-1, testHandler,
309                    TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
310            fail("IllegalArgumentException expected with bad phoneId");
311        } catch (IllegalArgumentException e) {}
312        try {
313            testedSubMonitor.registerForSubscriptionChanged(numPhones, testHandler,
314                      TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
315            fail("IllegalArgumentException expected with bad phoneId");
316        } catch (IllegalArgumentException e) {}
317        try {
318            testedSubMonitor.registerForDefaultDataSubscriptionChanged(numPhones, testHandler,
319                    TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
320            fail("IllegalArgumentException expected with bad phoneId");
321        } catch (IllegalArgumentException e) {}
322
323        subController.setDefaultDataSubId(0);
324        subController.setSlotSubId(0, 0);
325
326        if (testHandler.getSubscriptionChangedCount() != 0) {
327            fail("getSubscriptionChangedCount reported non-zero!");
328        }
329        if (testHandler.getDefaultSubscriptionChangedCount() != 0) {
330            fail("getDefaultSubscriptionChangedCount reported non-zero!");
331        }
332
333        testHandler.die();
334    }
335
336    /**
337     * Try to force spurious notifications - register/unregister in tight loop with
338     * events happening in the unregistered gap.
339     */
340    @SmallTest
341    public void testSpuriousNotifications() throws Exception {
342        final int numPhones = 2;
343        final ContextFixture contextFixture = new ContextFixture();
344        final Context context = contextFixture.getTestDouble();
345        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
346        SubscriptionControllerMock subController =
347                new SubscriptionControllerMock(context, telRegistry, numPhones);
348
349        SubscriptionMonitor testedSubMonitor =
350                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
351
352        TestHandler testHandler = TestHandler.makeHandler();
353        Object subChangedObject = new Object();
354        testHandler.setSubscriptionChangedObject(subChangedObject);
355
356        Object defaultSubChangedObject = new Object();
357        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
358
359        final int PHONE_ID = 0;
360        final int FIRST_SUB_ID = 0;
361        final int SECOND_SUB_ID = 1;
362
363        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
364                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
365        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
366                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
367        final int LOOP_COUNT = 1000;
368        for (int i = 0; i < LOOP_COUNT; i++) {
369            testedSubMonitor.unregisterForSubscriptionChanged(PHONE_ID, testHandler);
370            testedSubMonitor.unregisterForDefaultDataSubscriptionChanged(PHONE_ID, testHandler);
371
372            subController.setDefaultDataSubId(FIRST_SUB_ID);
373            subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
374
375            subController.setDefaultDataSubId(SECOND_SUB_ID);
376            subController.setSlotSubId(PHONE_ID, SECOND_SUB_ID);
377
378            testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
379                    TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
380            testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
381                    TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
382        }
383        testHandler.blockTilIdle();
384
385        // should get one for every registration
386        if (testHandler.getSubscriptionChangedCount() != 1 + LOOP_COUNT) {
387            fail("getSubscriptionChangedCount reported " +
388                    testHandler.getSubscriptionChangedCount() + " != " + (1 + LOOP_COUNT));
389        }
390        if (testHandler.getDefaultSubscriptionChangedCount() != 1 + LOOP_COUNT) {
391            fail("getDefaultSubscriptionChangedCount reported " +
392                    testHandler.getDefaultSubscriptionChangedCount() + " != " + (1 + LOOP_COUNT));
393        }
394
395        testHandler.die();
396    }
397
398    /**
399     * Test duplicate registrations - both should survive
400     * Also test duplicate unreg - shouldn't crash..
401     */
402    @SmallTest
403    public void testMultiRegUnregistration() throws Exception {
404        final int numPhones = 2;
405        final ContextFixture contextFixture = new ContextFixture();
406        final Context context = contextFixture.getTestDouble();
407        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
408        SubscriptionControllerMock subController =
409                new SubscriptionControllerMock(context, telRegistry, numPhones);
410
411        SubscriptionMonitor testedSubMonitor =
412                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
413
414        TestHandler testHandler = TestHandler.makeHandler();
415        Object subChangedObject = new Object();
416        testHandler.setSubscriptionChangedObject(subChangedObject);
417
418        Object defaultSubChangedObject = new Object();
419        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
420
421        final int PHONE_ID = 0;
422        final int FIRST_SUB_ID = 0;
423        final int SECOND_SUB_ID = 1;
424
425        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
426                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
427        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
428                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
429
430        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
431                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
432        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
433                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
434
435        subController.setDefaultDataSubId(FIRST_SUB_ID);
436        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
437
438        subController.setDefaultDataSubId(SECOND_SUB_ID);
439        subController.setSlotSubId(PHONE_ID, SECOND_SUB_ID);
440
441        testHandler.blockTilIdle();
442
443        // should get 1 for each registration and 4 for the two events
444        if (testHandler.getSubscriptionChangedCount() != 6) {
445            fail("getSubscriptionChangedCount reported " +
446                    testHandler.getSubscriptionChangedCount() + " != 6");
447        }
448        // 2 for the 2 registrations, 2 for the single event in the first cluster (2 listeners)
449        // 2 for the setDefatulDataSub in the second cluster (lost data sub)
450        // 2 for the setSlotSubId (regain default)
451        if (testHandler.getDefaultSubscriptionChangedCount() != 8) {
452            fail("getDefaultSubscriptionChangedCount reported " +
453                    testHandler.getDefaultSubscriptionChangedCount() + " != 8");
454        }
455
456        testedSubMonitor.unregisterForSubscriptionChanged(PHONE_ID, testHandler);
457        testedSubMonitor.unregisterForDefaultDataSubscriptionChanged(PHONE_ID, testHandler);
458        testedSubMonitor.unregisterForSubscriptionChanged(PHONE_ID, testHandler);
459        testedSubMonitor.unregisterForDefaultDataSubscriptionChanged(PHONE_ID, testHandler);
460
461        testHandler.die();
462    }
463
464    /**
465     * Try event flood while registered - verify receive all.
466     */
467    @SmallTest
468    public void testEventFloodNotifications() throws Exception {
469        final int numPhones = 2;
470        final ContextFixture contextFixture = new ContextFixture();
471        final Context context = contextFixture.getTestDouble();
472        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
473        SubscriptionControllerMock subController =
474                new SubscriptionControllerMock(context, telRegistry, numPhones);
475
476        SubscriptionMonitor testedSubMonitor =
477                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
478
479        TestHandler testHandler = TestHandler.makeHandler();
480        Object subChangedObject = new Object();
481        testHandler.setSubscriptionChangedObject(subChangedObject);
482
483        Object defaultSubChangedObject = new Object();
484        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
485
486        final int PHONE_ID = 0;
487        final int FIRST_SUB_ID = 0;
488        final int SECOND_SUB_ID = 1;
489
490        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
491                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
492        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
493                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
494
495        final int LOOP_COUNT = 1;
496        for (int i = 0; i < LOOP_COUNT; i++) {
497            subController.setDefaultDataSubId(FIRST_SUB_ID);
498            subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
499
500            subController.setDefaultDataSubId(SECOND_SUB_ID);
501            subController.setSlotSubId(PHONE_ID, SECOND_SUB_ID);
502        }
503        testHandler.blockTilIdle();
504
505        // should get one for registration + 2 per loop
506        if (testHandler.getSubscriptionChangedCount() != 1 + (2 * LOOP_COUNT)) {
507            fail("getSubscriptionChangedCount reported " +
508                    testHandler.getSubscriptionChangedCount() + " != " + (1 + (2 * LOOP_COUNT)));
509        }
510        // should get one for registration + 3 for first loop + 4 for subsequent loops
511        if (testHandler.getDefaultSubscriptionChangedCount() != (4 * LOOP_COUNT)) {
512            fail("getDefaultSubscriptionChangedCount reported " +
513                    testHandler.getDefaultSubscriptionChangedCount() + " != " +
514                    (4 * LOOP_COUNT));
515        }
516
517        testHandler.die();
518    }
519
520    /**
521     * Try tests with no default set
522     */
523    @SmallTest
524    public void testNoDefaultNotifications() throws Exception {
525        final int numPhones = 2;
526        final ContextFixture contextFixture = new ContextFixture();
527        final Context context = contextFixture.getTestDouble();
528        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
529        SubscriptionControllerMock subController =
530                new SubscriptionControllerMock(context, telRegistry, numPhones);
531
532        SubscriptionMonitor testedSubMonitor =
533                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
534
535        TestHandler testHandler = TestHandler.makeHandler();
536        Object subChangedObject = new Object();
537        testHandler.setSubscriptionChangedObject(subChangedObject);
538
539        Object defaultSubChangedObject = new Object();
540        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
541
542        final int PHONE_ID = 0;
543        final int FIRST_SUB_ID = 0;
544        final int SECOND_SUB_ID = 1;
545
546        subController.setDefaultDataSubId(INVALID_SUBSCRIPTION_ID);
547        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
548
549        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
550                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
551        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
552                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
553
554
555        subController.setSlotSubId(PHONE_ID, SECOND_SUB_ID);
556        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
557
558        testHandler.blockTilIdle();
559
560        if (testHandler.getSubscriptionChangedCount() != 3) {
561            fail("getSubscriptionChangedCount reported " +
562                    testHandler.getSubscriptionChangedCount() + " != 3");
563        }
564        if (testHandler.getDefaultSubscriptionChangedCount() != 1) {
565            fail("getDefaultSubscriptionChangedCount reported " +
566                    testHandler.getDefaultSubscriptionChangedCount() + " != 1");
567        }
568
569        testHandler.die();
570    }
571
572    @SmallTest
573    public void testNoSubChange() throws Exception {
574        String TAG = "testNoSubChange";
575        final int numPhones = 2;
576        final ContextFixture contextFixture = new ContextFixture();
577        final Context context = contextFixture.getTestDouble();
578        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
579        SubscriptionControllerMock subController =
580                new SubscriptionControllerMock(context, telRegistry, numPhones);
581
582        SubscriptionMonitor testedSubMonitor =
583                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
584
585        TestHandler testHandler = TestHandler.makeHandler();
586        Object subChangedObject = new Object();
587        testHandler.setSubscriptionChangedObject(subChangedObject);
588
589        Object defaultSubChangedObject = new Object();
590        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
591
592        final int PHONE_ID = 0;
593        final int FIRST_SUB_ID = 0;
594        final int SECOND_SUB_ID = 1;
595
596        testHandler.blockTilIdle();
597        Rlog.d(TAG, "1");
598
599        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
600                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
601
602        testHandler.blockTilIdle();
603        Rlog.d(TAG, "2");
604
605        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
606                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
607
608        testHandler.blockTilIdle();
609        Rlog.d(TAG, "3");
610
611        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
612
613        testHandler.blockTilIdle();
614        Rlog.d(TAG, "4");
615
616        subController.setDefaultDataSubId(FIRST_SUB_ID);
617
618        testHandler.blockTilIdle();
619        Rlog.d(TAG, "5");
620
621        if (testHandler.getSubscriptionChangedCount() != 2) {
622            fail("getSubscriptionChangedCount reported " +
623                    testHandler.getSubscriptionChangedCount() + " != 2");
624        }
625        // 1 gained for reg  and 1 for the setting above
626        if (testHandler.getDefaultSubscriptionChangedCount() != 2) {
627            fail("getDefaultSubscriptionChangedCount reported " +
628                    testHandler.getDefaultSubscriptionChangedCount() + " != 2");
629        }
630
631        Rlog.d(TAG, "6");
632
633        // cause a notification that subscription info changed
634        subController.notifySubscriptionInfoChanged();
635        testHandler.blockTilIdle();
636
637        Rlog.d(TAG, "7");
638
639        if (testHandler.getSubscriptionChangedCount() != 2) {
640            fail("getSubscriptionChangedCount reported " +
641                    testHandler.getSubscriptionChangedCount() + " != 2");
642        }
643        if (testHandler.getDefaultSubscriptionChangedCount() != 2) {
644            fail("getDefaultSubscriptionChangedCount reported " +
645                    testHandler.getDefaultSubscriptionChangedCount() + " != 2");
646        }
647
648        // now change the default - should cause a default notification (we lost the default)
649        subController.setDefaultDataSubId(SECOND_SUB_ID);
650
651        testHandler.blockTilIdle();
652        Rlog.d(TAG, "8");
653
654        if (testHandler.getSubscriptionChangedCount() != 2) {
655            fail("getSubscriptionChangedCount reported " +
656                    testHandler.getSubscriptionChangedCount() + " != 2");
657        }
658        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
659            fail("getDefaultSubscriptionChangedCount reported " +
660                    testHandler.getDefaultSubscriptionChangedCount() + " != 3");
661        }
662        testHandler.die();
663    }
664
665    /**
666     * Try setting the subIds first and then the default subId and verify we get all our
667     * notifications.
668     */
669    @SmallTest
670    public void testSubBeforeDefaultNotifications() throws Exception {
671        final int numPhones = 2;
672        final ContextFixture contextFixture = new ContextFixture();
673        final Context context = contextFixture.getTestDouble();
674        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
675        SubscriptionControllerMock subController =
676                new SubscriptionControllerMock(context, telRegistry, numPhones);
677
678        SubscriptionMonitor testedSubMonitor =
679                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
680
681        TestHandler testHandler = TestHandler.makeHandler();
682        Object subChangedObject = new Object();
683        testHandler.setSubscriptionChangedObject(subChangedObject);
684
685        Object defaultSubChangedObject = new Object();
686        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
687
688        final int PHONE_ID = 0;
689        final int SECOND_PHONE_ID = 1;
690        final int FIRST_SUB_ID = 0;
691        final int SECOND_SUB_ID = 1;
692        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
693                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
694        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
695                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
696        subController.setSlotSubId(PHONE_ID, -2);
697        subController.setSlotSubId(SECOND_PHONE_ID, -3);
698        testHandler.blockTilIdle();
699        // should get one for registration and 1 for the change
700        if (testHandler.getSubscriptionChangedCount() != 2) {
701            fail("test1 " + testHandler.getSubscriptionChangedCount() + " != 2");
702        }
703        // should get one for registration
704        if (testHandler.getDefaultSubscriptionChangedCount() != 1) {
705            fail("test2 " + testHandler.getDefaultSubscriptionChangedCount() + " != 1");
706        }
707
708        subController.setDefaultDataSubId(FIRST_SUB_ID);
709        testHandler.blockTilIdle();
710
711        // no change
712        if (testHandler.getSubscriptionChangedCount() != 2) {
713            fail("test3 " + testHandler.getSubscriptionChangedCount() + " != 2");
714        }
715        if (testHandler.getDefaultSubscriptionChangedCount() != 1) {
716            fail("test4 " + testHandler.getDefaultSubscriptionChangedCount() + " != 1");
717        }
718
719        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
720        testHandler.blockTilIdle();
721
722        // should get one more default-change-notification
723        if (testHandler.getSubscriptionChangedCount() != 3) {
724            fail("test5 " + testHandler.getSubscriptionChangedCount() + " != 3");
725        }
726        if (testHandler.getDefaultSubscriptionChangedCount() != 2) {
727            fail("test6 " + testHandler.getDefaultSubscriptionChangedCount() + " != 2");
728        }
729
730        subController.setDefaultDataSubId(SECOND_SUB_ID);
731        testHandler.blockTilIdle();
732
733        // should get one more default-change-notification
734        if (testHandler.getSubscriptionChangedCount() != 3) {
735            fail("test7 " + testHandler.getSubscriptionChangedCount() + " != 3");
736        }
737        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
738            fail("test8 " + testHandler.getDefaultSubscriptionChangedCount() + " != 3");
739        }
740
741        subController.setDefaultDataSubId(FIRST_SUB_ID);
742        testHandler.blockTilIdle();
743
744        // should get one more default-change-notification
745        if (testHandler.getSubscriptionChangedCount() != 3) {
746            fail("test9 " + testHandler.getSubscriptionChangedCount() + " != 3");
747        }
748        if (testHandler.getDefaultSubscriptionChangedCount() != 4) {
749            fail("test10 " + testHandler.getDefaultSubscriptionChangedCount() + " != 4");
750        }
751
752        testHandler.die();
753    }
754
755    /**
756     * It turns out when we swap sims on a single sim we do something like:
757     *   Phone[0] subId  1 -> -2
758     *   Phone[0] subId -2 ->  2
759     *   Default change  1 ->  2
760     * Try that and verify we get all the subId and default changes we expect.
761     */
762    @SmallTest
763    public void testSimSwapNotifications() throws Exception {
764        final int numPhones = 1;
765        final ContextFixture contextFixture = new ContextFixture();
766        final Context context = contextFixture.getTestDouble();
767        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
768        SubscriptionControllerMock subController =
769                new SubscriptionControllerMock(context, telRegistry, numPhones);
770
771        SubscriptionMonitor testedSubMonitor =
772                new SubscriptionMonitor(telRegistry, context, subController, numPhones);
773
774        TestHandler testHandler = TestHandler.makeHandler();
775        Object subChangedObject = new Object();
776        testHandler.setSubscriptionChangedObject(subChangedObject);
777
778        Object defaultSubChangedObject = new Object();
779        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);
780
781        final int PHONE_ID = 0;
782        final int FIRST_SUB_ID = 0;
783        final int SECOND_SUB_ID = 1;
784        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
785                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
786        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
787                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
788        subController.setSlotSubId(PHONE_ID, -2);
789        testHandler.blockTilIdle();
790        // should get one for registration and 1 for the change
791        if (testHandler.getSubscriptionChangedCount() != 2) {
792            fail("test1 " + testHandler.getSubscriptionChangedCount() + " != 2");
793        }
794        // should get one for registration
795        if (testHandler.getDefaultSubscriptionChangedCount() != 1) {
796            fail("test2 " + testHandler.getDefaultSubscriptionChangedCount() + " != 1");
797        }
798
799        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
800        testHandler.blockTilIdle();
801        if (testHandler.getSubscriptionChangedCount() != 3) {
802            fail("test3 " + testHandler.getSubscriptionChangedCount() + " != 3");
803        }
804
805        subController.setDefaultDataSubId(FIRST_SUB_ID);
806        testHandler.blockTilIdle();
807        if (testHandler.getDefaultSubscriptionChangedCount() != 2) {
808            fail("test4 " + testHandler.getDefaultSubscriptionChangedCount() + " != 2");
809        }
810
811        // ok - now for the sim swap
812        subController.setSlotSubId(PHONE_ID, -2);
813        testHandler.blockTilIdle();
814        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
815            fail("test5 " + testHandler.getDefaultSubscriptionChangedCount() + " != 3");
816        }
817        if (testHandler.getSubscriptionChangedCount() != 4) {
818            fail("test6 " + testHandler.getSubscriptionChangedCount() + " != 4");
819        }
820
821        subController.setSlotSubId(PHONE_ID, SECOND_SUB_ID);
822        testHandler.blockTilIdle();
823
824        if (testHandler.getSubscriptionChangedCount() != 5) {
825            fail("test7 " + testHandler.getSubscriptionChangedCount() + " != 5");
826        }
827
828        subController.setDefaultDataSubId(SECOND_SUB_ID);
829        testHandler.blockTilIdle();
830
831        if (testHandler.getDefaultSubscriptionChangedCount() != 4) {
832            fail("test8 " + testHandler.getDefaultSubscriptionChangedCount() + " != 4");
833        }
834        // no change
835        if (testHandler.getSubscriptionChangedCount() != 5) {
836            fail("test9 " + testHandler.getSubscriptionChangedCount() + " != 5");
837        }
838
839        testHandler.die();
840    }
841}
842