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 
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.LinkedBlockingQueue;
23 import java.util.concurrent.TimeUnit;
24 
25 public class TestImsRegistration extends ImsRegistrationImplBase {
26 
27     public static class NetworkRegistrationInfo {
28         public final int sipCode;
29         public final String sipReason;
NetworkRegistrationInfo(int code, String reason)30         NetworkRegistrationInfo(int code, String reason) {
31             sipCode = code;
32             sipReason = reason;
33         }
34     }
35 
36     public static final int LATCH_UPDATE_REGISTRATION = 0;
37     public static final int LATCH_TRIGGER_DEREGISTRATION = 1;
38     private static final int LATCH_MAX = 2;
39     private static final CountDownLatch[] sLatches = new CountDownLatch[LATCH_MAX];
40     static {
41         for (int i = 0; i < LATCH_MAX; i++) {
42             sLatches[i] = new CountDownLatch(1);
43         }
44     }
45 
46     private final LinkedBlockingQueue<NetworkRegistrationInfo> mPendingFullRegistrationRequests =
47             new LinkedBlockingQueue<>();
48 
49     @Override
triggerFullNetworkRegistration(int sipCode, String sipReason)50     public void triggerFullNetworkRegistration(int sipCode, String sipReason) {
51         mPendingFullRegistrationRequests.offer(new NetworkRegistrationInfo(sipCode, sipReason));
52     }
53 
54     @Override
updateSipDelegateRegistration()55     public void updateSipDelegateRegistration() {
56         synchronized (sLatches) {
57             sLatches[LATCH_UPDATE_REGISTRATION].countDown();
58         }
59     }
60 
61     @Override
triggerSipDelegateDeregistration()62     public void triggerSipDelegateDeregistration() {
63         synchronized (sLatches) {
64             sLatches[LATCH_TRIGGER_DEREGISTRATION].countDown();
65         }
66     }
67 
getNextFullNetworkRegRequest(int timeoutMs)68     public NetworkRegistrationInfo getNextFullNetworkRegRequest(int timeoutMs) throws Exception {
69         return mPendingFullRegistrationRequests.poll(timeoutMs, TimeUnit.MILLISECONDS);
70     }
71 
resetLatch(int latchIndex, int newCount)72     public void resetLatch(int latchIndex, int newCount) {
73         synchronized (sLatches) {
74             sLatches[latchIndex] = new CountDownLatch(newCount);
75         }
76     }
77 
waitForLatchCountDown(int latchIndex, int timeoutMs)78     public boolean waitForLatchCountDown(int latchIndex, int timeoutMs) {
79         CountDownLatch latch;
80         synchronized (sLatches) {
81             latch = sLatches[latchIndex];
82         }
83         while (latch.getCount() > 0) {
84             try {
85                 return latch.await(timeoutMs, TimeUnit.MILLISECONDS);
86             } catch (InterruptedException e) { }
87         }
88         return true;
89     }
90 }
91