Bug 1320775 - Add tests for SuggestedSiteLoader/Preparer r=sebastian

MozReview-Commit-ID: GXB4Ott4MAi

--HG--
extra : rebase_source : e08796dd385fa821d35f8de5e0abfd1bbdb55646
This commit is contained in:
Andrzej Hunt 2017-02-08 16:37:30 -08:00
Родитель e50e97366d
Коммит 11a122fac4
2 изменённых файлов: 217 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,140 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.icons.loader;
import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.UserManager;
import com.squareup.picasso.RequestCreator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.R;
import org.mozilla.gecko.background.testhelpers.TestRunner;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.SuggestedSites;
import org.mozilla.gecko.icons.IconDescriptor;
import org.mozilla.gecko.icons.IconRequest;
import org.mozilla.gecko.icons.IconRequestBuilder;
import org.mozilla.gecko.icons.IconResponse;
import org.mozilla.gecko.icons.Icons;
import org.mozilla.gecko.restrictions.Restrictions;
import org.robolectric.RuntimeEnvironment;
import java.io.IOException;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
@RunWith(TestRunner.class)
public class TestSuggestedSiteLoader {
private Context context;
private static final String[] DEFAULT_SUGGESTED_SITES_ICONS = {
"https://m.facebook.com/",
"https://m.youtube.com/",
"https://www.amazon.com/",
"https://www.wikipedia.org/",
"https://mobile.twitter.com/"
};
@Before
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void setUp() throws IOException {
// This seems to be done automatically in newer versions of RoboElectric, we need our own
// version for now:
if (!AppConstants.Versions.preJBMR2) {
context = spy(RuntimeEnvironment.application);
UserManager userManager = spy((UserManager) context.getSystemService(Context.USER_SERVICE));
doReturn(new Bundle()).when(userManager).getApplicationRestrictions(anyString());
doReturn(new Bundle()).when(userManager).getUserRestrictions();
doReturn(userManager).when(context).getSystemService(Context.USER_SERVICE);
} else {
context = RuntimeEnvironment.application;
}
final SuggestedSites sites = new SuggestedSites(context);
BrowserDB.from(context).setSuggestedSites(sites);
// Force loading sites, so that SuggestedSites is correctly initialised. Without this, we
// won't be able to retrieve site data (i.e. icon locations and colours).
final Cursor sitesCursor = sites.get(200);
sitesCursor.close();
}
@Test
public void testLoadingNonSuggestedIcons() {
final IconRequest request = Icons.with(RuntimeEnvironment.application)
.pageUrl("http://www.mozilla.org")
.icon(IconDescriptor.createGenericIcon("http://www.mozilla.org/favicon.ico"))
.build();
final IconLoader loader = new SuggestedSiteLoader();
final IconResponse response = loader.load(request);
Assert.assertNull(response);
}
private IconResponse loadIconForSite(final String site, final boolean skipDisk) throws IOException {
IconRequestBuilder requestBuilder = Icons.with(RuntimeEnvironment.application)
.pageUrl(site)
.icon(IconDescriptor.createGenericIcon(SuggestedSiteLoader.SUGGESTED_SITE_TOUCHTILE + site));
if (skipDisk) {
requestBuilder = requestBuilder.skipDisk();
}
final IconRequest request = requestBuilder.build();
// We have to mock the loader: there's no practical reason for not loading the actual images,
// but Picasso expects to run off the main thread, and RoboElectric only has one thread.
final SuggestedSiteLoader loader = spy(new SuggestedSiteLoader());
// Returning any valid bitmap is good enough:
doReturn(
Bitmap.createBitmap(new int[] { Color.RED }, 1, 1, null))
.when(loader)
.loadBitmap(any(Context.class), anyString());
return loader.load(request);
}
@Test
public void testLoadingSuggestedIcons() throws IOException {
for (final String site : DEFAULT_SUGGESTED_SITES_ICONS) {
final IconResponse response = loadIconForSite(site, false);
Assert.assertNotNull(response);
final Bitmap out = response.getBitmap();
final int expectedSize = context.getResources().getDimensionPixelSize(R.dimen.favicon_bg);
Assert.assertEquals(expectedSize, out.getWidth());
Assert.assertEquals(expectedSize, out.getWidth());
}
}
@Test
public void testRespectsSkipDisk() throws IOException {
for (final String site : DEFAULT_SUGGESTED_SITES_ICONS) {
final IconResponse response = loadIconForSite(site, true);
Assert.assertNull(response);
}
}
}

Просмотреть файл

@ -0,0 +1,77 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.icons.preparation;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mozilla.gecko.background.testhelpers.TestRunner;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.SuggestedSites;
import org.mozilla.gecko.icons.IconRequest;
import org.mozilla.gecko.icons.Icons;
import org.mozilla.gecko.icons.loader.SuggestedSiteLoader;
import org.mozilla.gecko.restrictions.Restrictions;
import org.robolectric.RuntimeEnvironment;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
@RunWith(TestRunner.class)
public class TestSuggestedSitePreparer {
private static final String[] DEFAULT_SUGGESTED_SITES_ICONS = {
"https://m.facebook.com/",
"https://m.youtube.com/",
"https://www.amazon.com/",
"https://www.wikipedia.org/",
"https://mobile.twitter.com/"
};
@Before
public void setUp() throws IOException {
Restrictions.createConfiguration(RuntimeEnvironment.application);
BrowserDB.from(RuntimeEnvironment.application).setSuggestedSites(new SuggestedSites(RuntimeEnvironment.application));
}
@Test
public void testNonSuggestedURL() {
final IconRequest request = Icons.with(RuntimeEnvironment.application)
.pageUrl("http://www.mozilla.org")
.build();
Assert.assertEquals(0, request.getIconCount());
final Preparer preparer = new SuggestedSitePreparer();
preparer.prepare(request);
Assert.assertEquals(0, request.getIconCount());
}
@Test
public void testSuggestedIconsAdded() {
for (String site : DEFAULT_SUGGESTED_SITES_ICONS) {
final IconRequest request = Icons.with(RuntimeEnvironment.application)
.pageUrl(site)
.build();
Assert.assertEquals(0, request.getIconCount());
final Preparer preparer = new SuggestedSitePreparer();
preparer.prepare(request);
Assert.assertEquals(1, request.getIconCount());
// We use the site URL as the icon URL (along with the touchtile "protocol"),
// SuggestSiteLoader then translates that to the appropriate underlying icons:
assertEquals(SuggestedSiteLoader.SUGGESTED_SITE_TOUCHTILE + site,
request.getBestIcon().getUrl());
}
}
}