1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.net; 6 7 import static org.junit.Assert.assertNotNull; 8 import static org.junit.Assert.assertNull; 9 10 import android.net.http.RequestFinishedInfo; 11 import android.os.ConditionVariable; 12 13 import java.util.concurrent.Executor; 14 import java.util.concurrent.Executors; 15 16 /** 17 * RequestFinishedInfo.Listener for testing, which saves the RequestFinishedInfo 18 */ 19 public class TestRequestFinishedListener extends RequestFinishedInfo.Listener { 20 private final ConditionVariable mBlock; 21 private RequestFinishedInfo mRequestInfo; 22 23 // TODO(mgersh): it's weird that you can use either this constructor or blockUntilDone() but 24 // not both. Either clean it up or document why it has to work this way. TestRequestFinishedListener(Executor executor)25 public TestRequestFinishedListener(Executor executor) { 26 super(executor); 27 mBlock = new ConditionVariable(); 28 } 29 TestRequestFinishedListener()30 public TestRequestFinishedListener() { 31 super(Executors.newSingleThreadExecutor()); 32 mBlock = new ConditionVariable(); 33 } 34 getRequestInfo()35 public RequestFinishedInfo getRequestInfo() { 36 return mRequestInfo; 37 } 38 39 @Override onRequestFinished(RequestFinishedInfo requestInfo)40 public void onRequestFinished(RequestFinishedInfo requestInfo) { 41 assertNull("onRequestFinished called repeatedly", mRequestInfo); 42 assertNotNull(requestInfo); 43 mRequestInfo = requestInfo; 44 mBlock.open(); 45 } 46 blockUntilDone()47 public void blockUntilDone() { 48 mBlock.block(); 49 } 50 reset()51 public void reset() { 52 mBlock.close(); 53 mRequestInfo = null; 54 } 55 } 56