mobile/android/base/tests/testThumbnails.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/testThumbnails.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +package org.mozilla.gecko.tests;
     1.5 +
     1.6 +import org.mozilla.gecko.db.BrowserDB;
     1.7 +
     1.8 +import android.content.ContentResolver;
     1.9 +import android.graphics.Color;
    1.10 +
    1.11 +/**
    1.12 + * Test for thumbnail updates.
    1.13 + * - loads 2 pages, each of which yield an HTTP 200
    1.14 + * - verifies thumbnails are updated for both pages
    1.15 + * - loads pages again; first page yields HTTP 200, second yields HTTP 404
    1.16 + * - verifies thumbnail is updated for HTTP 200, but not HTTP 404
    1.17 + * - finally, test that BrowserDB.removeThumbnails drops the thumbnails
    1.18 + */
    1.19 +public class testThumbnails extends BaseTest {
    1.20 +    public void testThumbnails() {
    1.21 +        final String site1Url = getAbsoluteUrl("/robocop/robocop_404.sjs?type=changeColor");
    1.22 +        final String site2Url = getAbsoluteUrl("/robocop/robocop_404.sjs?type=do404");
    1.23 +        final String site1Title = "changeColor";
    1.24 +        final String site2Title = "do404";
    1.25 +
    1.26 +        // the session snapshot runnable is run 500ms after document stop. a
    1.27 +        // 3000ms delay gives us 2.5 seconds to take the screenshot, which
    1.28 +        // should be plenty of time, even on slow devices
    1.29 +        final int thumbnailDelay = 3000;
    1.30 +
    1.31 +        blockForGeckoReady();
    1.32 +
    1.33 +        // load sites; both will return HTTP 200 with a green background
    1.34 +        inputAndLoadUrl(site1Url);
    1.35 +        mSolo.sleep(thumbnailDelay);
    1.36 +        inputAndLoadUrl(site2Url);
    1.37 +        mSolo.sleep(thumbnailDelay);
    1.38 +        inputAndLoadUrl("about:home");
    1.39 +        waitForTest(new ThumbnailTest(site1Title, Color.GREEN), 5000);
    1.40 +        mAsserter.is(getTopSiteThumbnailColor(site1Title), Color.GREEN, "Top site thumbnail updated for HTTP 200");
    1.41 +        waitForTest(new ThumbnailTest(site2Title, Color.GREEN), 5000);
    1.42 +        mAsserter.is(getTopSiteThumbnailColor(site2Title), Color.GREEN, "Top site thumbnail updated for HTTP 200");
    1.43 +
    1.44 +        // load sites again; both will have red background, and do404 will return HTTP 404
    1.45 +        inputAndLoadUrl(site1Url);
    1.46 +        mSolo.sleep(thumbnailDelay);
    1.47 +        inputAndLoadUrl(site2Url);
    1.48 +        mSolo.sleep(thumbnailDelay);
    1.49 +        inputAndLoadUrl("about:home");
    1.50 +        waitForTest(new ThumbnailTest(site1Title, Color.RED), 5000);
    1.51 +        mAsserter.is(getTopSiteThumbnailColor(site1Title), Color.RED, "Top site thumbnail updated for HTTP 200");
    1.52 +        waitForTest(new ThumbnailTest(site2Title, Color.GREEN), 5000);
    1.53 +        mAsserter.is(getTopSiteThumbnailColor(site2Title), Color.GREEN, "Top site thumbnail not updated for HTTP 404");
    1.54 +
    1.55 +        // test dropping thumbnails
    1.56 +        final ContentResolver resolver = getActivity().getContentResolver();
    1.57 +        // check that the thumbnail is non-null
    1.58 +        byte[] thumbnailData = BrowserDB.getThumbnailForUrl(resolver, site1Url);
    1.59 +        mAsserter.ok(thumbnailData != null && thumbnailData.length > 0, "Checking for thumbnail data", "No thumbnail data found");
    1.60 +        // drop thumbnails
    1.61 +        BrowserDB.removeThumbnails(resolver);
    1.62 +        // check that the thumbnail is now null
    1.63 +        thumbnailData = BrowserDB.getThumbnailForUrl(resolver, site1Url);
    1.64 +        mAsserter.ok(thumbnailData == null || thumbnailData.length == 0, "Checking for thumbnail data", "Thumbnail data found");
    1.65 +    }
    1.66 +
    1.67 +    private class ThumbnailTest implements BooleanTest {
    1.68 +        private String mTitle;
    1.69 +        private int mColor;
    1.70 +
    1.71 +        public ThumbnailTest(String title, int color) {
    1.72 +            mTitle = title;
    1.73 +            mColor = color;
    1.74 +        }
    1.75 +
    1.76 +        @Override
    1.77 +        public boolean test() {
    1.78 +            return getTopSiteThumbnailColor(mTitle) == mColor;
    1.79 +        }
    1.80 +    }
    1.81 +
    1.82 +    private int getTopSiteThumbnailColor(String title) {
    1.83 +        // This test is not currently run, so this just needs to compile.
    1.84 +        return -1;
    1.85 +//        ViewGroup topSites = (ViewGroup) getActivity().findViewById(mTopSitesId);
    1.86 +//        if (topSites != null) {
    1.87 +//            final int childCount = topSites.getChildCount();
    1.88 +//            for (int i = 0; i < childCount; i++) {
    1.89 +//                View child = topSites.getChildAt(i);
    1.90 +//                if (child != null) {
    1.91 +//                    TextView titleView = (TextView) child.findViewById(R.id.title);
    1.92 +//                    if (titleView != null) {
    1.93 +//                        if (titleView.getText().equals(title)) {
    1.94 +//                            ImageView thumbnailView = (ImageView) child.findViewById(R.id.thumbnail);
    1.95 +//                            if (thumbnailView != null) {
    1.96 +//                                Bitmap thumbnail = ((BitmapDrawable) thumbnailView.getDrawable()).getBitmap();
    1.97 +//                                return thumbnail.getPixel(0, 0);
    1.98 +//                            } else {
    1.99 +//                                mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mThumbnailId: "+R.id.thumbnail);
   1.100 +//                            }
   1.101 +//                        }
   1.102 +//                    } else {
   1.103 +//                        mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find R.id.title: "+R.id.title);
   1.104 +//                    }
   1.105 +//                } else {
   1.106 +//                    mAsserter.dumpLog("getTopSiteThumbnailColor: skipped null child at index "+i);
   1.107 +//                }
   1.108 +//            }
   1.109 +//        } else {
   1.110 +//            mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mTopSitesId: " + mTopSitesId);
   1.111 +//        }
   1.112 +//        return -1;
   1.113 +    }
   1.114 +}

mercurial