Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: talk/app/webrtc/androidtests/src/org/webrtc/SurfaceViewRendererOnMeasureTest.java

Issue 1379793003: Android SurfaceViewRenderer: Add tests for onMeasure() (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Change I420Frame constructors to package private Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 package org.webrtc;
29
30 import android.graphics.Point;
31 import android.opengl.EGL14;
32 import android.test.ActivityTestCase;
33 import android.test.suitebuilder.annotation.MediumTest;
34 import android.view.View.MeasureSpec;
35
36 import java.nio.ByteBuffer;
37 import java.util.Arrays;
38 import java.util.List;
39
40 public final class SurfaceViewRendererOnMeasureTest extends ActivityTestCase {
41 /**
42 * List with all possible scaling types.
43 */
44 private static final List<RendererCommon.ScalingType> scalingTypes = Arrays.as List(
45 RendererCommon.ScalingType.SCALE_ASPECT_FIT,
46 RendererCommon.ScalingType.SCALE_ASPECT_FILL,
47 RendererCommon.ScalingType.SCALE_ASPECT_BALANCED);
48
49 /**
50 * List with MeasureSpec modes.
51 */
52 private static final List<Integer> measureSpecModes =
53 Arrays.asList(MeasureSpec.EXACTLY, MeasureSpec.AT_MOST);
54
55 /**
56 * Returns a dummy YUV frame.
57 */
58 static VideoRenderer.I420Frame createFrame(int width, int height, int rotation Degree) {
59 final int[] yuvStrides = new int[] {width, (width + 1) / 2, (width + 1) / 2} ;
60 final int[] yuvHeights = new int[] {height, (height + 1) / 2, (height + 1) / 2};
61 final ByteBuffer[] yuvPlanes = new ByteBuffer[3];
62 for (int i = 0; i < 3; ++i) {
63 yuvPlanes[i] = ByteBuffer.allocateDirect(yuvStrides[i] * yuvHeights[i]);
64 }
65 return new VideoRenderer.I420Frame(width, height, rotationDegree, yuvStrides , yuvPlanes, 0);
66 }
67
68 /**
69 * Assert onMeasure() with given parameters will result in expected measured s ize.
70 */
71 private static void assertMeasuredSize(
72 SurfaceViewRenderer surfaceViewRenderer, RendererCommon.ScalingType scalin gType,
73 String frameDimensions,
74 int expectedWidth, int expectedHeight,
75 int widthSpec, int heightSpec) {
76 surfaceViewRenderer.setScalingType(scalingType);
77 surfaceViewRenderer.onMeasure(widthSpec, heightSpec);
78 final int measuredWidth = surfaceViewRenderer.getMeasuredWidth();
79 final int measuredHeight = surfaceViewRenderer.getMeasuredHeight();
80 if (measuredWidth != expectedWidth || measuredHeight != expectedHeight) {
81 fail("onMeasure("
82 + MeasureSpec.toString(widthSpec) + ", " + MeasureSpec.toString(height Spec) + ")"
83 + " with scaling type " + scalingType
84 + " and frame: " + frameDimensions
85 + " expected measured size " + expectedWidth + "x" + expectedHeight
86 + ", but was " + measuredWidth + "x" + measuredHeight);
87 }
88 }
89
90 /**
91 * Test how SurfaceViewRenderer.onMeasure() behaves when no frame has been del ivered.
92 */
93 @MediumTest
94 public void testNoFrame() {
95 final SurfaceViewRenderer surfaceViewRenderer =
96 new SurfaceViewRenderer(getInstrumentation().getContext());
97 final String frameDimensions = "null";
98
99 // Test behaviour before SurfaceViewRenderer.init() is called.
100 for (RendererCommon.ScalingType scalingType : scalingTypes) {
101 for (int measureSpecMode : measureSpecModes) {
102 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpecMo de);
103 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
104 0, 0, zeroMeasureSize, zeroMeasureSize);
105 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
106 1280, 720,
107 MeasureSpec.makeMeasureSpec(1280, measureSpecMode),
108 MeasureSpec.makeMeasureSpec(720, measureSpecMode));
109 }
110 }
111
112 // Test behaviour after SurfaceViewRenderer.init() is called, but still no fr ame.
113 surfaceViewRenderer.init(EGL14.EGL_NO_CONTEXT, null);
114 for (RendererCommon.ScalingType scalingType : scalingTypes) {
115 for (int measureSpecMode : measureSpecModes) {
116 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpecMo de);
117 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
118 0, 0, zeroMeasureSize, zeroMeasureSize);
119 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
120 1280, 720,
121 MeasureSpec.makeMeasureSpec(1280, measureSpecMode),
122 MeasureSpec.makeMeasureSpec(720, measureSpecMode));
123 }
124 }
125
126 surfaceViewRenderer.release();
127 }
128
129 /**
130 * Test how SurfaceViewRenderer.onMeasure() behaves with a 1280x720 frame.
131 */
132 @MediumTest
133 public void testFrame1280x720() {
134 final SurfaceViewRenderer surfaceViewRenderer =
135 new SurfaceViewRenderer(getInstrumentation().getContext());
136 surfaceViewRenderer.init(EGL14.EGL_NO_CONTEXT, null);
137
138 // Test different rotation degress, but same rotated size.
139 for (int rotationDegree : new int[] {0, 90, 180, 270}) {
140 final int rotatedWidth = 1280;
141 final int rotatedHeight = 720;
142 final int unrotatedWidth = (rotationDegree % 180 == 0 ? rotatedWidth : rot atedHeight);
143 final int unrotatedHeight = (rotationDegree % 180 == 0 ? rotatedHeight : r otatedWidth);
144 final VideoRenderer.I420Frame frame =
145 createFrame(unrotatedWidth, unrotatedHeight, rotationDegree);
146 assertEquals(rotatedWidth, frame.rotatedWidth());
147 assertEquals(rotatedHeight, frame.rotatedHeight());
148 final String frameDimensions =
149 unrotatedWidth + "x" + unrotatedHeight + " with rotation " + rotationD egree;
150 surfaceViewRenderer.renderFrame(frame);
151
152 // Test forcing to zero size.
153 for (RendererCommon.ScalingType scalingType : scalingTypes) {
154 for (int measureSpecMode : measureSpecModes) {
155 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpec Mode);
156 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
157 0, 0, zeroMeasureSize, zeroMeasureSize);
158 }
159 }
160
161 // Test perfect fit.
162 for (RendererCommon.ScalingType scalingType : scalingTypes) {
163 for (int measureSpecMode : measureSpecModes) {
164 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
165 rotatedWidth, rotatedHeight,
166 MeasureSpec.makeMeasureSpec(rotatedWidth, measureSpecMode),
167 MeasureSpec.makeMeasureSpec(rotatedHeight, measureSpecMode));
168 }
169 }
170
171 // Force spec size with different aspect ratio than frame aspect ratio.
172 for (RendererCommon.ScalingType scalingType : scalingTypes) {
173 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
174 720, 1280,
175 MeasureSpec.makeMeasureSpec(720, MeasureSpec.EXACTLY),
176 MeasureSpec.makeMeasureSpec(1280, MeasureSpec.EXACTLY));
177 }
178
179 final float videoAspectRatio = (float) rotatedWidth / rotatedHeight;
180 {
181 // Relax both width and height constraints.
182 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.AT_MO ST);
183 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.AT_ MOST);
184 for (RendererCommon.ScalingType scalingType : scalingTypes) {
185 final Point expectedSize =
186 RendererCommon.getDisplaySize(scalingType, videoAspectRatio, 720, 1280);
187 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
188 expectedSize.x, expectedSize.y, widthSpec, heightSpec);
189 }
190 }
191 {
192 // Force width to 720, but relax height constraint. This will give the s ame result as
193 // above, because width is already the limiting factor and will be maxed out.
194 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.EXACT LY);
195 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.AT_ MOST);
196 for (RendererCommon.ScalingType scalingType : scalingTypes) {
197 final Point expectedSize =
198 RendererCommon.getDisplaySize(scalingType, videoAspectRatio, 720, 1280);
199 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
200 expectedSize.x, expectedSize.y, widthSpec, heightSpec);
201 }
202 }
203 {
204 // Force height, but relax width constraint. This will force a bad layou t size.
205 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.AT_MO ST);
206 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.EXA CTLY);
207 for (RendererCommon.ScalingType scalingType : scalingTypes) {
208 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
209 720, 1280, widthSpec, heightSpec);
210 }
211 }
212 }
213
214 surfaceViewRenderer.release();
215 }
216 }
OLDNEW
« no previous file with comments | « no previous file | talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698