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.metrics;
18
19import android.test.suitebuilder.annotation.SmallTest;
20
21import com.android.internal.telephony.TelephonyProto;
22import com.android.internal.telephony.TelephonyTest;
23
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertTrue;
31
32public class InProgressSmsSessionTest extends TelephonyTest {
33
34    private InProgressSmsSession mSmsSession;
35
36    @Before
37    public void setUp() throws Exception {
38        super.setUp(getClass().getSimpleName());
39        mSmsSession = new InProgressSmsSession(mPhone.getPhoneId());
40    }
41
42    @After
43    public void tearDown() throws Exception {
44        super.tearDown();
45    }
46
47    // Test add event
48    @Test
49    @SmallTest
50    public void testAddEvent() {
51        SmsSessionEventBuilder builder = new SmsSessionEventBuilder(
52                TelephonyProto.SmsSession.Event.Type.SMS_SEND_RESULT)
53                .setRilRequestId(2);
54        mSmsSession.addEvent(builder);
55        assertEquals(builder.build(), mSmsSession.events.getFirst());
56        assertFalse(mSmsSession.isEventsDropped());
57    }
58
59    // Test dropped event scenario
60    @Test
61    @SmallTest
62    public void testEventDropped() {
63        for (int i = 0; i < 25; i++) {
64            SmsSessionEventBuilder builder = new SmsSessionEventBuilder(
65                    TelephonyProto.SmsSession.Event.Type.SMS_SEND_RESULT)
66                    .setRilRequestId(i + 1);
67            mSmsSession.addEvent(builder);
68        }
69
70        assertTrue(mSmsSession.isEventsDropped());
71        assertEquals(6, mSmsSession.events.getFirst().getRilRequestId());
72    }
73
74    // Test dropped event scenario
75    @Test
76    @SmallTest
77    public void testExpectedResponse() {
78        mSmsSession.increaseExpectedResponse();
79        assertEquals(1, mSmsSession.getNumExpectedResponses());
80        mSmsSession.decreaseExpectedResponse();
81        assertEquals(0, mSmsSession.getNumExpectedResponses());
82
83        for (int i = 0; i < 100; i++) {
84            mSmsSession.increaseExpectedResponse();
85        }
86
87        assertEquals(100, mSmsSession.getNumExpectedResponses());
88
89        for (int i = 0; i < 50; i++) {
90            mSmsSession.decreaseExpectedResponse();
91        }
92
93        assertEquals(50, mSmsSession.getNumExpectedResponses());
94    }
95}