michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.mozilla.gecko.db.BrowserDB; michael@0: michael@0: import android.content.ContentResolver; michael@0: import android.graphics.Color; michael@0: michael@0: /** michael@0: * Test for thumbnail updates. michael@0: * - loads 2 pages, each of which yield an HTTP 200 michael@0: * - verifies thumbnails are updated for both pages michael@0: * - loads pages again; first page yields HTTP 200, second yields HTTP 404 michael@0: * - verifies thumbnail is updated for HTTP 200, but not HTTP 404 michael@0: * - finally, test that BrowserDB.removeThumbnails drops the thumbnails michael@0: */ michael@0: public class testThumbnails extends BaseTest { michael@0: public void testThumbnails() { michael@0: final String site1Url = getAbsoluteUrl("/robocop/robocop_404.sjs?type=changeColor"); michael@0: final String site2Url = getAbsoluteUrl("/robocop/robocop_404.sjs?type=do404"); michael@0: final String site1Title = "changeColor"; michael@0: final String site2Title = "do404"; michael@0: michael@0: // the session snapshot runnable is run 500ms after document stop. a michael@0: // 3000ms delay gives us 2.5 seconds to take the screenshot, which michael@0: // should be plenty of time, even on slow devices michael@0: final int thumbnailDelay = 3000; michael@0: michael@0: blockForGeckoReady(); michael@0: michael@0: // load sites; both will return HTTP 200 with a green background michael@0: inputAndLoadUrl(site1Url); michael@0: mSolo.sleep(thumbnailDelay); michael@0: inputAndLoadUrl(site2Url); michael@0: mSolo.sleep(thumbnailDelay); michael@0: inputAndLoadUrl("about:home"); michael@0: waitForTest(new ThumbnailTest(site1Title, Color.GREEN), 5000); michael@0: mAsserter.is(getTopSiteThumbnailColor(site1Title), Color.GREEN, "Top site thumbnail updated for HTTP 200"); michael@0: waitForTest(new ThumbnailTest(site2Title, Color.GREEN), 5000); michael@0: mAsserter.is(getTopSiteThumbnailColor(site2Title), Color.GREEN, "Top site thumbnail updated for HTTP 200"); michael@0: michael@0: // load sites again; both will have red background, and do404 will return HTTP 404 michael@0: inputAndLoadUrl(site1Url); michael@0: mSolo.sleep(thumbnailDelay); michael@0: inputAndLoadUrl(site2Url); michael@0: mSolo.sleep(thumbnailDelay); michael@0: inputAndLoadUrl("about:home"); michael@0: waitForTest(new ThumbnailTest(site1Title, Color.RED), 5000); michael@0: mAsserter.is(getTopSiteThumbnailColor(site1Title), Color.RED, "Top site thumbnail updated for HTTP 200"); michael@0: waitForTest(new ThumbnailTest(site2Title, Color.GREEN), 5000); michael@0: mAsserter.is(getTopSiteThumbnailColor(site2Title), Color.GREEN, "Top site thumbnail not updated for HTTP 404"); michael@0: michael@0: // test dropping thumbnails michael@0: final ContentResolver resolver = getActivity().getContentResolver(); michael@0: // check that the thumbnail is non-null michael@0: byte[] thumbnailData = BrowserDB.getThumbnailForUrl(resolver, site1Url); michael@0: mAsserter.ok(thumbnailData != null && thumbnailData.length > 0, "Checking for thumbnail data", "No thumbnail data found"); michael@0: // drop thumbnails michael@0: BrowserDB.removeThumbnails(resolver); michael@0: // check that the thumbnail is now null michael@0: thumbnailData = BrowserDB.getThumbnailForUrl(resolver, site1Url); michael@0: mAsserter.ok(thumbnailData == null || thumbnailData.length == 0, "Checking for thumbnail data", "Thumbnail data found"); michael@0: } michael@0: michael@0: private class ThumbnailTest implements BooleanTest { michael@0: private String mTitle; michael@0: private int mColor; michael@0: michael@0: public ThumbnailTest(String title, int color) { michael@0: mTitle = title; michael@0: mColor = color; michael@0: } michael@0: michael@0: @Override michael@0: public boolean test() { michael@0: return getTopSiteThumbnailColor(mTitle) == mColor; michael@0: } michael@0: } michael@0: michael@0: private int getTopSiteThumbnailColor(String title) { michael@0: // This test is not currently run, so this just needs to compile. michael@0: return -1; michael@0: // ViewGroup topSites = (ViewGroup) getActivity().findViewById(mTopSitesId); michael@0: // if (topSites != null) { michael@0: // final int childCount = topSites.getChildCount(); michael@0: // for (int i = 0; i < childCount; i++) { michael@0: // View child = topSites.getChildAt(i); michael@0: // if (child != null) { michael@0: // TextView titleView = (TextView) child.findViewById(R.id.title); michael@0: // if (titleView != null) { michael@0: // if (titleView.getText().equals(title)) { michael@0: // ImageView thumbnailView = (ImageView) child.findViewById(R.id.thumbnail); michael@0: // if (thumbnailView != null) { michael@0: // Bitmap thumbnail = ((BitmapDrawable) thumbnailView.getDrawable()).getBitmap(); michael@0: // return thumbnail.getPixel(0, 0); michael@0: // } else { michael@0: // mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mThumbnailId: "+R.id.thumbnail); michael@0: // } michael@0: // } michael@0: // } else { michael@0: // mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find R.id.title: "+R.id.title); michael@0: // } michael@0: // } else { michael@0: // mAsserter.dumpLog("getTopSiteThumbnailColor: skipped null child at index "+i); michael@0: // } michael@0: // } michael@0: // } else { michael@0: // mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mTopSitesId: " + mTopSitesId); michael@0: // } michael@0: // return -1; michael@0: } michael@0: }