Index: components/cronet/android/test/javatests/src/org/chromium/net/MetricsTestUtil.java |
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/MetricsTestUtil.java b/components/cronet/android/test/javatests/src/org/chromium/net/MetricsTestUtil.java |
index 2d2a8d12d99bb91110fcb54c0ffed9a9e9cdea43..e605357d534aaf9751228f2e43b072f916526d66 100644 |
--- a/components/cronet/android/test/javatests/src/org/chromium/net/MetricsTestUtil.java |
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/MetricsTestUtil.java |
@@ -6,9 +6,11 @@ package org.chromium.net; |
import static junit.framework.Assert.assertNotNull; |
import static junit.framework.Assert.assertNull; |
+import static junit.framework.Assert.assertTrue; |
import android.os.ConditionVariable; |
+import java.util.Date; |
import java.util.LinkedList; |
import java.util.NoSuchElementException; |
import java.util.concurrent.Executor; |
@@ -20,8 +22,8 @@ import java.util.concurrent.Executors; |
*/ |
public class MetricsTestUtil { |
/** |
- * Executor which runs tasks only when told to with runAllTasks(). |
- */ |
+ * Executor which runs tasks only when told to with runAllTasks(). |
+ */ |
public static class TestExecutor implements Executor { |
private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>(); |
@@ -49,6 +51,8 @@ public class MetricsTestUtil { |
private ConditionVariable mBlock; |
private int mNumExpectedRequests = -1; |
+ // TODO(mgersh): it's weird that you can use either this constructor or blockUntilDone() but |
+ // not both. Either clean it up or document why it has to work this way. |
public TestRequestFinishedListener(Executor executor) { |
super(executor); |
} |
@@ -83,5 +87,73 @@ public class MetricsTestUtil { |
public void blockUntilDone() { |
mBlock.block(); |
} |
+ |
+ public void reset() { |
+ mBlock.close(); |
+ mNumExpectedRequests = 1; |
+ mRequestInfo = null; |
+ } |
+ } |
+ |
+ /** |
+ * Check existence of all the timing metrics that apply to most test requests, |
+ * except those that come from net::LoadTimingInfo::ConnectTiming. |
+ * Also check some timing differences, focusing on things we can't check with asserts in the |
+ * CronetMetrics constructor. |
+ * Don't check push times here. |
+ */ |
+ public static void checkTimingMetrics( |
+ RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime) { |
+ assertNotNull(metrics.getRequestStart()); |
+ assertTrue(metrics.getRequestStart().after(startTime) |
+ || metrics.getRequestStart().equals(startTime)); |
+ assertNotNull(metrics.getSendingStart()); |
+ assertTrue(metrics.getSendingStart().after(startTime)); |
+ assertNotNull(metrics.getSendingEnd()); |
+ assertTrue(metrics.getSendingEnd().before(endTime)); |
+ assertNotNull(metrics.getResponseStart()); |
+ assertTrue(metrics.getResponseStart().after(startTime)); |
+ assertNotNull(metrics.getResponseEnd()); |
+ assertTrue(metrics.getResponseEnd().before(endTime) |
+ || metrics.getResponseEnd().equals(endTime)); |
+ // Entire request should take more than 0 ms |
+ assertTrue(metrics.getResponseEnd().getTime() - metrics.getRequestStart().getTime() > 0); |
+ } |
+ |
+ /** |
+ * Check that the timing metrics which come from net::LoadTimingInfo::ConnectTiming exist, |
+ * except SSL times in the case of non-https requests. |
+ */ |
+ public static void checkHasConnectTiming( |
+ RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, boolean isSsl) { |
+ assertNotNull(metrics.getDnsStart()); |
+ assertTrue(metrics.getDnsStart().after(startTime)); |
+ assertNotNull(metrics.getDnsEnd()); |
+ assertTrue(metrics.getDnsEnd().before(endTime)); |
+ assertNotNull(metrics.getConnectStart()); |
+ assertTrue(metrics.getConnectStart().after(startTime)); |
+ assertNotNull(metrics.getConnectEnd()); |
+ assertTrue(metrics.getConnectEnd().before(endTime)); |
+ if (isSsl) { |
+ assertNotNull(metrics.getSslStart()); |
+ assertTrue(metrics.getSslStart().after(startTime)); |
+ assertNotNull(metrics.getSslEnd()); |
+ assertTrue(metrics.getSslEnd().before(endTime)); |
+ } else { |
+ assertNull(metrics.getSslStart()); |
+ assertNull(metrics.getSslEnd()); |
+ } |
+ } |
+ |
+ /** |
+ * Check that the timing metrics from net::LoadTimingInfo::ConnectTiming don't exist. |
+ */ |
+ public static void checkNoConnectTiming(RequestFinishedInfo.Metrics metrics) { |
+ assertNull(metrics.getDnsStart()); |
+ assertNull(metrics.getDnsEnd()); |
+ assertNull(metrics.getSslStart()); |
+ assertNull(metrics.getSslEnd()); |
+ assertNull(metrics.getConnectStart()); |
+ assertNull(metrics.getConnectEnd()); |
} |
} |