Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / VSyncMonitorTest.java
index a0274aa..eca3f1c 100644 (file)
@@ -5,28 +5,32 @@
 package org.chromium.content.browser;
 
 import android.content.Context;
+import android.os.SystemClock;
 import android.test.InstrumentationTestCase;
 import android.test.suitebuilder.annotation.MediumTest;
+import android.view.WindowManager;
 
 import org.chromium.base.ThreadUtils;
+import org.chromium.ui.VSyncMonitor;
 
 import java.util.Arrays;
 import java.util.concurrent.Callable;
 
+/**
+ * Tests VSyncMonitor to make sure it generates correct VSync timestamps.
+ */
 public class VSyncMonitorTest extends InstrumentationTestCase {
     private static class VSyncDataCollector implements VSyncMonitor.Listener {
         public long mFramePeriods[];
         public int mFrameCount;
         public long mLastVSyncCpuTimeMillis;
 
-        private final boolean mActivelyRequestUpdate;
         private boolean mDone;
         private long mPreviousVSyncTimeMicros;
         private Object mSyncRoot = new Object();
 
-        VSyncDataCollector(int frames, boolean activelyRequestUpdate) {
-            mFramePeriods = new long[frames];
-            mActivelyRequestUpdate = activelyRequestUpdate;
+        VSyncDataCollector(int frames) {
+            mFramePeriods = new long[frames - 1];
         }
 
         public boolean isDone() {
@@ -37,10 +41,12 @@ public class VSyncMonitorTest extends InstrumentationTestCase {
 
         @Override
         public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros) {
-            mLastVSyncCpuTimeMillis = System.currentTimeMillis();
+            mLastVSyncCpuTimeMillis = SystemClock.uptimeMillis();
             if (mPreviousVSyncTimeMicros == 0) {
                 mPreviousVSyncTimeMicros = vsyncTimeMicros;
-                return;
+            } else {
+                mFramePeriods[mFrameCount++] = vsyncTimeMicros - mPreviousVSyncTimeMicros;
+                mPreviousVSyncTimeMicros = vsyncTimeMicros;
             }
             if (mFrameCount >= mFramePeriods.length) {
                 synchronized (mSyncRoot) {
@@ -49,9 +55,7 @@ public class VSyncMonitorTest extends InstrumentationTestCase {
                 }
                 return;
             }
-            mFramePeriods[mFrameCount++] = vsyncTimeMicros - mPreviousVSyncTimeMicros;
-            mPreviousVSyncTimeMicros = vsyncTimeMicros;
-            if (mActivelyRequestUpdate) monitor.requestUpdate();
+            monitor.requestUpdate();
         }
 
         public void waitTillDone() throws InterruptedException {
@@ -79,14 +83,13 @@ public class VSyncMonitorTest extends InstrumentationTestCase {
     // Check that the vsync period roughly matches the timestamps that the monitor generates.
     private void performVSyncPeriodTest(boolean enableJBVSync) throws InterruptedException {
         // Collect roughly one second of data on a 60 fps display.
-        collectAndCheckVSync(enableJBVSync, 60, true);
-        collectAndCheckVSync(enableJBVSync, VSyncMonitor.MAX_AUTO_ONVSYNC_COUNT, false);
+        collectAndCheckVSync(enableJBVSync, 60);
     }
 
     private void collectAndCheckVSync(
-            boolean enableJBVSync, final int totalFrames, final boolean activeFrames)
+            boolean enableJBVSync, final int totalFrames)
             throws InterruptedException {
-        VSyncDataCollector collector = new VSyncDataCollector(totalFrames, activeFrames);
+        VSyncDataCollector collector = new VSyncDataCollector(totalFrames);
         VSyncMonitor monitor = createVSyncMonitor(collector, enableJBVSync);
 
         long reportedFramePeriod = monitor.getVSyncPeriodInMicroseconds();
@@ -96,17 +99,26 @@ public class VSyncMonitorTest extends InstrumentationTestCase {
         monitor.requestUpdate();
         collector.waitTillDone();
         assertTrue(collector.isDone());
-        monitor.stop();
 
         // Check that the median frame rate is within 10% of the reported frame period.
-        assertTrue(collector.mFrameCount == totalFrames);
+        assertTrue(collector.mFrameCount == totalFrames - 1);
         Arrays.sort(collector.mFramePeriods, 0, collector.mFramePeriods.length);
         long medianFramePeriod = collector.mFramePeriods[collector.mFramePeriods.length / 2];
         if (Math.abs(medianFramePeriod - reportedFramePeriod) > reportedFramePeriod * .1) {
             fail("Measured median frame period " + medianFramePeriod
                     + " differs by more than 10% from the reported frame period "
-                    + reportedFramePeriod + " for "
-                    + (activeFrames ? "requested" : "automatically sent") + " frames");
+                    + reportedFramePeriod + " for requested frames");
+        }
+
+        if (enableJBVSync) {
+            Context context = getInstrumentation().getContext();
+            float refreshRate = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
+                    .getDefaultDisplay().getRefreshRate();
+            if (refreshRate < 30.0f) {
+                // Reported refresh rate is most likely incorrect.
+                // Estimated vsync period is expected to be lower than (1000000 / 30) microseconds
+                assertTrue(monitor.getVSyncPeriodInMicroseconds() < 1000000 / 30);
+            }
         }
     }
 
@@ -124,16 +136,15 @@ public class VSyncMonitorTest extends InstrumentationTestCase {
 
     // Check that the vsync period roughly matches the timestamps that the monitor generates.
     private void performVSyncActivationFromIdle(boolean enableJBVSync) throws InterruptedException {
-        VSyncDataCollector collector = new VSyncDataCollector(1, false);
+        VSyncDataCollector collector = new VSyncDataCollector(1);
         VSyncMonitor monitor = createVSyncMonitor(collector, enableJBVSync);
 
         monitor.requestUpdate();
         collector.waitTillDone();
         assertTrue(collector.isDone());
-        monitor.stop();
 
         long period = monitor.getVSyncPeriodInMicroseconds() / 1000;
-        long delay = System.currentTimeMillis() - collector.mLastVSyncCpuTimeMillis;
+        long delay = SystemClock.uptimeMillis() - collector.mLastVSyncCpuTimeMillis;
 
         // The VSync should have activated immediately instead of at the next real vsync.
         assertTrue(delay < period);