gecko-dev/mobile/android/base/tests/testHistoryTab.java.in
2013-02-01 16:39:49 +02:00

265 lines
9.8 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
import android.view.ViewGroup;
import android.view.View;
import android.widget.ListView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.text.TextUtils;
import android.content.ContentValues;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
/* Tests opening the history tab, that items look correct, clicking on an item
and long tapping on an item
*/
public class testHistoryTab extends PixelTest {
private static final String ABOUT_HOME_URL = "about:home";
private static final String OPEN_NEW_TAB = "Open in New Tab";
private static ListView listview = null;
private String[] bookmarks = new String[] {
"http://mochi.test:8888/tests/robocop/robocop_blank_01.html"
};
private View mFirstChild;
private boolean mNearMidnight;
@Override
protected int getTestType() {
return TEST_MOCHITEST;
}
public void testHistoryTab() {
blockForGeckoReady();
// very approximate date-rollover detection
Calendar cal = new GregorianCalendar();
mNearMidnight = (cal.get(Calendar.HOUR_OF_DAY) == 23);
// load two pages so there is something in our history
// bookmark one of them
String url = getAbsoluteUrl("/robocop/robocop_big_link.html");
loadAndPaint(url);
url = getAbsoluteUrl("/robocop/robocop_blank_01.html");
loadAndPaint(url);
mActions.sendSpecialKey(Actions.SpecialKey.MENU);
mSolo.waitForText("Settings");
// On ICS+ phones, there is no button labeled "Bookmarks"
// instead we have to just dig through every button on the screen
ArrayList<View> images = mSolo.getCurrentViews();
for (int i = 0; i < images.size(); i++) {
final View view = images.get(i);
boolean found = false;
found = "Bookmark".equals(view.getContentDescription());
// on older android versions, try looking at the button's text
if (!found) {
if (view instanceof TextView) {
found = "Bookmark".equals(((TextView)view).getText());
}
}
if (found) {
int[] xy = new int[2];
view.getLocationOnScreen(xy);
final int viewWidth = view.getWidth();
final int viewHeight = view.getHeight();
final float x = xy[0] + (viewWidth / 2.0f);
float y = xy[1] + (viewHeight / 2.0f);
mSolo.clickOnScreen(x, y);
}
}
mAsserter.is(mSolo.waitForText("Bookmark added"), true, "bookmark added successfully");
testList(url);
testContextMenu(url);
testClick(url);
}
private void testList(String url) {
listview = getHistoryList();
// Around midnight we will switch from Today -> Yesterday, account for both
if (!mSolo.waitForText("Today")) {
mSolo.waitForText("Yesterday");
}
mAsserter.isnot(listview, null, "checking that history list exists");
final int count = listview.getChildCount();
mAsserter.is(count, 3, "history list has 3 children");
String loadUrl = "";
for (int i = count - 1; i >= 0; i--) {
View child = listview.getChildAt(i);
ArrayList<View> views = mSolo.getViews(child);
ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
int expectedImages = 1;
for (int j = 0; j < views.size(); j++) {
View v = views.get(j);
if (i == 0) {
ArrayList<TextView> views2 = mSolo.getCurrentTextViews(v);
TextView t = views2.get(0);
String string = t.getText().toString();
boolean headerTextOK = string.equals("Today");
if (!headerTextOK && mNearMidnight && string.equals("Yesterday")) {
headerTextOK = true;
}
mAsserter.ok(headerTextOK, "First row has Today header", string);
expectedImages = 0;
} else if (v instanceof TextView) {
TextView t = (TextView)v;
String string = t.getText().toString();
mAsserter.ok(!TextUtils.isEmpty(string), "TextView is filled in", string);
if (i == 1 || string.startsWith("http")) {
loadUrl = string;
}
int index = Arrays.binarySearch(bookmarks, string);
if (index > -1) {
expectedImages = 2;
}
} else if (v instanceof ImageView) {
imageViews.add((ImageView)v);
}
}
int visible = 0;
for (int j = 0; j < imageViews.size(); j++) {
ImageView img = imageViews.get(j);
visible += (img.getVisibility() == View.VISIBLE) ? 1 : 0;
}
mAsserter.is(visible, expectedImages, "Correct number of ImageViews visible");
}
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
}
private void testContextMenu(String url) {
listview = getHistoryList();
mSolo.waitForText(url);
// wait for the history list to be populated
mFirstChild = null;
boolean success = waitForTest(new BooleanTest() {
public boolean test() {
mFirstChild = listview.getChildAt(1);
if (mFirstChild == null) {
return false;
}
if (mFirstChild instanceof android.view.ViewGroup) {
ViewGroup group = (ViewGroup)mFirstChild;
if (group.getChildCount() < 1) {
return false;
}
for (int i = 0; i < group.getChildCount(); i++) {
View grandChild = group.getChildAt(i);
if (grandChild instanceof android.widget.TextView) {
mAsserter.ok(true, "found TextView:", ((android.widget.TextView)grandChild).getText().toString());
}
}
} else {
mAsserter.dumpLog("first child not a ViewGroup: "+mFirstChild);
return false;
}
return true;
}
}, MAX_WAIT_MS);
if (success == true && mFirstChild != null) {
mAsserter.dumpLog("clickLongOnView: "+mFirstChild);
mSolo.clickLongOnView(mFirstChild);
// TODO: Test clicking these does the right thing
mAsserter.ok(mSolo.waitForText("Open in New Tab"), "Context menu has New Tab option", "Open in New Tab");
mAsserter.ok(mSolo.searchText("Share", true), "Context menu has Share option", "Share");
mAsserter.ok(mSolo.searchText("Remove", true), "Context menu has Remove option", "Remove");
mAsserter.ok(mSolo.searchText("Add to Home Screen", true), "Context menu has Add to Home Screen option", "Add to Home Screen");
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
View child = listview.getChildAt(0);
mSolo.clickLongOnView(child);
mAsserter.is(false, mSolo.waitForText("Open in New Tab"), "Header rows should not show a context menu");
} else {
mAsserter.ok(false, "waiting for history item", "history item available");
}
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
}
private void testClick(String url) {
listview = getHistoryList();
mSolo.waitForText(url);
View child = listview.getChildAt(0);
mSolo.clickOnView(child);
// nothing should happen
Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
mFirstChild = null;
boolean success = waitForTest(new BooleanTest() {
public boolean test() {
mFirstChild = listview.getChildAt(1);
if (mFirstChild == null) {
return false;
}
return true;
}
}, MAX_WAIT_MS);
if (success == true && mFirstChild != null) {
mSolo.clickOnView(mFirstChild);
contentEventExpecter.blockForEvent();
verifyUrl(url);
} else {
mAsserter.ok(false, "waiting for history item", "history item available");
}
}
public void tearDown() throws Exception {
ContentResolver resolver = getActivity().getContentResolver();
Uri uri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/history");
uri = uri.buildUpon().appendQueryParameter("profile", "default")
.appendQueryParameter("sync", "true").build();
resolver.delete(uri, "url = ?", new String[] {
"http://mochi.test:8888/tests/robocop/robocop_blank_01.html"
});
resolver.delete(uri, "url = ?", new String[] {
"http://mochi.test:8888/tests/robocop/robocop_big_link.html"
});
uri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
uri = uri.buildUpon().appendQueryParameter("profile", "default")
.appendQueryParameter("sync", "true").build();
resolver.delete(uri, "url = ?", new String[] {
"http://mochi.test:8888/tests/robocop/robocop_blank_01.html"
});
super.tearDown();
}
}