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
5package org.chromium.android_webview.test;
6
7import android.content.Context;
8import android.content.res.Configuration;
9import android.graphics.Canvas;
10import android.graphics.Rect;
11import android.view.KeyEvent;
12import android.view.MotionEvent;
13import android.view.View;
14import android.view.ViewGroup;
15import android.view.inputmethod.EditorInfo;
16import android.view.inputmethod.InputConnection;
17import android.widget.FrameLayout;
18import android.util.Log;
19
20import org.chromium.android_webview.AwContents;
21import org.chromium.content.browser.ContentViewCore;
22
23/**
24 * A View used for testing the AwContents internals.
25 *
26 * This class takes the place android.webkit.WebView would have in the production configuration.
27 */
28public class AwTestContainerView extends FrameLayout {
29    private AwContents mAwContents;
30    private AwContents.InternalAccessDelegate mInternalAccessDelegate;
31
32    public AwTestContainerView(Context context) {
33        super(context);
34        mInternalAccessDelegate = new InternalAccessAdapter();
35        setOverScrollMode(View.OVER_SCROLL_ALWAYS);
36    }
37
38    public void initialize(AwContents awContents) {
39        mAwContents = awContents;
40    }
41
42    public ContentViewCore getContentViewCore() {
43        return mAwContents.getContentViewCore();
44    }
45
46    public AwContents getAwContents() {
47        return mAwContents;
48    }
49
50    public AwContents.InternalAccessDelegate getInternalAccessDelegate() {
51        return mInternalAccessDelegate;
52    }
53
54    public void destroy() {
55        mAwContents.destroy();
56    }
57
58    @Override
59    public void onConfigurationChanged(Configuration newConfig) {
60        super.onConfigurationChanged(newConfig);
61        mAwContents.onConfigurationChanged(newConfig);
62    }
63
64    @Override
65    public void onAttachedToWindow() {
66        super.onAttachedToWindow();
67        mAwContents.onAttachedToWindow();
68    }
69
70    @Override
71    public void onDetachedFromWindow() {
72        super.onDetachedFromWindow();
73        mAwContents.onDetachedFromWindow();
74    }
75
76    @Override
77    public void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
78        super.onFocusChanged(focused, direction, previouslyFocusedRect);
79        mAwContents.onFocusChanged(focused, direction, previouslyFocusedRect);
80    }
81
82    @Override
83    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
84        return mAwContents.onCreateInputConnection(outAttrs);
85    }
86
87    @Override
88    public boolean onKeyUp(int keyCode, KeyEvent event) {
89        return mAwContents.onKeyUp(keyCode, event);
90    }
91
92    @Override
93    public boolean dispatchKeyEvent(KeyEvent event) {
94        return mAwContents.dispatchKeyEvent(event);
95    }
96
97    @Override
98    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
99        mAwContents.onMeasure(widthMeasureSpec, heightMeasureSpec);
100    }
101
102    @Override
103    public void onSizeChanged(int w, int h, int ow, int oh) {
104        super.onSizeChanged(w, h, ow, oh);
105        mAwContents.onSizeChanged(w, h, ow, oh);
106    }
107
108    @Override
109    public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
110        mAwContents.onContainerViewOverScrolled(scrollX, scrollY, clampedX, clampedY);
111    }
112
113    @Override
114    public void onScrollChanged(int l, int t, int oldl, int oldt) {
115        super.onScrollChanged(l, t, oldl, oldt);
116        if (mAwContents != null) {
117            mAwContents.onContainerViewScrollChanged(l, t, oldl, oldt);
118        }
119    }
120
121    @Override
122    public void computeScroll() {
123        mAwContents.computeScroll();
124    }
125
126    @Override
127    public void onVisibilityChanged(View changedView, int visibility) {
128        super.onVisibilityChanged(changedView, visibility);
129        mAwContents.onVisibilityChanged(changedView, visibility);
130    }
131
132    @Override
133    public void onWindowVisibilityChanged(int visibility) {
134        super.onWindowVisibilityChanged(visibility);
135        mAwContents.onWindowVisibilityChanged(visibility);
136    }
137
138    @Override
139    public boolean onTouchEvent(MotionEvent ev) {
140        super.onTouchEvent(ev);
141        return mAwContents.onTouchEvent(ev);
142    }
143
144    @Override
145    public void onDraw(Canvas canvas) {
146        mAwContents.onDraw(canvas);
147        super.onDraw(canvas);
148    }
149
150    // TODO: AwContents could define a generic class that holds an implementation similar to
151    // the one below.
152    private class InternalAccessAdapter implements AwContents.InternalAccessDelegate {
153
154        @Override
155        public boolean drawChild(Canvas canvas, View child, long drawingTime) {
156            return AwTestContainerView.super.drawChild(canvas, child, drawingTime);
157        }
158
159        @Override
160        public boolean super_onKeyUp(int keyCode, KeyEvent event) {
161            return AwTestContainerView.super.onKeyUp(keyCode, event);
162        }
163
164        @Override
165        public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
166            return AwTestContainerView.super.dispatchKeyEventPreIme(event);
167        }
168
169        @Override
170        public boolean super_dispatchKeyEvent(KeyEvent event) {
171            return AwTestContainerView.super.dispatchKeyEvent(event);
172        }
173
174        @Override
175        public boolean super_onGenericMotionEvent(MotionEvent event) {
176            return AwTestContainerView.super.onGenericMotionEvent(event);
177        }
178
179        @Override
180        public void super_onConfigurationChanged(Configuration newConfig) {
181            AwTestContainerView.super.onConfigurationChanged(newConfig);
182        }
183
184        @Override
185        public void super_scrollTo(int scrollX, int scrollY) {
186            // We're intentionally not calling super.scrollTo here to make testing easier.
187            AwTestContainerView.this.scrollTo(scrollX, scrollY);
188        }
189
190        @Override
191        public void overScrollBy(int deltaX, int deltaY,
192                int scrollX, int scrollY,
193                int scrollRangeX, int scrollRangeY,
194                int maxOverScrollX, int maxOverScrollY,
195                boolean isTouchEvent) {
196            // We're intentionally not calling super.scrollTo here to make testing easier.
197            AwTestContainerView.this.overScrollBy(deltaX, deltaY, scrollX, scrollY,
198                     scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
199        }
200
201        @Override
202        public void onScrollChanged(int l, int t, int oldl, int oldt) {
203            AwTestContainerView.super.onScrollChanged(l, t, oldl, oldt);
204        }
205
206        @Override
207        public boolean awakenScrollBars() {
208            return AwTestContainerView.super.awakenScrollBars();
209        }
210
211        @Override
212        public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
213            return AwTestContainerView.super.awakenScrollBars(startDelay, invalidate);
214        }
215
216        @Override
217        public void setMeasuredDimension(int measuredWidth, int measuredHeight) {
218            AwTestContainerView.super.setMeasuredDimension(measuredWidth, measuredHeight);
219        }
220
221        @Override
222        public int super_getScrollBarStyle() {
223            return AwTestContainerView.super.getScrollBarStyle();
224        }
225
226        @Override
227        public boolean requestDrawGL(Canvas canvas) {
228            return false;
229        }
230    }
231}
232