Handle fenced frames that have not been presented.

Change-Id: I9690199076c3a031bd278559e4f6196364b2b25f
diff --git a/src/android/support/test/jank/JankResult.java b/src/android/support/test/jank/JankResult.java
index a5a1eb4..71e595c 100644
--- a/src/android/support/test/jank/JankResult.java
+++ b/src/android/support/test/jank/JankResult.java
@@ -16,6 +16,7 @@
 
 package android.support.test.jank;
 
+import android.util.Log;
 import android.view.FrameStats;
 
 /** A {@link JankResult} contains the results of a jank monitoring session. This includes the number
@@ -23,6 +24,8 @@
  * as the nomalized longest frame time.*/
 public class JankResult {
 
+    private static final String TAG = JankResult.class.getSimpleName();
+
     // Maximum normalized error in frame duration before the frame is considered janky
     private static final double MAX_ERROR = 0.5f;
     // Maximum normalized frame duration before the frame is considered a pause
@@ -51,7 +54,13 @@
         long totalDuration = 0;
         // Skip first frame
         for (int i = 2; i < frameCount; i++) {
-            // TODO: Handle fenced frames that have not been presented. Either skip or throw.
+            // Handle frames that have not been presented.
+            if (stats.getFramePresentedTimeNano(i) == -1) {
+                // The animation must not have completed. Warn and break out of the loop.
+                Log.w(TAG, "Skipping fenced frame.");
+                frameCount = i;
+                break;
+            }
             long frameDuration = stats.getFramePresentedTimeNano(i) -
                     stats.getFramePresentedTimeNano(i - 1);
             double normalized = (double)frameDuration / refreshPeriod;