Bug 959382 - BaseTest.setPreferenceAndWaitForChange helper method. r=mcomella

This commit is contained in:
vivek 2014-03-15 17:20:25 -04:00
parent c4ebc3e784
commit 2bad21fdcf
5 changed files with 76 additions and 60 deletions

View File

@ -6,6 +6,10 @@ import com.jayway.android.robotium.solo.Solo;
import org.mozilla.gecko.*;
import org.mozilla.gecko.GeckoThread.LaunchState;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.ContentResolver;
@ -59,9 +63,11 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
public static final int MAX_WAIT_MS = 4500;
public static final int LONG_PRESS_TIME = 6000;
private static final int GECKO_READY_WAIT_MS = 180000;
public static final int MAX_WAIT_BLOCK_FOR_EVENT_DATA_MS = 90000;
private static Class<Activity> mLauncherActivityClass;
private Activity mActivity;
private int mPreferenceRequestID = 0;
protected Solo mSolo;
protected Driver mDriver;
protected Assert mAsserter;
@ -897,4 +903,63 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
}
}
/**
* Set the preference and wait for it to change before proceeding with the test.
*/
public void setPreferenceAndWaitForChange(final JSONObject jsonPref) {
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
// Get the preference name from the json and store it in an array. This array
// will be used later while fetching the preference data.
String[] prefNames = new String[1];
try {
prefNames[0] = jsonPref.getString("name");
} catch (JSONException e) {
mAsserter.ok(false, "Exception in setPreferenceAndWaitForChange", getStackTraceString(e));
}
// Wait for confirmation of the pref change before proceeding with the test.
final int ourRequestID = mPreferenceRequestID--;
final Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestID, prefNames);
// Wait until we get the correct "Preferences:Data" event
waitForCondition(new Condition() {
final long endTime = SystemClock.elapsedRealtime() + MAX_WAIT_BLOCK_FOR_EVENT_DATA_MS;
@Override
public boolean isSatisfied() {
try {
long timeout = endTime - SystemClock.elapsedRealtime();
if (timeout < 0) {
timeout = 0;
}
JSONObject data = new JSONObject(eventExpecter.blockForEventDataWithTimeout(timeout));
int requestID = data.getInt("requestId");
if (requestID != ourRequestID) {
return false;
}
JSONArray preferences = data.getJSONArray("preferences");
mAsserter.is(preferences.length(), 1, "Expecting preference array to have one element");
JSONObject prefs = (JSONObject) preferences.get(0);
mAsserter.is(prefs.getString("name"), jsonPref.getString("name"),
"Expecting returned preference name to be the same as the set name");
mAsserter.is(prefs.getString("type"), jsonPref.getString("type"),
"Expecting returned preference type to be the same as the set type");
mAsserter.is(prefs.get("value"), jsonPref.get("value"),
"Expecting returned preference value to be the same as the set value");
return true;
} catch(JSONException e) {
mAsserter.ok(false, "Exception in setPreferenceAndWaitForChange", getStackTraceString(e));
// Please the java compiler
return false;
}
}
}, MAX_WAIT_BLOCK_FOR_EVENT_DATA_MS);
eventExpecter.unregisterListener();
}
}

View File

@ -55,23 +55,7 @@ public class testAddonManager extends PixelTest {
jsonPref.put("name", "extensions.getAddons.browseAddons");
jsonPref.put("type", "string");
jsonPref.put("value", getAbsoluteUrl("/robocop/robocop_blank_01.html"));
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
// Wait for confirmation of the pref change before proceeding with the test.
final String[] prefNames = { "extensions.getAddons.browseAddons" };
final int ourRequestId = 0x7357;
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestId, prefNames);
JSONObject data = null;
int requestId = -1;
// Wait until we get the correct "Preferences:Data" event
while (requestId != ourRequestId) {
data = new JSONObject(eventExpecter.blockForEventData());
requestId = data.getInt("requestId");
}
eventExpecter.unregisterListener();
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in testAddonManager", ex.toString());

View File

@ -31,23 +31,7 @@ public class testAdobeFlash extends PixelTest {
jsonPref.put("name", "plugin.enable");
jsonPref.put("type", "string");
jsonPref.put("value", "1");
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
// Wait for confirmation of the pref change before proceeding with the test.
final String[] prefNames = { "plugin.default.state" };
final int ourRequestId = 0x7358;
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestId, prefNames);
JSONObject data = null;
int requestId = -1;
// Wait until we get the correct "Preferences:Data" event
while (requestId != ourRequestId) {
data = new JSONObject(eventExpecter.blockForEventData());
requestId = data.getInt("requestId");
}
eventExpecter.unregisterListener();
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in testAdobeFlash", ex.toString());
}

View File

@ -77,14 +77,12 @@ public class testDoorHanger extends BaseTest {
boolean offlineAllowedByDefault = true;
// Save offline-allow-by-default preferences first
final String[] prefNames = { "offline-apps.allow_by_default" };
final int ourRequestId = 0x7357;
final Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestId, prefNames);
try {
// Save offline-allow-by-default preferences first
final String[] prefNames = { "offline-apps.allow_by_default" };
final int ourRequestId = 0x7357;
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestId, prefNames);
JSONObject data = null;
int requestId = -1;
@ -106,7 +104,7 @@ public class testDoorHanger extends BaseTest {
jsonPref.put("name", "offline-apps.allow_by_default");
jsonPref.put("type", "bool");
jsonPref.put("value", false);
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
setPreferenceAndWaitForChange(jsonPref);
} catch (JSONException e) {
mAsserter.ok(false, "exception getting preference", e.toString());
}
@ -134,9 +132,9 @@ public class testDoorHanger extends BaseTest {
// Revert offline setting
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", "offline-apps.allow_by_default");
jsonPref.put("type", "boolean");
jsonPref.put("type", "bool");
jsonPref.put("value", offlineAllowedByDefault);
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
setPreferenceAndWaitForChange(jsonPref);
} catch (JSONException e) {
mAsserter.ok(false, "exception setting preference", e.toString());
}

View File

@ -113,22 +113,7 @@ public class testPasswordEncrypt extends BaseTest {
jsonPref.put("name", "privacy.masterpassword.enabled");
jsonPref.put("type", "string");
jsonPref.put("value", passwd);
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
// Wait for confirmation of the pref change before proceeding with the test.
final String[] prefNames = { "privacy.masterpassword.enabled" };
final int ourRequestId = 0x73577;
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestId, prefNames);
JSONObject data = null;
int requestId = -1;
// Wait until we get the correct "Preferences:Data" event
while (requestId != ourRequestId) {
data = new JSONObject(eventExpecter.blockForEventData());
requestId = data.getInt("requestId");
}
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in toggleMasterPassword", ex.toString());
}