| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
| 8 import android.support.test.filters.MediumTest; | 9 import android.support.test.filters.MediumTest; |
| 9 import android.test.InstrumentationTestCase; | |
| 10 import android.view.WindowManager; | 10 import android.view.WindowManager; |
| 11 | 11 |
| 12 import org.junit.Assert; |
| 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; |
| 15 |
| 12 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
| 17 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 13 import org.chromium.base.test.util.DisabledTest; | 18 import org.chromium.base.test.util.DisabledTest; |
| 14 import org.chromium.ui.VSyncMonitor; | 19 import org.chromium.ui.VSyncMonitor; |
| 15 | 20 |
| 16 import java.util.Arrays; | 21 import java.util.Arrays; |
| 17 import java.util.concurrent.Callable; | 22 import java.util.concurrent.Callable; |
| 18 | 23 |
| 19 /** | 24 /** |
| 20 * Tests VSyncMonitor to make sure it generates correct VSync timestamps. | 25 * Tests VSyncMonitor to make sure it generates correct VSync timestamps. |
| 21 */ | 26 */ |
| 22 public class VSyncMonitorTest extends InstrumentationTestCase { | 27 @RunWith(BaseJUnit4ClassRunner.class) |
| 28 public class VSyncMonitorTest { |
| 23 private static final int FRAME_COUNT = 60; | 29 private static final int FRAME_COUNT = 60; |
| 24 | 30 |
| 25 private static class VSyncDataCollector implements VSyncMonitor.Listener { | 31 private static class VSyncDataCollector implements VSyncMonitor.Listener { |
| 26 public long mFramePeriods[]; | 32 public long mFramePeriods[]; |
| 27 public int mFrameCount; | 33 public int mFrameCount; |
| 28 | 34 |
| 29 private boolean mDone; | 35 private boolean mDone; |
| 30 private long mPreviousVSyncTimeMicros; | 36 private long mPreviousVSyncTimeMicros; |
| 31 private final Object mSyncRoot = new Object(); | 37 private final Object mSyncRoot = new Object(); |
| 32 | 38 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 73 } |
| 68 } | 74 } |
| 69 } | 75 } |
| 70 | 76 |
| 71 // The vsync monitor must be created on the UI thread to avoid associating t
he underlying | 77 // The vsync monitor must be created on the UI thread to avoid associating t
he underlying |
| 72 // Choreographer with the Looper from the test runner thread. | 78 // Choreographer with the Looper from the test runner thread. |
| 73 private VSyncMonitor createVSyncMonitor(final VSyncMonitor.Listener listener
) { | 79 private VSyncMonitor createVSyncMonitor(final VSyncMonitor.Listener listener
) { |
| 74 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<VSyncMo
nitor>() { | 80 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<VSyncMo
nitor>() { |
| 75 @Override | 81 @Override |
| 76 public VSyncMonitor call() { | 82 public VSyncMonitor call() { |
| 77 Context context = getInstrumentation().getContext(); | 83 Context context = InstrumentationRegistry.getInstrumentation().g
etContext(); |
| 78 return new VSyncMonitor(context, listener); | 84 return new VSyncMonitor(context, listener); |
| 79 } | 85 } |
| 80 }); | 86 }); |
| 81 } | 87 } |
| 82 | 88 |
| 83 // Vsync requests should be made on the same thread as that used to create t
he VSyncMonitor (the | 89 // Vsync requests should be made on the same thread as that used to create t
he VSyncMonitor (the |
| 84 // UI thread). | 90 // UI thread). |
| 85 private void requestVSyncMonitorUpdate(final VSyncMonitor monitor) { | 91 private void requestVSyncMonitorUpdate(final VSyncMonitor monitor) { |
| 86 ThreadUtils.runOnUiThread(new Runnable() { | 92 ThreadUtils.runOnUiThread(new Runnable() { |
| 87 @Override | 93 @Override |
| 88 public void run() { | 94 public void run() { |
| 89 monitor.requestUpdate(); | 95 monitor.requestUpdate(); |
| 90 } | 96 } |
| 91 }); | 97 }); |
| 92 } | 98 } |
| 93 | 99 |
| 94 // Check that the vsync period roughly matches the timestamps that the monit
or generates. | 100 // Check that the vsync period roughly matches the timestamps that the monit
or generates. |
| 101 @Test |
| 95 @MediumTest | 102 @MediumTest |
| 96 @DisabledTest(message = "crbug.com/674129") | 103 @DisabledTest(message = "crbug.com/674129") |
| 97 public void testVSyncPeriod() throws InterruptedException { | 104 public void testVSyncPeriod() throws InterruptedException { |
| 98 // Collect roughly one second of data on a 60 fps display. | 105 // Collect roughly one second of data on a 60 fps display. |
| 99 VSyncDataCollector collector = new VSyncDataCollector(FRAME_COUNT); | 106 VSyncDataCollector collector = new VSyncDataCollector(FRAME_COUNT); |
| 100 VSyncMonitor monitor = createVSyncMonitor(collector); | 107 VSyncMonitor monitor = createVSyncMonitor(collector); |
| 101 | 108 |
| 102 long reportedFramePeriod = monitor.getVSyncPeriodInMicroseconds(); | 109 long reportedFramePeriod = monitor.getVSyncPeriodInMicroseconds(); |
| 103 assertTrue(reportedFramePeriod > 0); | 110 Assert.assertTrue(reportedFramePeriod > 0); |
| 104 | 111 |
| 105 assertFalse(collector.isDone()); | 112 Assert.assertFalse(collector.isDone()); |
| 106 requestVSyncMonitorUpdate(monitor); | 113 requestVSyncMonitorUpdate(monitor); |
| 107 collector.waitTillDone(); | 114 collector.waitTillDone(); |
| 108 assertTrue(collector.isDone()); | 115 Assert.assertTrue(collector.isDone()); |
| 109 | 116 |
| 110 // Check that the median frame rate is within 10% of the reported frame
period. | 117 // Check that the median frame rate is within 10% of the reported frame
period. |
| 111 assertTrue(collector.mFrameCount == FRAME_COUNT - 1); | 118 Assert.assertTrue(collector.mFrameCount == FRAME_COUNT - 1); |
| 112 Arrays.sort(collector.mFramePeriods, 0, collector.mFramePeriods.length); | 119 Arrays.sort(collector.mFramePeriods, 0, collector.mFramePeriods.length); |
| 113 long medianFramePeriod = collector.mFramePeriods[collector.mFramePeriods
.length / 2]; | 120 long medianFramePeriod = collector.mFramePeriods[collector.mFramePeriods
.length / 2]; |
| 114 if (Math.abs(medianFramePeriod - reportedFramePeriod) > reportedFramePer
iod * .1) { | 121 if (Math.abs(medianFramePeriod - reportedFramePeriod) > reportedFramePer
iod * .1) { |
| 115 fail("Measured median frame period " + medianFramePeriod | 122 Assert.fail("Measured median frame period " + medianFramePeriod |
| 116 + " differs by more than 10% from the reported frame period
" | 123 + " differs by more than 10% from the reported frame period
" |
| 117 + reportedFramePeriod + " for requested frames"); | 124 + reportedFramePeriod + " for requested frames"); |
| 118 } | 125 } |
| 119 | 126 |
| 120 Context context = getInstrumentation().getContext(); | 127 Context context = InstrumentationRegistry.getInstrumentation().getContex
t(); |
| 121 float refreshRate = ((WindowManager) context.getSystemService(Context.WI
NDOW_SERVICE)) | 128 float refreshRate = ((WindowManager) context.getSystemService(Context.WI
NDOW_SERVICE)) |
| 122 .getDefaultDisplay().getRefreshRate(); | 129 .getDefaultDisplay().getRefreshRate(); |
| 123 if (refreshRate < 30.0f) { | 130 if (refreshRate < 30.0f) { |
| 124 // Reported refresh rate is most likely incorrect. | 131 // Reported refresh rate is most likely incorrect. |
| 125 // Estimated vsync period is expected to be lower than (1000000 / 30
) microseconds | 132 // Estimated vsync period is expected to be lower than (1000000 / 30
) microseconds |
| 126 assertTrue(monitor.getVSyncPeriodInMicroseconds() < 1000000 / 30); | 133 Assert.assertTrue(monitor.getVSyncPeriodInMicroseconds() < 1000000 /
30); |
| 127 } | 134 } |
| 128 } | 135 } |
| 129 } | 136 } |
| OLD | NEW |