diff --git a/test/src/org/apache/cordova/test/JQMTabTest.java b/test/src/org/apache/cordova/test/JQMTabTest.java index 8d01ff29..1e58dee4 100644 --- a/test/src/org/apache/cordova/test/JQMTabTest.java +++ b/test/src/org/apache/cordova/test/JQMTabTest.java @@ -21,15 +21,39 @@ package org.apache.cordova.test; */ +import org.apache.cordova.CordovaWebView; +import org.apache.cordova.test.util.Purity; import org.apache.cordova.test.actions.jqmtabbackbutton; +import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; +import android.widget.FrameLayout; +import android.widget.LinearLayout; public class JQMTabTest extends ActivityInstrumentationTestCase2 { + private Instrumentation mInstr; + private jqmtabbackbutton testActivity; + private FrameLayout containerView; + private LinearLayout innerContainer; + private CordovaWebView testView; + private Purity touchTool; + public JQMTabTest(Class activityClass) { super(activityClass); // TODO Auto-generated constructor stub } + + protected void setUp() throws Exception { + super.setUp(); + mInstr = this.getInstrumentation(); + testActivity = this.getActivity(); + containerView = (FrameLayout) testActivity.findViewById(android.R.id.content); + innerContainer = (LinearLayout) containerView.getChildAt(0); + testView = (CordovaWebView) innerContainer.getChildAt(0); + touchTool = new Purity(testActivity, getInstrumentation()); + } + + } diff --git a/test/src/org/apache/cordova/test/util/Purity.java b/test/src/org/apache/cordova/test/util/Purity.java new file mode 100644 index 00000000..0cee4944 --- /dev/null +++ b/test/src/org/apache/cordova/test/util/Purity.java @@ -0,0 +1,101 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +/* + * Purity is a small set of Android utility methods that allows us to simulate touch events on + * Android applications. This is important for simulating some of the most annoying tests. + */ + +package org.apache.cordova.test.util; + +import android.app.Instrumentation; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Picture; +import android.os.SystemClock; +import android.util.DisplayMetrics; +import android.view.MotionEvent; +import android.webkit.WebView; + + +public class Purity { + + Instrumentation inst; + int width, height; + float density; + Bitmap state; + + public Purity(Context ctx, Instrumentation i) + { + inst = i; + DisplayMetrics display = ctx.getResources().getDisplayMetrics(); + density = display.density; + width = display.widthPixels; + height = display.heightPixels; + + } + + /* + * WebKit doesn't give you real pixels anymore, this is done for subpixel fonts to appear on + * iOS and Android. However, Android automation requires real pixels + */ + private int getRealCoord(int coord) + { + return (int) (coord * density); + } + + public void touch(int x, int y) + { + int realX = getRealCoord(x); + int realY = getRealCoord(y); + long downTime = SystemClock.uptimeMillis(); + // event time MUST be retrieved only by this way! + long eventTime = SystemClock.uptimeMillis(); + MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, realX, realY, 0); + inst.sendPointerSync(event); + } + + public void setBitmap(WebView view) + { + Picture p = view.capturePicture(); + state = Bitmap.createBitmap(p.getWidth(), p.getHeight(), Bitmap.Config.ARGB_8888); + } + + public boolean checkRenderView(WebView view) + { + if(state == null) + { + setBitmap(view); + return false; + } + else + { + Picture p = view.capturePicture(); + Bitmap newState = Bitmap.createBitmap(p.getWidth(), p.getHeight(), Bitmap.Config.ARGB_8888); + boolean result = newState.equals(state); + newState.recycle(); + return result; + } + } + + public void clearBitmap() + { + state.recycle(); + } +}