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 17 package android.telephony.embms.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 22 import android.telephony.MbmsDownloadSession; 23 import android.telephony.cts.embmstestapp.CtsDownloadService; 24 import android.telephony.mbms.DownloadProgressListener; 25 import android.telephony.mbms.DownloadRequest; 26 import android.telephony.mbms.DownloadStatusListener; 27 import android.telephony.mbms.FileInfo; 28 29 import java.util.concurrent.BlockingQueue; 30 import java.util.concurrent.LinkedBlockingQueue; 31 import java.util.concurrent.TimeUnit; 32 33 import org.junit.Test; 34 35 public class MbmsDownloadCallbackTest extends MbmsDownloadTestBase { 36 private static final long SHORT_TIMEOUT = 500; 37 38 private class TestDSCallback extends DownloadStatusListener { 39 private final BlockingQueue<SomeArgs> mCalls = new LinkedBlockingQueue<>(); 40 41 @Override 42 public void onStatusUpdated(DownloadRequest request, FileInfo fileInfo, 43 @MbmsDownloadSession.DownloadStatus int state) { 44 SomeArgs args = SomeArgs.obtain(); 45 args.arg1 = request; 46 args.arg2 = fileInfo; 47 args.arg3 = state; 48 mCalls.add(args); 49 } 50 51 public SomeArgs waitOnStatusUpdated(long timeout) { 52 try { 53 return mCalls.poll(timeout, TimeUnit.MILLISECONDS); 54 } catch (InterruptedException e) { 55 return null; 56 } 57 } 58 } 59 60 private class TestDPCallback extends DownloadProgressListener { 61 private final BlockingQueue<SomeArgs> mProgressUpdatedCalls = new LinkedBlockingQueue<>(); 62 63 @Override 64 public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo, int 65 currentDownloadSize, int fullDownloadSize, int currentDecodedSize, int 66 fullDecodedSize) { 67 SomeArgs args = SomeArgs.obtain(); 68 args.arg1 = request; 69 args.arg2 = fileInfo; 70 args.arg3 = currentDownloadSize; 71 args.arg4 = fullDownloadSize; 72 args.arg5 = currentDecodedSize; 73 args.arg6 = fullDecodedSize; 74 mProgressUpdatedCalls.add(args); 75 } 76 77 public SomeArgs waitOnProgressUpdated(long timeout) { 78 try { 79 return mProgressUpdatedCalls.poll(timeout, TimeUnit.MILLISECONDS); 80 } catch (InterruptedException e) { 81 return null; 82 } 83 } 84 } 85 86 @Test 87 public void testFullCallback() throws Exception { 88 int sampleInt = 10; 89 TestDSCallback statusCallback = new TestDSCallback(); 90 TestDPCallback progressCallback = new TestDPCallback(); 91 DownloadRequest request = downloadRequestTemplate.build(); 92 mDownloadSession.addStatusListener(request, mCallbackExecutor, statusCallback); 93 mDownloadSession.addProgressListener(request, mCallbackExecutor, progressCallback); 94 mMiddlewareControl.fireOnProgressUpdated(request, CtsDownloadService.FILE_INFO_1, 95 sampleInt, sampleInt, sampleInt, sampleInt); 96 SomeArgs progressArgs = progressCallback.waitOnProgressUpdated(ASYNC_TIMEOUT); 97 assertEquals(request, progressArgs.arg1); 98 assertEquals(CtsDownloadService.FILE_INFO_1, progressArgs.arg2); 99 assertEquals(sampleInt, progressArgs.arg3); 100 assertEquals(sampleInt, progressArgs.arg4); 101 assertEquals(sampleInt, progressArgs.arg5); 102 assertEquals(sampleInt, progressArgs.arg6); 103 104 mMiddlewareControl.fireOnStateUpdated(request, CtsDownloadService.FILE_INFO_1, sampleInt); 105 SomeArgs stateArgs = statusCallback.waitOnStatusUpdated(ASYNC_TIMEOUT); 106 assertEquals(request, stateArgs.arg1); 107 assertEquals(CtsDownloadService.FILE_INFO_1, stateArgs.arg2); 108 assertEquals(sampleInt, stateArgs.arg3); 109 } 110 111 @Test 112 public void testDeregistration() throws Exception { 113 TestDSCallback statusCallback = new TestDSCallback(); 114 TestDPCallback progressCallback = new TestDPCallback(); 115 DownloadRequest request = downloadRequestTemplate.build(); 116 mDownloadSession.addProgressListener(request, mCallbackExecutor, progressCallback); 117 mDownloadSession.addStatusListener(request, mCallbackExecutor, statusCallback); 118 mDownloadSession.removeProgressListener(request, progressCallback); 119 mDownloadSession.removeStatusListener(request, statusCallback); 120 121 mMiddlewareControl.fireOnStateUpdated(null, null, 0); 122 assertNull(statusCallback.waitOnStatusUpdated(SHORT_TIMEOUT)); 123 mMiddlewareControl.fireOnProgressUpdated(null, null, 0, 0, 0, 0); 124 assertNull(progressCallback.waitOnProgressUpdated(SHORT_TIMEOUT)); 125 } 126 } 127