use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.
the class NetworkStatsObserversTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
MockitoAnnotations.initMocks(this);
mObserverHandlerThread = new IdleableHandlerThread("HandlerThread");
mObserverHandlerThread.start();
final Looper observerLooper = mObserverHandlerThread.getLooper();
mStatsObservers = new NetworkStatsObservers() {
@Override
protected Looper getHandlerLooperLocked() {
return observerLooper;
}
};
mCv = new ConditionVariable();
mHandler = new LatchedHandler(Looper.getMainLooper(), mCv);
mMessenger = new Messenger(mHandler);
mActiveIfaces = new ArrayMap<>();
mActiveUidIfaces = new ArrayMap<>();
}
use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.
the class NetworkStatsServiceTest method testRegisterUsageCallback.
public void testRegisterUsageCallback() throws Exception {
// pretend that wifi network comes online; service should ask about full
// network state, and poll any existing interfaces before updating.
expectCurrentTime();
expectDefaultSettings();
expectNetworkState(buildWifiState());
expectNetworkStatsSummary(buildEmptyStats());
expectNetworkStatsUidDetail(buildEmptyStats());
expectNetworkStatsPoll();
expectBandwidthControlCheck();
replay();
mService.forceUpdateIfaces();
// verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
verifyAndReset();
String callingPackage = "the.calling.package";
// very small; should be overriden by framework
long thresholdInBytes = 1L;
DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdInBytes);
// Create a messenger that waits for callback activity
ConditionVariable cv = new ConditionVariable(false);
LatchedHandler latchedHandler = new LatchedHandler(Looper.getMainLooper(), cv);
Messenger messenger = new Messenger(latchedHandler);
// Allow binder to connect
IBinder mockBinder = createMock(IBinder.class);
mockBinder.linkToDeath((IBinder.DeathRecipient) anyObject(), anyInt());
EasyMock.replay(mockBinder);
// Force poll
expectCurrentTime();
expectDefaultSettings();
expectNetworkStatsSummary(buildEmptyStats());
expectNetworkStatsUidDetail(buildEmptyStats());
expectNetworkStatsPoll();
replay();
// Register and verify request and that binder was called
DataUsageRequest request = mService.registerUsageCallback(callingPackage, inputRequest, messenger, mockBinder);
assertTrue(request.requestId > 0);
assertTrue(Objects.equals(sTemplateWifi, request.template));
// 2 MB
long minThresholdInBytes = 2 * 1024 * 1024;
assertEquals(minThresholdInBytes, request.thresholdInBytes);
// Send dummy message to make sure that any previous message has been handled
mHandler.sendMessage(mHandler.obtainMessage(-1));
mHandlerThread.waitForIdle(WAIT_TIMEOUT);
verifyAndReset();
// Make sure that the caller binder gets connected
EasyMock.verify(mockBinder);
EasyMock.reset(mockBinder);
// modify some number on wifi, and trigger poll event
// not enough traffic to call data usage callback
incrementCurrentTime(HOUR_IN_MILLIS);
expectCurrentTime();
expectDefaultSettings();
expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
expectNetworkStatsUidDetail(buildEmptyStats());
expectNetworkStatsPoll();
replay();
forcePollAndWaitForIdle();
// verify service recorded history
verifyAndReset();
assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
// make sure callback has not being called
assertEquals(INVALID_TYPE, latchedHandler.mLastMessageType);
// and bump forward again, with counters going higher. this is
// important, since it will trigger the data usage callback
incrementCurrentTime(DAY_IN_MILLIS);
expectCurrentTime();
expectDefaultSettings();
expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 4096000L, 4L, 8192000L, 8L));
expectNetworkStatsUidDetail(buildEmptyStats());
expectNetworkStatsPoll();
replay();
forcePollAndWaitForIdle();
// verify service recorded history
assertNetworkTotal(sTemplateWifi, 4096000L, 4L, 8192000L, 8L, 0);
verifyAndReset();
// Wait for the caller to ack receipt of CALLBACK_LIMIT_REACHED
assertTrue(cv.block(WAIT_TIMEOUT));
assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, latchedHandler.mLastMessageType);
cv.close();
// Allow binder to disconnect
expect(mockBinder.unlinkToDeath((IBinder.DeathRecipient) anyObject(), anyInt())).andReturn(true);
EasyMock.replay(mockBinder);
// Unregister request
mService.unregisterUsageRequest(request);
// Wait for the caller to ack receipt of CALLBACK_RELEASED
assertTrue(cv.block(WAIT_TIMEOUT));
assertEquals(NetworkStatsManager.CALLBACK_RELEASED, latchedHandler.mLastMessageType);
// Make sure that the caller binder gets disconnected
EasyMock.verify(mockBinder);
}
use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.
the class GLThreadManager method setConfigurationAndWait.
/**
* Configure the GL renderer for the given set of output surfaces, and block until
* this configuration has been applied.
*
* @param surfaces a collection of pairs of {@link android.view.Surface}s and their
* corresponding sizes to configure.
* @param collector a {@link CaptureCollector} to retrieve requests from.
*/
public void setConfigurationAndWait(Collection<Pair<Surface, Size>> surfaces, CaptureCollector collector) {
checkNotNull(collector, "collector must not be null");
Handler handler = mGLHandlerThread.getHandler();
final ConditionVariable condition = new ConditionVariable(/*closed*/
false);
ConfigureHolder configure = new ConfigureHolder(condition, surfaces, collector);
Message m = handler.obtainMessage(MSG_NEW_CONFIGURATION, /*arg1*/
0, /*arg2*/
0, configure);
handler.sendMessage(m);
// Block until configuration applied.
condition.block();
}
use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.
the class RequestThreadManager method configure.
/**
* Configure with the current list of output Surfaces.
*
* <p>
* This operation blocks until the configuration is complete.
* </p>
*
* <p>Using a {@code null} or empty {@code outputs} list is the equivalent of unconfiguring.</p>
*
* @param outputs a {@link java.util.Collection} of outputs to configure.
*/
public void configure(Collection<Pair<Surface, Size>> outputs) {
Handler handler = mRequestThread.waitAndGetHandler();
final ConditionVariable condition = new ConditionVariable(/*closed*/
false);
ConfigureHolder holder = new ConfigureHolder(condition, outputs);
handler.sendMessage(handler.obtainMessage(MSG_CONFIGURE_OUTPUTS, 0, 0, holder));
condition.block();
}
use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.
the class CameraTest method initializeMessageLooper.
/*
* Initializes the message looper so that the Camera object can
* receive the callback messages.
*/
private void initializeMessageLooper() {
final ConditionVariable startDone = new ConditionVariable();
Log.v(TAG, "start looper");
new Thread() {
@Override
public void run() {
// Set up a looper to be used by camera.
Looper.prepare();
Log.v(TAG, "start loopRun");
// Save the looper so that we can terminate this thread
// after we are done with it.
mLooper = Looper.myLooper();
mCamera = Camera.open(CAMERA_ID);
startDone.open();
// Blocks forever until Looper.quit() is called.
Looper.loop();
Log.v(TAG, "initializeMessageLooper: quit.");
}
}.start();
if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
fail("initializeMessageLooper: start timeout");
}
}
Aggregations