1/*
2 * Copyright (C) 2016 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 com.android.internal.telephony.TelephonyTestUtils.waitForMs;
20import junit.framework.TestCase;
21
22public class ClientWakelockTrackerTest extends TestCase {
23    ClientWakelockTracker myTracker;
24
25    public void setUp() throws Exception {
26        super.setUp();
27        myTracker = new ClientWakelockTracker();
28    }
29
30    public void tearDown() throws Exception {
31        super.tearDown();
32    }
33
34    /* This test has client "ABC" send 1 message at time t and gets response at t+40,
35    client "PQR" sends a message at t+20 and gets response at t+120. Verify that
36    "ABC" is attributed 30ms and "PQR" 90ms of the total wakelock time of 120ms
37     */
38    public void testTwoClients() throws Exception {
39        myTracker.startTracking("ABC", 101, 1, 1);
40        waitForMs(20);
41        assertEquals(1, myTracker.mActiveClients.size());
42        myTracker.startTracking("PQR", 102, 2, 2);
43        assertEquals(2, myTracker.mActiveClients.size());
44        ClientWakelockAccountant abc = myTracker.mClients.get("ABC");
45        ClientWakelockAccountant pqr = myTracker.mClients.get("PQR");
46        assertEquals(2, abc.mPendingRilWakelocks.get(0).getConcurrentRequests());
47        assertEquals(2, pqr.mPendingRilWakelocks.get(0).getConcurrentRequests());
48        waitForMs(20);
49        myTracker.stopTracking("ABC", 101, 1, 1);
50        assertEquals(1, myTracker.mActiveClients.size());
51        assertEquals(0, abc.getPendingRequestCount());
52        assertEquals(1, pqr.mPendingRilWakelocks.get(0).getConcurrentRequests());
53        waitForMs(80);
54        myTracker.stopTracking("PQR", 102, 2, 0);
55        assertEquals(0, myTracker.mActiveClients.size());
56        assertEquals(0, abc.getPendingRequestCount());
57        assertEquals(0, pqr.getPendingRequestCount());
58
59        assertTimeTaken(abc, 30);
60        assertTimeTaken(pqr, 90);
61    }
62
63    private void assertTimeTaken(ClientWakelockAccountant abc, int time) {
64        assertTrue(abc.mRequestStats.getCompletedRequestsWakelockTime() > (time - 1));
65        assertTrue(abc.mRequestStats.getCompletedRequestsWakelockTime() < (time + 19));
66    }
67
68    /* This test has client "ABC" send 1 message at time t and gets response at t+40,
69    and sends another message at t+20 and gets response at t+120. Verify that
70    "ABC" is attributed 120ms
71     */
72    public void testOneClient() throws Exception {
73        myTracker.startTracking("ABC", 101, 1, 1);
74        waitForMs(20);
75        assertEquals(1, myTracker.mActiveClients.size());
76        myTracker.startTracking("ABC", 102, 2, 2);
77        assertEquals(1, myTracker.mActiveClients.size());
78        ClientWakelockAccountant abc = myTracker.mClients.get("ABC");
79        assertEquals(2, abc.mPendingRilWakelocks.get(0).getConcurrentRequests());
80        assertEquals(2, abc.mPendingRilWakelocks.get(1).getConcurrentRequests());
81        waitForMs(20);
82        myTracker.stopTracking("ABC", 101, 1, 1);
83        assertEquals(1, myTracker.mActiveClients.size());
84        assertEquals(1, abc.getPendingRequestCount());
85        assertEquals(1, abc.mPendingRilWakelocks.get(0).getConcurrentRequests());
86        waitForMs(80);
87        myTracker.stopTracking("ABC", 102, 2, 0);
88        assertEquals(0, myTracker.mActiveClients.size());
89        assertEquals(0, abc.getPendingRequestCount());
90        assertEquals(2, abc.mRequestStats.getCompletedRequestsCount());
91
92        assertTimeTaken(abc, 120);
93    }
94
95    /* This test has client "ABC" send 1 message at time t and another at time t+20
96    and gets response for all at t+40. Verify that "ABC" is attributed 40ms
97     */
98    public void testStopTrackingAllOneClient() throws Exception {
99        myTracker.startTracking("ABC", 101, 1, 1);
100        waitForMs(20);
101        assertEquals(1, myTracker.mActiveClients.size());
102        myTracker.startTracking("ABC", 102, 2, 2);
103        ClientWakelockAccountant abc = myTracker.mClients.get("ABC");
104        assertEquals(1, myTracker.mActiveClients.size());
105        assertEquals(2, abc.mPendingRilWakelocks.get(0).getConcurrentRequests());
106        assertEquals(2, abc.mPendingRilWakelocks.get(1).getConcurrentRequests());
107        waitForMs(20);
108        myTracker.stopTrackingAll();
109        assertEquals(0, myTracker.mActiveClients.size());
110        assertEquals(0, abc.getPendingRequestCount());
111        assertEquals(2, abc.mRequestStats.getCompletedRequestsCount());
112
113        assertTimeTaken(abc, 40);
114    }
115
116    /* This test has client "ABC" send 1 message at time t and client "PQR" sends 1 message
117     at time (t+20)ms. Both of them get response at (t+40). Verify that
118    "ABC" is attributed 30ms amd PQR is attributed 10ms
119     */
120    public void testStopTrackingAllTwoClients() throws Exception {
121        myTracker.startTracking("ABC", 101, 1, 1);
122        waitForMs(20);
123        assertEquals(1, myTracker.mActiveClients.size());
124        myTracker.startTracking("PQR", 102, 2, 2);
125        ClientWakelockAccountant abc = myTracker.mClients.get("ABC");
126        ClientWakelockAccountant pqr = myTracker.mClients.get("PQR");
127        assertEquals(2, myTracker.mActiveClients.size());
128        assertEquals(2, abc.mPendingRilWakelocks.get(0).getConcurrentRequests());
129        assertEquals(2, pqr.mPendingRilWakelocks.get(0).getConcurrentRequests());
130        waitForMs(20);
131        myTracker.stopTrackingAll();
132        assertEquals(0, myTracker.mActiveClients.size());
133        assertEquals(0, abc.getPendingRequestCount());
134        assertEquals(1, abc.mRequestStats.getCompletedRequestsCount());
135        assertEquals(0, pqr.getPendingRequestCount());
136
137        assertTimeTaken(pqr, 10);
138        assertTimeTaken(abc, 30);
139    }
140}
141