1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | package org.chromium.android_webview.test; |
6 | |
7 | import android.view.View; |
8 | import android.view.View.MeasureSpec; |
9 | import android.test.InstrumentationTestCase; |
10 | import android.test.suitebuilder.annotation.SmallTest; |
11 | |
12 | import org.chromium.android_webview.AwLayoutSizer; |
13 | |
14 | public class AwLayoutSizerTest extends InstrumentationTestCase { |
15 | static class LayoutSizerDelegate implements AwLayoutSizer.Delegate { |
16 | public int requestLayoutCallCount; |
17 | public boolean setMeasuredDimensionCalled; |
18 | public int measuredWidth; |
19 | public int measuredHeight; |
20 | |
21 | @Override |
22 | public void requestLayout() { |
23 | requestLayoutCallCount++; |
24 | } |
25 | |
26 | @Override |
27 | public void setMeasuredDimension(int measuredWidth, int measuredHeight) { |
28 | setMeasuredDimensionCalled = true; |
29 | this.measuredWidth = measuredWidth; |
30 | this.measuredHeight = measuredHeight; |
31 | } |
32 | } |
33 | |
34 | private static final int FIRST_CONTENT_WIDTH = 101; |
35 | private static final int FIRST_CONTENT_HEIGHT = 389; |
36 | private static final int SECOND_CONTENT_WIDTH = 103; |
37 | private static final int SECOND_CONTENT_HEIGHT = 397; |
38 | |
39 | private static final int SMALLER_CONTENT_SIZE = 25; |
40 | private static final int AT_MOST_MEASURE_SIZE = 50; |
41 | private static final int TOO_LARGE_CONTENT_SIZE = 100; |
42 | |
43 | private static final double INITIAL_PAGE_SCALE = 1.0; |
44 | private static final double DIP_SCALE = 1.0; |
45 | |
46 | public void testCanQueryContentSize() { |
47 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
48 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
49 | layoutSizer.setDelegate(delegate); |
50 | layoutSizer.setDIPScale(DIP_SCALE); |
51 | |
52 | final int contentWidth = 101; |
53 | final int contentHeight = 389; |
54 | |
55 | layoutSizer.onContentSizeChanged(contentWidth, contentHeight); |
56 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
57 | layoutSizer.onMeasure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), |
58 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
59 | |
60 | assertTrue(delegate.setMeasuredDimensionCalled); |
61 | assertEquals(contentWidth, delegate.measuredWidth & View.MEASURED_SIZE_MASK); |
62 | assertEquals(contentHeight, delegate.measuredHeight & View.MEASURED_SIZE_MASK); |
63 | } |
64 | |
65 | public void testContentSizeChangeRequestsLayout() { |
66 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
67 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
68 | layoutSizer.setDelegate(delegate); |
69 | layoutSizer.setDIPScale(DIP_SCALE); |
70 | |
71 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
72 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
73 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
74 | layoutSizer.onContentSizeChanged(SECOND_CONTENT_WIDTH, SECOND_CONTENT_WIDTH); |
75 | |
76 | assertEquals(requestLayoutCallCount + 1, delegate.requestLayoutCallCount); |
77 | } |
78 | |
79 | public void testContentSizeChangeDoesNotRequestLayoutIfMeasuredExcatly() { |
80 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
81 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
82 | layoutSizer.setDelegate(delegate); |
83 | layoutSizer.setDIPScale(DIP_SCALE); |
84 | |
85 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
86 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
87 | layoutSizer.onMeasure(MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY), |
88 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
89 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
90 | layoutSizer.onContentSizeChanged(SECOND_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
91 | |
92 | assertEquals(requestLayoutCallCount, delegate.requestLayoutCallCount); |
93 | } |
94 | |
95 | public void testDuplicateContentSizeChangeDoesNotRequestLayout() { |
96 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
97 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
98 | layoutSizer.setDelegate(delegate); |
99 | layoutSizer.setDIPScale(DIP_SCALE); |
100 | |
101 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
102 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
103 | layoutSizer.onMeasure(MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY), |
104 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
105 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
106 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
107 | |
108 | assertEquals(requestLayoutCallCount, delegate.requestLayoutCallCount); |
109 | } |
110 | |
111 | public void testContentHeightGrowsTillAtMostSize() { |
112 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
113 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
114 | layoutSizer.setDelegate(delegate); |
115 | layoutSizer.setDIPScale(DIP_SCALE); |
116 | |
117 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
118 | layoutSizer.onContentSizeChanged(SMALLER_CONTENT_SIZE, SMALLER_CONTENT_SIZE); |
119 | layoutSizer.onMeasure( |
120 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST), |
121 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST)); |
122 | assertEquals(AT_MOST_MEASURE_SIZE, delegate.measuredWidth); |
123 | assertEquals(SMALLER_CONTENT_SIZE, delegate.measuredHeight); |
124 | |
125 | layoutSizer.onContentSizeChanged(TOO_LARGE_CONTENT_SIZE, TOO_LARGE_CONTENT_SIZE); |
126 | layoutSizer.onMeasure( |
127 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST), |
128 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST)); |
129 | assertEquals(AT_MOST_MEASURE_SIZE, delegate.measuredWidth & View.MEASURED_SIZE_MASK); |
130 | assertEquals(AT_MOST_MEASURE_SIZE, delegate.measuredHeight & View.MEASURED_SIZE_MASK); |
131 | } |
132 | |
133 | public void testScaleChangeRequestsLayout() { |
134 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
135 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
136 | layoutSizer.setDelegate(delegate); |
137 | layoutSizer.setDIPScale(DIP_SCALE); |
138 | |
139 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
140 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
141 | layoutSizer.onMeasure( |
142 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.EXACTLY), |
143 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
144 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
145 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE + 0.5); |
146 | |
147 | assertEquals(requestLayoutCallCount + 1, delegate.requestLayoutCallCount); |
148 | } |
149 | |
150 | public void testDuplicateScaleChangeDoesNotRequestLayout() { |
151 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
152 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
153 | layoutSizer.setDelegate(delegate); |
154 | layoutSizer.setDIPScale(DIP_SCALE); |
155 | |
156 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
157 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
158 | layoutSizer.onMeasure(MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY), |
159 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
160 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
161 | layoutSizer.onPageScaleChanged(DIP_SCALE); |
162 | |
163 | assertEquals(requestLayoutCallCount, delegate.requestLayoutCallCount); |
164 | } |
165 | |
166 | public void testScaleChangeGrowsTillAtMostSize() { |
167 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
168 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
169 | layoutSizer.setDelegate(delegate); |
170 | layoutSizer.setDIPScale(DIP_SCALE); |
171 | |
172 | final double tooLargePageScale = 3.00; |
173 | |
174 | layoutSizer.onContentSizeChanged(SMALLER_CONTENT_SIZE, SMALLER_CONTENT_SIZE); |
175 | layoutSizer.onPageScaleChanged(INITIAL_PAGE_SCALE); |
176 | layoutSizer.onMeasure( |
177 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST), |
178 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST)); |
179 | assertEquals(AT_MOST_MEASURE_SIZE, delegate.measuredWidth); |
180 | assertEquals(SMALLER_CONTENT_SIZE, delegate.measuredHeight); |
181 | |
182 | layoutSizer.onPageScaleChanged(tooLargePageScale); |
183 | layoutSizer.onMeasure( |
184 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST), |
185 | MeasureSpec.makeMeasureSpec(AT_MOST_MEASURE_SIZE, MeasureSpec.AT_MOST)); |
186 | assertEquals(AT_MOST_MEASURE_SIZE, delegate.measuredWidth & View.MEASURED_SIZE_MASK); |
187 | assertEquals(AT_MOST_MEASURE_SIZE, delegate.measuredHeight & View.MEASURED_SIZE_MASK); |
188 | } |
189 | |
190 | public void testFreezeAndUnfreezeDoesntCauseLayout() { |
191 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
192 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
193 | layoutSizer.setDelegate(delegate); |
194 | layoutSizer.setDIPScale(DIP_SCALE); |
195 | |
196 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
197 | layoutSizer.freezeLayoutRequests(); |
198 | layoutSizer.unfreezeLayoutRequests(); |
199 | assertEquals(requestLayoutCallCount, delegate.requestLayoutCallCount); |
200 | } |
201 | |
202 | public void testFreezeInhibitsLayoutRequest() { |
203 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
204 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
205 | layoutSizer.setDelegate(delegate); |
206 | layoutSizer.setDIPScale(DIP_SCALE); |
207 | |
208 | layoutSizer.freezeLayoutRequests(); |
209 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
210 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
211 | layoutSizer.onContentSizeChanged(SECOND_CONTENT_WIDTH, SECOND_CONTENT_WIDTH); |
212 | assertEquals(requestLayoutCallCount, delegate.requestLayoutCallCount); |
213 | } |
214 | |
215 | public void testUnfreezeIssuesLayoutRequest() { |
216 | AwLayoutSizer layoutSizer = new AwLayoutSizer(); |
217 | LayoutSizerDelegate delegate = new LayoutSizerDelegate(); |
218 | layoutSizer.setDelegate(delegate); |
219 | layoutSizer.setDIPScale(DIP_SCALE); |
220 | |
221 | layoutSizer.freezeLayoutRequests(); |
222 | layoutSizer.onContentSizeChanged(FIRST_CONTENT_WIDTH, FIRST_CONTENT_HEIGHT); |
223 | final int requestLayoutCallCount = delegate.requestLayoutCallCount; |
224 | layoutSizer.onContentSizeChanged(SECOND_CONTENT_WIDTH, SECOND_CONTENT_WIDTH); |
225 | assertEquals(requestLayoutCallCount, delegate.requestLayoutCallCount); |
226 | layoutSizer.unfreezeLayoutRequests(); |
227 | assertEquals(requestLayoutCallCount + 1, delegate.requestLayoutCallCount); |
228 | } |
229 | } |