From f2fdf411925ad172b5e0b25b0c6df880256691d4 Mon Sep 17 00:00:00 2001 From: Sohani Rao Date: Mon, 19 Dec 2016 18:42:19 -0800 Subject: Log API surfaces of WifiService Log following ways to interact with WifiServiceImpl - AIDL methods, trace calling uid - AsyncChannel messages sent from and handled by WifiService using WifiAsyncChannel instead of AsyncChannel - AsyncChannel messages sent to WifiService using WifiHandler instead of Handler This CL changes the construction of WifiServiceImpl to use the WifiAsyncChannel object instead. Exercise WifiAsyncChannel using tests added in WifiServiceImplTest and mock logging objects. Bug: 33780715 Test: Unit tests, Sanity tests (power on and connect to AP), and verify logging Change-Id: I7a56806ea1b166d15a4c75e5e0d5bfcaf23c7b98 --- .../android/server/wifi/WifiServiceImplTest.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index c5b0f8a4c..bba9977cd 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -16,21 +16,30 @@ package com.android.server.wifi; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import android.content.Context; import android.content.res.Resources; +import android.os.Handler; import android.os.HandlerThread; +import android.os.Looper; +import android.os.Message; +import android.os.Messenger; import android.os.test.TestLooper; import android.test.suitebuilder.annotation.SmallTest; import com.android.internal.util.AsyncChannel; +import com.android.server.wifi.util.WifiAsyncChannel; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.mockito.Spy; /** * Unit tests for {@link WifiServiceImpl}. @@ -58,6 +67,60 @@ public class WifiServiceImplTest { @Mock WifiMulticastLockManager mWifiMulticastLockManager; @Mock WifiLastResortWatchdog mWifiLastResortWatchdog; @Mock WifiBackupRestore mWifiBackupRestore; + @Spy FakeWifiLog mLog; + + private class WifiAsyncChannelTester { + private static final String TAG = "WifiAsyncChannelTester"; + public static final int CHANNEL_STATE_FAILURE = -1; + public static final int CHANNEL_STATE_DISCONNECTED = 0; + public static final int CHANNEL_STATE_HALF_CONNECTED = 1; + public static final int CHANNEL_STATE_FULLY_CONNECTED = 2; + + private int mState = CHANNEL_STATE_DISCONNECTED; + private WifiAsyncChannel mChannel; + private WifiLog mAsyncTestLog; + + WifiAsyncChannelTester(WifiInjector wifiInjector) { + mAsyncTestLog = wifiInjector.makeLog(TAG); + } + + public int getChannelState() { + return mState; + } + + public void connect(final Looper looper, final Messenger messenger, + final Handler incomingMessageHandler) { + assertEquals("AsyncChannel must be in disconnected state", + CHANNEL_STATE_DISCONNECTED, mState); + mChannel = new WifiAsyncChannel(TAG); + mChannel.setWifiLog(mLog); + Handler handler = new Handler(mLooper.getLooper()) { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: + if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { + mChannel.sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); + mState = CHANNEL_STATE_HALF_CONNECTED; + } else { + mState = CHANNEL_STATE_FAILURE; + } + break; + case AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED: + mState = CHANNEL_STATE_FULLY_CONNECTED; + break; + case AsyncChannel.CMD_CHANNEL_DISCONNECTED: + mState = CHANNEL_STATE_DISCONNECTED; + break; + default: + incomingMessageHandler.handleMessage(msg); + break; + } + } + }; + mChannel.connect(null, handler, messenger); + } + } @Before public void setUp() { MockitoAnnotations.initMocks(this); @@ -67,12 +130,20 @@ public class WifiServiceImplTest { when(mWifiInjector.getWifiServiceHandlerThread()).thenReturn(mHandlerThread); when(mHandlerThread.getLooper()).thenReturn(mLooper.getLooper()); when(mContext.getResources()).thenReturn(mResources); + WifiAsyncChannel wifiAsyncChannel = new WifiAsyncChannel("WifiServiceImplTest"); + wifiAsyncChannel.setWifiLog(mLog); + when(mFrameworkFacade.makeWifiAsyncChannel(anyString())).thenReturn(wifiAsyncChannel); when(mWifiInjector.getFrameworkFacade()).thenReturn(mFrameworkFacade); when(mWifiInjector.getWifiLockManager()).thenReturn(mLockManager); when(mWifiInjector.getWifiMulticastLockManager()).thenReturn(mWifiMulticastLockManager); when(mWifiInjector.getWifiLastResortWatchdog()).thenReturn(mWifiLastResortWatchdog); when(mWifiInjector.getWifiBackupRestore()).thenReturn(mWifiBackupRestore); + when(mWifiInjector.makeLog(anyString())).thenReturn(mLog); + WifiTrafficPoller wifiTrafficPoller = new WifiTrafficPoller(mContext, + mLooper.getLooper(), "mockWlan"); + when(mWifiInjector.getWifiTrafficPoller()).thenReturn(wifiTrafficPoller); mWifiServiceImpl = new WifiServiceImpl(mContext, mWifiInjector, mAsyncChannel); + mWifiServiceImpl.setWifiHandlerLogForTest(mLog); } @Test @@ -80,4 +151,16 @@ public class WifiServiceImplTest { assertFalse(mWifiServiceImpl.removeNetwork(-1)); } + @Test + public void testAsyncChannelHalfConnected() { + WifiAsyncChannelTester channelTester = new WifiAsyncChannelTester(mWifiInjector); + Handler handler = mock(Handler.class); + TestLooper looper = new TestLooper(); + channelTester.connect(looper.getLooper(), mWifiServiceImpl.getWifiServiceMessenger(), + handler); + mLooper.dispatchAll(); + assertEquals("AsyncChannel must be half connected", + WifiAsyncChannelTester.CHANNEL_STATE_HALF_CONNECTED, + channelTester.getChannelState()); + } } -- cgit v1.2.3