#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 final int WAIT_FOR_CHILD_TIMEOUT = 2000; 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("Bookmark"); mSolo.clickOnText("Bookmark"); mAsserter.is(mSolo.waitForText("Bookmark added"), true, "bookmark added successfully"); testList(url); testContextMenu(url); testClick(url); } private void testList(String url) { listview = getHistoryList(); // clear VKB mActions.sendSpecialKey(Actions.SpecialKey.BACK); mSolo.waitForText(url); mAsserter.ok(listview != null, "checking that history list exists", listview.toString()); mAsserter.is(listview.getChildCount(), 3, "history list has 3 children"); final int count = listview.getChildCount(); String loadUrl = ""; for (int i = count - 1; i >= 0; i--) { View child = listview.getChildAt(i); ArrayList views = mSolo.getViews(child); ArrayList imageViews = new ArrayList(); int expectedImages = 1; for (int j = 0; j < views.size(); j++) { View v = views.get(j); if (i == 0) { ArrayList 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(); // clear VKB mActions.sendSpecialKey(Actions.SpecialKey.BACK); 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; } }, WAIT_FOR_CHILD_TIMEOUT); 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(); // clear VKB mActions.sendSpecialKey(Actions.SpecialKey.BACK); 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; } }, WAIT_FOR_CHILD_TIMEOUT); if (success == true && mFirstChild != null) { mSolo.clickOnView(mFirstChild); contentEventExpecter.blockForEvent(); verifyUrl(url); } else { mAsserter.ok(false, "waiting for history item", "history item available"); } } private ListView getHistoryList() { Activity awesomeBarActivity = clickOnAwesomeBar(); mSolo.clickOnText("History"); final ArrayList views = mSolo.getCurrentListViews(); listview = null; boolean success = waitForTest(new BooleanTest() { public boolean test() { for (ListView view : views) { if (view.getTag() == "history") { listview = view; return true; } } return false; } }, WAIT_FOR_CHILD_TIMEOUT); return listview; } 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(); } }