mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-31 17:32:51 +08:00
Merge branch 'puritytool'
This commit is contained in:
commit
381ce535bf
File diff suppressed because it is too large
Load Diff
@ -21,14 +21,72 @@ package org.apache.cordova.test;
|
||||
*/
|
||||
|
||||
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
import org.apache.cordova.test.util.Purity;
|
||||
import org.apache.cordova.test.actions.iframe;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Instrumentation;
|
||||
import android.test.ActivityInstrumentationTestCase2;
|
||||
import android.test.TouchUtils;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
public class IFrameTest extends ActivityInstrumentationTestCase2<iframe> {
|
||||
public class IFrameTest extends ActivityInstrumentationTestCase2 {
|
||||
|
||||
public IFrameTest() {
|
||||
super("org.apache.cordova.test",iframe.class);
|
||||
}
|
||||
|
||||
private Instrumentation mInstr;
|
||||
private Activity testActivity;
|
||||
private FrameLayout containerView;
|
||||
private LinearLayout innerContainer;
|
||||
private CordovaWebView testView;
|
||||
private TouchUtils touch;
|
||||
private Purity touchTool;
|
||||
|
||||
public IFrameTest() {
|
||||
super("org.apache.cordova.test",iframe.class);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
touch = new TouchUtils();
|
||||
touchTool = new Purity(testActivity, getInstrumentation());
|
||||
}
|
||||
|
||||
|
||||
public void testIframeDest()
|
||||
{
|
||||
testView.sendJavascript("loadUrl('http://maps.google.com/maps?output=embed');");
|
||||
sleep(3000);
|
||||
testView.sendJavascript("loadUrl('index2.html')");
|
||||
sleep(1000);
|
||||
String url = testView.getUrl();
|
||||
assertTrue(url.endsWith("index.html"));
|
||||
}
|
||||
|
||||
public void testIframeHistory()
|
||||
{
|
||||
testView.sendJavascript("loadUrl('http://maps.google.com/maps?output=embed');");
|
||||
sleep(3000);
|
||||
testView.sendJavascript("loadUrl('index2.html')");
|
||||
sleep(1000);
|
||||
String url = testView.getUrl();
|
||||
testView.backHistory();
|
||||
sleep(1000);
|
||||
assertTrue(url.endsWith("index.html"));
|
||||
}
|
||||
|
||||
private void sleep(int timeout) {
|
||||
try {
|
||||
Thread.sleep(timeout);
|
||||
} catch (InterruptedException e) {
|
||||
fail("Unexpected Timeout");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,15 +21,55 @@ 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<jqmtabbackbutton> {
|
||||
|
||||
public JQMTabTest(Class<jqmtabbackbutton> activityClass) {
|
||||
super(activityClass);
|
||||
// TODO Auto-generated constructor stub
|
||||
private Instrumentation mInstr;
|
||||
private jqmtabbackbutton testActivity;
|
||||
private FrameLayout containerView;
|
||||
private LinearLayout innerContainer;
|
||||
private CordovaWebView testView;
|
||||
private Purity touchTool;
|
||||
|
||||
public JQMTabTest()
|
||||
{
|
||||
super("org.apache.cordova.test.activity", jqmtabbackbutton.class);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
public void testTouch()
|
||||
{
|
||||
sleep(5000);
|
||||
int viewportHeight = touchTool.getViewportHeight() - 40;
|
||||
int viewportWidth = touchTool.getViewportWidth();
|
||||
touchTool.touch(50, viewportHeight);
|
||||
sleep(10000);
|
||||
}
|
||||
|
||||
private void sleep(int timeout) {
|
||||
try {
|
||||
Thread.sleep(timeout);
|
||||
} catch (InterruptedException e) {
|
||||
fail("Unexpected Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
171
test/src/org/apache/cordova/test/util/Purity.java
Normal file
171
test/src/org/apache/cordova/test/util/Purity.java
Normal file
@ -0,0 +1,171 @@
|
||||
/*
|
||||
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;
|
||||
boolean fingerDown = false;
|
||||
|
||||
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 int getViewportWidth()
|
||||
{
|
||||
return (int) (width/density);
|
||||
}
|
||||
|
||||
public int getViewportHeight()
|
||||
{
|
||||
return (int) (height/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();
|
||||
if(!fingerDown)
|
||||
{
|
||||
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, realX, realY, 0);
|
||||
inst.sendPointerSync(downEvent);
|
||||
}
|
||||
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, realX, realY, 0);
|
||||
inst.sendPointerSync(upEvent);
|
||||
}
|
||||
|
||||
public void touchStart(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);
|
||||
fingerDown = true;
|
||||
}
|
||||
|
||||
//Move from the touch start
|
||||
public void touchMove(int x, int y)
|
||||
{
|
||||
if(!fingerDown)
|
||||
touchStart(x,y);
|
||||
else
|
||||
{
|
||||
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_MOVE, realX, realY, 0);
|
||||
inst.sendPointerSync(event);
|
||||
}
|
||||
}
|
||||
|
||||
public void touchEnd(int x, int y)
|
||||
{
|
||||
if(!fingerDown)
|
||||
{
|
||||
touch(x, y);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
fingerDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
if(state != null)
|
||||
state.recycle();
|
||||
}
|
||||
|
||||
protected void finalize()
|
||||
{
|
||||
clearBitmap();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user