Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 1 | package android.net; |
| 2 | |
| 3 | import static android.net.NetworkRecommendationProvider.EXTRA_RECOMMENDATION_RESULT; |
| 4 | import static android.net.NetworkRecommendationProvider.EXTRA_SEQUENCE; |
| 5 | |
| 6 | import android.content.Context; |
| 7 | import android.os.Bundle; |
| 8 | import android.os.Handler; |
| 9 | import android.os.HandlerThread; |
| 10 | import android.os.IRemoteCallback; |
| 11 | import android.test.InstrumentationTestCase; |
| 12 | import android.test.suitebuilder.annotation.MediumTest; |
| 13 | import android.test.suitebuilder.annotation.SmallTest; |
| 14 | |
| 15 | import org.mockito.ArgumentCaptor; |
| 16 | import org.mockito.Mock; |
| 17 | import org.mockito.Mockito; |
| 18 | import org.mockito.MockitoAnnotations; |
| 19 | |
| 20 | import java.util.concurrent.CountDownLatch; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
| 23 | /** |
| 24 | * Unit test for the {@link NetworkRecommendationProvider}. |
| 25 | */ |
| 26 | public class NetworkRecommendationProviderTest extends InstrumentationTestCase { |
| 27 | @Mock private IRemoteCallback mMockRemoteCallback; |
| 28 | private NetworkRecProvider mRecProvider; |
| 29 | private Handler mHandler; |
| 30 | private INetworkRecommendationProvider mStub; |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 31 | private CountDownLatch mRecRequestLatch; |
| 32 | private CountDownLatch mScoreRequestLatch; |
| 33 | private NetworkKey[] mTestNetworkKeys; |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 34 | |
| 35 | @Override |
| 36 | public void setUp() throws Exception { |
| 37 | super.setUp(); |
| 38 | |
| 39 | // Configuration needed to make mockito/dexcache work. |
| 40 | final Context context = getInstrumentation().getTargetContext(); |
| 41 | System.setProperty("dexmaker.dexcache", |
| 42 | context.getCacheDir().getPath()); |
| 43 | ClassLoader newClassLoader = getInstrumentation().getClass().getClassLoader(); |
| 44 | Thread.currentThread().setContextClassLoader(newClassLoader); |
| 45 | |
| 46 | MockitoAnnotations.initMocks(this); |
| 47 | |
| 48 | HandlerThread thread = new HandlerThread("NetworkRecommendationProviderTest"); |
| 49 | thread.start(); |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 50 | mRecRequestLatch = new CountDownLatch(1); |
| 51 | mScoreRequestLatch = new CountDownLatch(1); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 52 | mHandler = new Handler(thread.getLooper()); |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 53 | mRecProvider = new NetworkRecProvider(mHandler, mRecRequestLatch, mScoreRequestLatch); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 54 | mStub = INetworkRecommendationProvider.Stub.asInterface(mRecProvider.getBinder()); |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 55 | mTestNetworkKeys = new NetworkKey[2]; |
| 56 | mTestNetworkKeys[0] = new NetworkKey(new WifiKey("\"ssid_01\"", "00:00:00:00:00:11")); |
| 57 | mTestNetworkKeys[1] = new NetworkKey(new WifiKey("\"ssid_02\"", "00:00:00:00:00:22")); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @MediumTest |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 61 | public void testRecommendationRequestReceived() throws Exception { |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 62 | final RecommendationRequest request = new RecommendationRequest.Builder().build(); |
| 63 | final int sequence = 100; |
| 64 | mStub.requestRecommendation(request, mMockRemoteCallback, sequence); |
| 65 | |
| 66 | // wait for onRequestRecommendation() to be called in our impl below. |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 67 | mRecRequestLatch.await(200, TimeUnit.MILLISECONDS); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 68 | NetworkRecommendationProvider.ResultCallback expectedResultCallback = |
| 69 | new NetworkRecommendationProvider.ResultCallback(mMockRemoteCallback, sequence); |
| 70 | assertEquals(request, mRecProvider.mCapturedRequest); |
| 71 | assertEquals(expectedResultCallback, mRecProvider.mCapturedCallback); |
| 72 | } |
| 73 | |
| 74 | @SmallTest |
| 75 | public void testResultCallbackOnResult() throws Exception { |
| 76 | final int sequence = 100; |
| 77 | final NetworkRecommendationProvider.ResultCallback callback = |
| 78 | new NetworkRecommendationProvider.ResultCallback(mMockRemoteCallback, sequence); |
| 79 | |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 80 | final RecommendationResult result = RecommendationResult.createDoNotConnectRecommendation(); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 81 | callback.onResult(result); |
| 82 | |
| 83 | final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 84 | Mockito.verify(mMockRemoteCallback).sendResult(bundleCaptor.capture()); |
| 85 | Bundle capturedBundle = bundleCaptor.getValue(); |
| 86 | assertEquals(sequence, capturedBundle.getInt(EXTRA_SEQUENCE)); |
| 87 | assertSame(result, capturedBundle.getParcelable(EXTRA_RECOMMENDATION_RESULT)); |
| 88 | } |
| 89 | |
| 90 | @SmallTest |
| 91 | public void testResultCallbackOnResult_runTwice_throwsException() throws Exception { |
| 92 | final int sequence = 100; |
| 93 | final NetworkRecommendationProvider.ResultCallback callback = |
| 94 | new NetworkRecommendationProvider.ResultCallback(mMockRemoteCallback, sequence); |
| 95 | |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 96 | final RecommendationResult result = RecommendationResult.createDoNotConnectRecommendation(); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 97 | callback.onResult(result); |
| 98 | |
| 99 | try { |
| 100 | callback.onResult(result); |
| 101 | fail("Callback ran more than once."); |
| 102 | } catch (IllegalStateException e) { |
| 103 | // expected |
| 104 | } |
| 105 | } |
| 106 | |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 107 | @MediumTest |
| 108 | public void testScoreRequestReceived() throws Exception { |
| 109 | mStub.requestScores(mTestNetworkKeys); |
| 110 | |
| 111 | // wait for onRequestScores() to be called in our impl below. |
| 112 | mScoreRequestLatch.await(200, TimeUnit.MILLISECONDS); |
| 113 | |
| 114 | assertSame(mTestNetworkKeys, mRecProvider.mCapturedNetworks); |
| 115 | } |
| 116 | |
| 117 | @MediumTest |
| 118 | public void testScoreRequest_nullInput() throws Exception { |
| 119 | mStub.requestScores(null); |
| 120 | |
| 121 | // onRequestScores() should never be called |
| 122 | assertFalse(mScoreRequestLatch.await(200, TimeUnit.MILLISECONDS)); |
| 123 | } |
| 124 | |
| 125 | @MediumTest |
| 126 | public void testScoreRequest_emptyInput() throws Exception { |
| 127 | mStub.requestScores(new NetworkKey[0]); |
| 128 | |
| 129 | // onRequestScores() should never be called |
| 130 | assertFalse(mScoreRequestLatch.await(200, TimeUnit.MILLISECONDS)); |
| 131 | } |
| 132 | |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 133 | private static class NetworkRecProvider extends NetworkRecommendationProvider { |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 134 | private final CountDownLatch mRecRequestLatch; |
| 135 | private final CountDownLatch mScoreRequestLatch; |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 136 | RecommendationRequest mCapturedRequest; |
| 137 | ResultCallback mCapturedCallback; |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 138 | NetworkKey[] mCapturedNetworks; |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 139 | |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 140 | NetworkRecProvider(Handler handler, CountDownLatch recRequestLatch, |
| 141 | CountDownLatch networkRequestLatch) { |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 142 | super(handler); |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 143 | mRecRequestLatch = recRequestLatch; |
| 144 | mScoreRequestLatch = networkRequestLatch; |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void onRequestRecommendation(RecommendationRequest request, |
| 149 | ResultCallback callback) { |
| 150 | mCapturedRequest = request; |
| 151 | mCapturedCallback = callback; |
Jeremy Joslin | e7f273d | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 152 | mRecRequestLatch.countDown(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onRequestScores(NetworkKey[] networks) { |
| 157 | mCapturedNetworks = networks; |
| 158 | mScoreRequestLatch.countDown(); |
Jeremy Joslin | 280f82c | 2016-12-08 10:24:02 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |