1/*
2 * Copyright (C) 2017 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.server.wifi;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.*;
21
22import android.net.wifi.WifiConfiguration;
23import android.net.wifi.WifiManager;
24import android.os.Handler;
25import android.os.IBinder;
26import android.os.Message;
27import android.os.Messenger;
28import android.os.Process;
29import android.os.RemoteException;
30import android.os.test.TestLooper;
31import android.test.suitebuilder.annotation.SmallTest;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37
38/*
39 * Unit tests for {@link com.android.server.wifi.LocalOnlyHotspotRequestInfo}.
40 */
41@SmallTest
42public class LocalOnlyHotspotRequestInfoTest {
43
44    private static final String TAG = "LocalOnlyHotspotRequestInfoTest";
45    @Mock IBinder mAppBinder;
46    @Mock LocalOnlyHotspotRequestInfo.RequestingApplicationDeathCallback mCallback;
47    private Handler mHandler;
48    private Messenger mMessenger;
49    private TestLooper mTestLooper;
50    RemoteException mRemoteException;
51    private LocalOnlyHotspotRequestInfo mLOHSRequestInfo;
52
53    /**
54     * Setup test.
55     */
56    @Before
57    public void setUp() {
58        MockitoAnnotations.initMocks(this);
59        mTestLooper = new TestLooper();
60        mHandler = new Handler(mTestLooper.getLooper());
61        mMessenger = new Messenger(mHandler);
62        mRemoteException = new RemoteException("Test Remote Exception");
63    }
64
65    /**
66     * Make sure we link the call to request LocalOnlyHotspot by an app is linked to their Binder
67     * call.  This allows us to clean up if the app dies.
68     */
69    @Test
70    public void verifyBinderLinkToDeathIsCalled() throws Exception {
71        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
72        verify(mAppBinder).linkToDeath(eq(mLOHSRequestInfo), eq(0));
73    }
74
75    /**
76     * Calls to create the requestor to binder death should not pass null callback.
77     */
78    @Test(expected = NullPointerException.class)
79    public void verifyNullCallbackChecked() throws Exception {
80        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, null);
81    }
82
83    /**
84     * Calls to create the request info object should not pass a null messenger.
85     */
86    @Test(expected = NullPointerException.class)
87    public void verifyNullMessengerChecked() throws Exception {
88        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, null, mCallback);
89    }
90
91    /**
92     * Calls to link the requestor to binder death should not pass null binder
93     */
94    @Test(expected = NullPointerException.class)
95    public void verifyNullBinderChecked() throws Exception {
96        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(null, mMessenger, mCallback);
97    }
98
99    /**
100     * Calls to unlink the DeathRecipient should call to unlink from Binder.
101     */
102    @Test
103    public void verifyUnlinkDeathRecipientUnlinksFromBinder() throws Exception {
104        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
105        mLOHSRequestInfo.unlinkDeathRecipient();
106        verify(mAppBinder).unlinkToDeath(eq(mLOHSRequestInfo), eq(0));
107    }
108
109    /**
110     * Binder death notification should trigger a callback on the requestor.
111     */
112    @Test
113    public void verifyBinderDeathTriggersCallback() {
114        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
115        mLOHSRequestInfo.binderDied();
116        verify(mCallback).onLocalOnlyHotspotRequestorDeath(eq(mLOHSRequestInfo));
117    }
118
119    /**
120     * Verify a RemoteException when calling linkToDeath triggers the callback.
121     */
122    @Test
123    public void verifyRemoteExceptionTriggersCallback() throws Exception {
124        doThrow(mRemoteException).when(mAppBinder)
125                .linkToDeath(any(IBinder.DeathRecipient.class), eq(0));
126        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
127        verify(mCallback).onLocalOnlyHotspotRequestorDeath(eq(mLOHSRequestInfo));
128    }
129
130    /**
131     * Verify the pid is properly set.
132     */
133    @Test
134    public void verifyPid() {
135        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
136        assertEquals(Process.myPid(), mLOHSRequestInfo.getPid());
137    }
138
139    /**
140     * Verify that sendHotspotFailedMessage does send a Message properly
141     */
142    @Test
143    public void verifySendFailedMessage() throws Exception {
144        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
145        mLOHSRequestInfo.sendHotspotFailedMessage(
146                WifiManager.LocalOnlyHotspotCallback.ERROR_GENERIC);
147        Message message = mTestLooper.nextMessage();
148        assertEquals(WifiManager.HOTSPOT_FAILED, message.what);
149        assertEquals(WifiManager.LocalOnlyHotspotCallback.ERROR_GENERIC, message.arg1);
150    }
151
152    /**
153     * Verify that sendHotspotStartedMessage does send a Message properly
154     */
155    @Test
156    public void verifySendStartedMessage() throws Exception {
157        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
158        WifiConfiguration config = mock(WifiConfiguration.class);
159        mLOHSRequestInfo.sendHotspotStartedMessage(config);
160        Message message = mTestLooper.nextMessage();
161        assertEquals(WifiManager.HOTSPOT_STARTED, message.what);
162        assertEquals(config, (WifiConfiguration) message.obj);
163    }
164
165    /**
166     * Verify that sendHotspotStoppedMessage does send a Message properly
167     */
168    @Test
169    public void verifySendStoppedMessage() throws Exception {
170        mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback);
171        mLOHSRequestInfo.sendHotspotStoppedMessage();
172        Message message = mTestLooper.nextMessage();
173        assertEquals(WifiManager.HOTSPOT_STOPPED, message.what);
174    }
175}
176