1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.telephony.ims.cts;
18 
19 import android.telephony.ims.stub.ImsRegistrationImplBase;
20 import android.util.Log;
21 
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.TimeUnit;
26 
27 public class TestImsRegistration extends ImsRegistrationImplBase {
28 
29     private static final String TAG = "TestImsRegistration";
30 
31     private boolean mDeregistrationTriggered = false;
32     private @ImsRegistrationImplBase.ImsDeregistrationReason int mDeregistrationReason =
33             ImsRegistrationImplBase.REASON_UNKNOWN;
34 
TestImsRegistration()35     public TestImsRegistration() {
36         Log.d(TAG, "TestImsRegistration with default constructor");
37     }
38 
TestImsRegistration(Executor executor)39     public TestImsRegistration(Executor executor) {
40         super(executor);
41         Log.d(TAG, "TestImsRegistration with Executor constructor");
42     }
43 
44     public static class NetworkRegistrationInfo {
45         public final int sipCode;
46         public final String sipReason;
NetworkRegistrationInfo(int code, String reason)47         NetworkRegistrationInfo(int code, String reason) {
48             sipCode = code;
49             sipReason = reason;
50         }
51     }
52 
53     public static final int LATCH_UPDATE_REGISTRATION = 0;
54     public static final int LATCH_TRIGGER_DEREGISTRATION = 1;
55     public static final int LATCH_TRIGGER_DEREGISTRATION_BY_RADIO = 2;
56     private static final int LATCH_MAX = 3;
57     private static final CountDownLatch[] sLatches = new CountDownLatch[LATCH_MAX];
58     static {
59         for (int i = 0; i < LATCH_MAX; i++) {
60             sLatches[i] = new CountDownLatch(1);
61         }
62     }
63 
64     private final LinkedBlockingQueue<NetworkRegistrationInfo> mPendingFullRegistrationRequests =
65             new LinkedBlockingQueue<>();
66 
67     @Override
triggerFullNetworkRegistration(int sipCode, String sipReason)68     public void triggerFullNetworkRegistration(int sipCode, String sipReason) {
69         mPendingFullRegistrationRequests.offer(new NetworkRegistrationInfo(sipCode, sipReason));
70     }
71 
72     @Override
updateSipDelegateRegistration()73     public void updateSipDelegateRegistration() {
74         synchronized (sLatches) {
75             sLatches[LATCH_UPDATE_REGISTRATION].countDown();
76         }
77     }
78 
79     @Override
triggerSipDelegateDeregistration()80     public void triggerSipDelegateDeregistration() {
81         synchronized (sLatches) {
82             sLatches[LATCH_TRIGGER_DEREGISTRATION].countDown();
83         }
84     }
85 
86     @Override
triggerDeregistration(@msRegistrationImplBase.ImsDeregistrationReason int reason)87     public void triggerDeregistration(@ImsRegistrationImplBase.ImsDeregistrationReason int reason) {
88         mDeregistrationTriggered = true;
89         mDeregistrationReason = reason;
90         synchronized (sLatches) {
91             sLatches[LATCH_TRIGGER_DEREGISTRATION_BY_RADIO].countDown();
92         }
93     }
94 
getNextFullNetworkRegRequest(int timeoutMs)95     public NetworkRegistrationInfo getNextFullNetworkRegRequest(int timeoutMs) throws Exception {
96         return mPendingFullRegistrationRequests.poll(timeoutMs, TimeUnit.MILLISECONDS);
97     }
98 
resetLatch(int latchIndex, int newCount)99     public void resetLatch(int latchIndex, int newCount) {
100         synchronized (sLatches) {
101             sLatches[latchIndex] = new CountDownLatch(newCount);
102         }
103     }
104 
waitForLatchCountDown(int latchIndex, int timeoutMs)105     public boolean waitForLatchCountDown(int latchIndex, int timeoutMs) {
106         CountDownLatch latch;
107         synchronized (sLatches) {
108             latch = sLatches[latchIndex];
109         }
110         while (latch.getCount() > 0) {
111             try {
112                 return latch.await(timeoutMs, TimeUnit.MILLISECONDS);
113             } catch (InterruptedException e) { }
114         }
115         return true;
116     }
117 
resetDeregistrationTriggeredByRadio()118     public void resetDeregistrationTriggeredByRadio() {
119         mDeregistrationTriggered = false;
120         mDeregistrationReason = ImsRegistrationImplBase.REASON_UNKNOWN;
121         sLatches[LATCH_TRIGGER_DEREGISTRATION_BY_RADIO] = new CountDownLatch(1);
122     }
123 
isDeregistrationTriggeredByRadio()124     public boolean isDeregistrationTriggeredByRadio() {
125         return mDeregistrationTriggered;
126     }
127 
getDeregistrationTriggeredByRadioReason()128     public int getDeregistrationTriggeredByRadioReason() {
129         return mDeregistrationReason;
130     }
131 }
132