Bug 1013024 - Part 2: test referrer intents.

This commit is contained in:
Richard Newman 2014-07-03 19:45:24 -07:00
parent 1f0778d2d1
commit 15f495311c
4 changed files with 158 additions and 6 deletions

View File

@ -54,7 +54,8 @@ import android.util.Log;
* Handles distribution file loading and fetching,
* and the corresponding hand-offs to Gecko.
*/
public final class Distribution {
@RobocopTarget
public class Distribution {
private static final String LOGTAG = "GeckoDistribution";
private static final int STATE_UNKNOWN = 0;
@ -108,8 +109,11 @@ public final class Distribution {
/**
* Used as a drop-off point for ReferrerReceiver. Checked when we process
* first-run distribution.
*
* This is `protected` so that test code can clear it between runs.
*/
private static volatile ReferrerDescriptor referrer;
@RobocopTarget
protected static volatile ReferrerDescriptor referrer;
private static Distribution instance;
@ -134,6 +138,7 @@ public final class Distribution {
return instance;
}
@RobocopTarget
public static class DistributionDescriptor {
public final boolean valid;
public final String id;
@ -204,6 +209,7 @@ public final class Distribution {
* Use <code>Context.getPackageResourcePath</code> to find an implicit
* package path. Reuses the existing Distribution if one exists.
*/
@RobocopTarget
public static void init(final Context context) {
Distribution.init(Distribution.getInstance(context));
}
@ -324,9 +330,12 @@ public final class Distribution {
* Postcondition: if this returns true, distributionDir will have been
* set and populated.
*
* This method is *only* protected for use from testDistribution.
*
* @return true if we've set a distribution.
*/
private boolean doInit() {
@RobocopTarget
protected boolean doInit() {
ThreadUtils.assertNotOnUiThread();
// Bail if we've already tried to initialize the distribution, and
@ -449,9 +458,13 @@ public final class Distribution {
* Fetch the provided URI, returning a {@link JarInputStream} if the response body
* is appropriate.
*
* Protected to allow for mocking.
*
* @return the entity body as a stream, or null on failure.
*/
private JarInputStream fetchDistribution(URI uri, HttpURLConnection connection) throws IOException {
@SuppressWarnings("static-method")
@RobocopTarget
protected JarInputStream fetchDistribution(URI uri, HttpURLConnection connection) throws IOException {
final int status = connection.getResponseCode();
Log.d(LOGTAG, "Distribution fetch: " + status);

View File

@ -4,6 +4,8 @@
package org.mozilla.gecko.distribution;
import org.mozilla.gecko.mozglue.RobocopTarget;
import android.net.Uri;
/**
@ -15,6 +17,7 @@ import android.net.Uri;
*
* "utm_source=campsource&utm_medium=campmed&utm_term=term%2Bhere&utm_content=content&utm_campaign=name"
*/
@RobocopTarget
public class ReferrerDescriptor {
public final String source;
public final String medium;
@ -44,4 +47,9 @@ public class ReferrerDescriptor {
content = u.getQueryParameter("utm_content");
campaign = u.getQueryParameter("utm_campaign");
}
@Override
public String toString() {
return "{s: " + source + ", m: " + medium + ", t: " + term + ", c: " + content + ", c: " + campaign + "}";
}
}

View File

@ -33,6 +33,7 @@ public class ReferrerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(LOGTAG, "Received intent " + intent);
if (!ACTION_INSTALL_REFERRER.equals(intent.getAction())) {
// This should never happen.
return;

View File

@ -2,19 +2,29 @@ package org.mozilla.gecko.tests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.jar.JarInputStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.Actions;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.distribution.Distribution;
import org.mozilla.gecko.distribution.ReferrerDescriptor;
import org.mozilla.gecko.mozglue.RobocopTarget;
import org.mozilla.gecko.util.ThreadUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
/**
* Tests distribution customization.
@ -28,6 +38,38 @@ import android.content.SharedPreferences;
* engine.xml
*/
public class testDistribution extends ContentProviderTest {
private static final String CLASS_REFERRER_RECEIVER = "org.mozilla.gecko.distribution.ReferrerReceiver";
private static final String ACTION_INSTALL_REFERRER = "com.android.vending.INSTALL_REFERRER";
private static final int WAIT_TIMEOUT_MSEC = 10000;
public static final String LOGTAG = "GeckoTestDistribution";
public static class TestableDistribution extends Distribution {
@Override
protected JarInputStream fetchDistribution(URI uri,
HttpURLConnection connection) throws IOException {
Log.i(LOGTAG, "Not downloading: this is a test.");
return null;
}
public TestableDistribution(Context context) {
super(context);
}
public void go() {
doInit();
}
@RobocopTarget
public static void clearReferrerDescriptorForTesting() {
referrer = null;
}
@RobocopTarget
public static ReferrerDescriptor getReferrerDescriptorForTesting() {
return referrer;
}
}
private static final String MOCK_PACKAGE = "mock-package.zip";
private static final int PREF_REQUEST_ID = 0x7357;
@ -65,7 +107,7 @@ public class testDistribution extends ContentProviderTest {
mAsserter.dumpLog("Background task completed. Proceeding.");
}
public void testDistribution() {
public void testDistribution() throws Exception {
mActivity = getActivity();
String mockPackagePath = getMockPackagePath();
@ -87,6 +129,90 @@ public class testDistribution extends ContentProviderTest {
setTestLocale("es-MX");
initDistribution(mockPackagePath);
checkLocalizedPreferences("es-MX");
// Test the (stubbed) download interaction.
setTestLocale("en-US");
clearDistributionPref();
doTestValidReferrerIntent();
clearDistributionPref();
doTestInvalidReferrerIntent();
}
public void doTestValidReferrerIntent() throws Exception {
// Send the faux-download intent.
// Equivalent to
// am broadcast -a com.android.vending.INSTALL_REFERRER \
// -n org.mozilla.fennec/org.mozilla.gecko.distribution.ReferrerReceiver \
// --es "referrer" "utm_source=mozilla&utm_medium=testmedium&utm_term=testterm&utm_content=testcontent&utm_campaign=distribution"
final String ref = "utm_source=mozilla&utm_medium=testmedium&utm_term=testterm&utm_content=testcontent&utm_campaign=distribution";
final Intent intent = new Intent(ACTION_INSTALL_REFERRER);
intent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, CLASS_REFERRER_RECEIVER);
intent.putExtra("referrer", ref);
mActivity.sendBroadcast(intent);
// Wait for the intent to be processed.
final TestableDistribution distribution = new TestableDistribution(mActivity);
final Object wait = new Object();
distribution.addOnDistributionReadyCallback(new Runnable() {
@Override
public void run() {
mAsserter.ok(!distribution.exists(), "Not processed.", "No download because we're offline.");
ReferrerDescriptor referrerValue = TestableDistribution.getReferrerDescriptorForTesting();
mAsserter.dumpLog("Referrer was " + referrerValue);
mAsserter.is(referrerValue.content, "testcontent", "Referrer content");
mAsserter.is(referrerValue.medium, "testmedium", "Referrer medium");
mAsserter.is(referrerValue.campaign, "distribution", "Referrer campaign");
synchronized (wait) {
wait.notifyAll();
}
}
});
distribution.go();
synchronized (wait) {
wait.wait(WAIT_TIMEOUT_MSEC);
}
}
/**
* Test processing if the campaign isn't "distribution". The intent shouldn't
* result in a download, and won't be saved as the temporary referrer,
* even if we *do* include it in a Campaign:Set message.
*/
public void doTestInvalidReferrerIntent() throws Exception {
// Send the faux-download intent.
// Equivalent to
// am broadcast -a com.android.vending.INSTALL_REFERRER \
// -n org.mozilla.fennec/org.mozilla.gecko.distribution.ReferrerReceiver \
// --es "referrer" "utm_source=mozilla&utm_medium=testmedium&utm_term=testterm&utm_content=testcontent&utm_campaign=testname"
final String ref = "utm_source=mozilla&utm_medium=testmedium&utm_term=testterm&utm_content=testcontent&utm_campaign=testname";
final Intent intent = new Intent(ACTION_INSTALL_REFERRER);
intent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, CLASS_REFERRER_RECEIVER);
intent.putExtra("referrer", ref);
mActivity.sendBroadcast(intent);
// Wait for the intent to be processed.
final TestableDistribution distribution = new TestableDistribution(mActivity);
final Object wait = new Object();
distribution.addOnDistributionReadyCallback(new Runnable() {
@Override
public void run() {
mAsserter.ok(!distribution.exists(), "Not processed.", "No download because campaign was wrong.");
ReferrerDescriptor referrerValue = TestableDistribution.getReferrerDescriptorForTesting();
mAsserter.is(referrerValue, null, "No referrer.");
synchronized (wait) {
wait.notifyAll();
}
}
});
distribution.go();
synchronized (wait) {
wait.wait(WAIT_TIMEOUT_MSEC);
}
}
// Initialize the distribution from the mock package.
@ -288,12 +414,16 @@ public class testDistribution extends ContentProviderTest {
return mockPackagePath;
}
// Clears the distribution pref to return distribution state to STATE_UNKNOWN
/**
* Clears the distribution pref to return distribution state to STATE_UNKNOWN,
* and wipes the in-memory referrer pigeonhole.
*/
private void clearDistributionPref() {
mAsserter.dumpLog("Clearing distribution pref.");
SharedPreferences settings = mActivity.getSharedPreferences("GeckoApp", Activity.MODE_PRIVATE);
String keyName = mActivity.getPackageName() + ".distribution_state";
settings.edit().remove(keyName).commit();
TestableDistribution.clearReferrerDescriptorForTesting();
}
@Override