1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.webkit;
17
18import android.test.AndroidTestCase;
19
20public class ZoomManagerTest extends AndroidTestCase {
21
22    private ZoomManager zoomManager;
23
24    @Override
25    public void setUp() {
26        WebView webView = new WebView(this.getContext());
27        WebViewClassic webViewClassic = WebViewClassic.fromWebView(webView);
28        CallbackProxy callbackProxy = new CallbackProxy(this.getContext(), webViewClassic);
29        zoomManager = new ZoomManager(webViewClassic, callbackProxy);
30
31        zoomManager.init(1.00f);
32    }
33
34    public void testInit() {
35        testInit(0.01f);
36        testInit(1.00f);
37        testInit(1.25f);
38    }
39
40    private void testInit(float density) {
41        zoomManager.init(density);
42        actualScaleTest(density);
43        defaultScaleTest(density);
44        assertEquals(zoomManager.getDefaultMaxZoomScale(), zoomManager.getMaxZoomScale());
45        assertEquals(zoomManager.getDefaultMinZoomScale(), zoomManager.getMinZoomScale());
46        assertEquals(density, zoomManager.getTextWrapScale());
47    }
48
49    public void testUpdateDefaultZoomDensity() {
50        // test the basic case where the actual values are equal to the defaults
51        testUpdateDefaultZoomDensity(0.01f);
52        testUpdateDefaultZoomDensity(1.00f);
53        testUpdateDefaultZoomDensity(1.25f);
54    }
55
56    private void testUpdateDefaultZoomDensity(float density) {
57        zoomManager.updateDefaultZoomDensity(density);
58        defaultScaleTest(density);
59    }
60
61    public void testUpdateDefaultZoomDensityWithSmallMinZoom() {
62        // test the case where the minZoomScale has changed to be < the default
63        float newDefaultScale = 1.50f;
64        float minZoomScale = ZoomManager.DEFAULT_MIN_ZOOM_SCALE_FACTOR * newDefaultScale;
65        WebViewCore.ViewState minViewState = new WebViewCore.ViewState();
66        minViewState.mMinScale = minZoomScale - 0.1f;
67        zoomManager.updateZoomRange(minViewState, 0, 0);
68        zoomManager.updateDefaultZoomDensity(newDefaultScale);
69        defaultScaleTest(newDefaultScale);
70    }
71
72    public void testUpdateDefaultZoomDensityWithLargeMinZoom() {
73        // test the case where the minZoomScale has changed to be > the default
74        float newDefaultScale = 1.50f;
75        float minZoomScale = ZoomManager.DEFAULT_MIN_ZOOM_SCALE_FACTOR * newDefaultScale;
76        WebViewCore.ViewState minViewState = new WebViewCore.ViewState();
77        minViewState.mMinScale = minZoomScale + 0.1f;
78        zoomManager.updateZoomRange(minViewState, 0, 0);
79        zoomManager.updateDefaultZoomDensity(newDefaultScale);
80        defaultScaleTest(newDefaultScale);
81    }
82
83    public void testUpdateDefaultZoomDensityWithSmallMaxZoom() {
84        // test the case where the maxZoomScale has changed to be < the default
85        float newDefaultScale = 1.50f;
86        float maxZoomScale = ZoomManager.DEFAULT_MAX_ZOOM_SCALE_FACTOR * newDefaultScale;
87        WebViewCore.ViewState maxViewState = new WebViewCore.ViewState();
88        maxViewState.mMaxScale = maxZoomScale - 0.1f;
89        zoomManager.updateZoomRange(maxViewState, 0, 0);
90        zoomManager.updateDefaultZoomDensity(newDefaultScale);
91        defaultScaleTest(newDefaultScale);
92    }
93
94    public void testUpdateDefaultZoomDensityWithLargeMaxZoom() {
95        // test the case where the maxZoomScale has changed to be > the default
96        float newDefaultScale = 1.50f;
97        float maxZoomScale = ZoomManager.DEFAULT_MAX_ZOOM_SCALE_FACTOR * newDefaultScale;
98        WebViewCore.ViewState maxViewState = new WebViewCore.ViewState();
99        maxViewState.mMaxScale = maxZoomScale + 0.1f;
100        zoomManager.updateZoomRange(maxViewState, 0, 0);
101        zoomManager.updateDefaultZoomDensity(newDefaultScale);
102        defaultScaleTest(newDefaultScale);
103    }
104
105    public void testComputeScaleWithLimits() {
106        final float maxScale = zoomManager.getMaxZoomScale();
107        final float minScale = zoomManager.getMinZoomScale();
108        assertTrue(maxScale > minScale);
109        assertEquals(maxScale, zoomManager.computeScaleWithLimits(maxScale));
110        assertEquals(maxScale, zoomManager.computeScaleWithLimits(maxScale + .01f));
111        assertEquals(minScale, zoomManager.computeScaleWithLimits(minScale));
112        assertEquals(minScale, zoomManager.computeScaleWithLimits(minScale - .01f));
113    }
114
115    private void actualScaleTest(float actualScale) {
116        assertEquals(actualScale, zoomManager.getScale());
117        assertEquals(1 / actualScale, zoomManager.getInvScale());
118    }
119
120    private void defaultScaleTest(float defaultScale) {
121        final float maxDefault = ZoomManager.DEFAULT_MAX_ZOOM_SCALE_FACTOR * defaultScale;
122        final float minDefault = ZoomManager.DEFAULT_MIN_ZOOM_SCALE_FACTOR * defaultScale;
123        assertEquals(defaultScale, zoomManager.getDefaultScale());
124        assertEquals(1 / defaultScale, zoomManager.getInvDefaultScale());
125        assertEquals(maxDefault, zoomManager.getDefaultMaxZoomScale());
126        assertEquals(minDefault, zoomManager.getDefaultMinZoomScale());
127    }
128}
129