summaryrefslogtreecommitdiff
path: root/InCallUI/tests/src/com/android/incallui/spam/SpamCallListListenerTest.java
blob: fb0b460b6fa22b09e9957f923737be1688e1b27f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */


package com.android.incallui.spam;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.any;

import android.content.ContentValues;
import android.content.Context;
import android.provider.CallLog;
import android.telecom.DisconnectCause;
import android.test.InstrumentationTestCase;

import com.android.contacts.common.test.mocks.ContactsMockContext;
import com.android.contacts.common.test.mocks.MockContentProvider;
import com.android.dialer.calllog.CallLogAsyncTaskUtil;
import com.android.dialer.util.AsyncTaskExecutors;
import com.android.dialer.util.FakeAsyncTaskExecutor;
import com.android.dialer.util.TelecomUtil;
import com.android.incallui.Call;
import com.android.incallui.Call.CallHistoryStatus;
import com.android.incallui.Call.LogState;

public class SpamCallListListenerTest extends InstrumentationTestCase {
    private static final String NUMBER = "+18005657862";
    private static final int DURATION = 100;

    private TestSpamCallListListener mListener;
    private FakeAsyncTaskExecutor mFakeAsyncTaskExecutor;
    private ContactsMockContext mContext;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mContext = new ContactsMockContext(getInstrumentation().getContext(), CallLog.AUTHORITY);
        mListener = new TestSpamCallListListener(mContext);
        mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation());
        AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory());
    }

    @Override
    public void tearDown() throws Exception {
        AsyncTaskExecutors.setFactoryForTest(null);
        CallLogAsyncTaskUtil.resetForTest();
        super.tearDown();
    }

    public void testOutgoingCall() {
        Call call = getMockCall(NUMBER, false, 0, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.REMOTE);
        mListener.onDisconnect(call);
        assertFalse(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_UnknownNumber() {
        Call call = getMockCall(null, true, DURATION, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.REMOTE);
        mListener.onDisconnect(call);
        assertFalse(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_Rejected() {
        Call call = getMockCall(NUMBER, true, 0, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.REJECTED);
        mListener.onDisconnect(call);
        assertFalse(mListener.mShowNotificationCalled);
    }
    public void testIncomingCall_HangUpLocal() {
        Call call = getMockCall(NUMBER, true, DURATION, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.LOCAL);
        mListener.onDisconnect(call);
        assertTrue(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_HangUpRemote() {
        Call call = getMockCall(NUMBER, true, DURATION, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.REMOTE);
        mListener.onDisconnect(call);
        assertTrue(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_ValidNumber_NotInCallHistory_InContacts() {
        Call call = getMockCall(NUMBER, true, 0, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_LOCAL_CONTACT, DisconnectCause.REJECTED);
        mListener.onDisconnect(call);
        assertFalse(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_ValidNumber_InCallHistory_InContacts() {
        Call call = getMockCall(NUMBER, true, 0, Call.CALL_HISTORY_STATUS_PRESENT,
                LogState.LOOKUP_LOCAL_CONTACT, DisconnectCause.REJECTED);
        mListener.onDisconnect(call);
        assertFalse(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_ValidNumber_InCallHistory_NotInContacts() {
        Call call = getMockCall(NUMBER, true, 0, Call.CALL_HISTORY_STATUS_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.REJECTED);
        mListener.onDisconnect(call);
        assertFalse(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_ValidNumber_NotInCallHistory_NotInContacts() throws Throwable {
        Call call = getMockCall(NUMBER, true, DURATION, Call.CALL_HISTORY_STATUS_NOT_PRESENT,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.LOCAL);
        mListener.onDisconnect(call);
        assertTrue(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_CheckCallHistory_NumberExists() throws Throwable {
        final Call call = getMockCall(NUMBER, true, DURATION, Call.CALL_HISTORY_STATUS_UNKNOWN,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.LOCAL);
        expectCallLogQuery(NUMBER, true);
        incomingCall(call);
        verify(call).setCallHistoryStatus(eq(Call.CALL_HISTORY_STATUS_PRESENT));
        assertFalse(mListener.mShowNotificationCalled);
    }

    public void testIncomingCall_CheckCallHistory_NumberNotExists() throws Throwable {
        final Call call = getMockCall(NUMBER, true, DURATION, Call.CALL_HISTORY_STATUS_UNKNOWN,
                LogState.LOOKUP_UNKNOWN, DisconnectCause.LOCAL);
        expectCallLogQuery(NUMBER, false);
        incomingCall(call);
        verify(call).setCallHistoryStatus(eq(Call.CALL_HISTORY_STATUS_NOT_PRESENT));
        assertTrue(mListener.mShowNotificationCalled);
    }

    private void incomingCall(final Call call) throws Throwable {
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mListener.onIncomingCall(call);
            }
        });
        getInstrumentation().waitForIdleSync();
        mFakeAsyncTaskExecutor.runTask(CallLogAsyncTaskUtil.Tasks.GET_NUMBER_IN_CALL_HISTORY);
        mListener.onDisconnect(call);
    }

    private void expectCallLogQuery(String number, boolean inCallHistory) {
        MockContentProvider.Query query = mContext.getContactsProvider()
                .expectQuery(TelecomUtil.getCallLogUri(mContext))
                .withSelection(CallLog.Calls.NUMBER + " = ?", number)
                .withProjection(CallLog.Calls._ID)
                .withAnySortOrder();
        ContentValues values = new ContentValues();
        values.put(CallLog.Calls.NUMBER, number);
        if (inCallHistory) {
            query.returnRow(values);
        } else {
            query.returnEmptyCursor();
        }
    }

    private static Call getMockCall(String number,
                                    boolean isIncoming,
                                    int duration,
                                    @CallHistoryStatus int callHistoryStatus,
                                    int contactLookupResult,
                                    int disconnectCause) {
        Call call = mock(Call.class);
        LogState logState = new LogState();
        logState.isIncoming = isIncoming;
        logState.duration = duration;
        logState.contactLookupResult = contactLookupResult;
        when(call.getDisconnectCause()).thenReturn(new DisconnectCause(disconnectCause));
        when(call.getLogState()).thenReturn(logState);
        when(call.getNumber()).thenReturn(number);
        doCallRealMethod().when(call).setCallHistoryStatus(anyInt());
        when(call.getCallHistoryStatus()).thenCallRealMethod();
        call.setCallHistoryStatus(callHistoryStatus);
        return call;
    }

    private static class TestSpamCallListListener extends SpamCallListListener {
        private boolean mShowNotificationCalled;

        public TestSpamCallListListener(Context context) {
            super(context);
        }

        void showNotification(String number) {
            mShowNotificationCalled = true;
        }
    }
}