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