Bug 764901 - Robocop: make testPasswordEncrypt more robust; r=jmaher

This commit is contained in:
Geoff Brown 2012-08-10 15:59:12 -06:00
parent 951cdecc84
commit 123619eb02
3 changed files with 69 additions and 13 deletions

View File

@ -43,7 +43,7 @@ public interface Actions {
*
* @param geckoEvent The geckoEvent JSONObject's type
*/
EventExpecter expectGeckoEvent(String geckoEvent);
RepeatedEventExpecter expectGeckoEvent(String geckoEvent);
/**
* Listens for a paint event. Note that calling expectPaint() will

View File

@ -115,7 +115,7 @@ public class FennecNativeActions implements Actions {
}
}
class GeckoEventExpecter implements EventExpecter {
class GeckoEventExpecter implements RepeatedEventExpecter {
private final String mGeckoEvent;
private final Object[] mRegistrationParams;
private boolean mEventReceived;
@ -143,6 +143,61 @@ public class FennecNativeActions implements Actions {
return;
}
}
try {
mUnregisterGEL.invoke(null, mRegistrationParams);
} catch (IllegalAccessException e) {
FennecNativeDriver.log(LogLevel.ERROR, e);
} catch (InvocationTargetException e) {
FennecNativeDriver.log(LogLevel.ERROR, e);
}
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"unblocked on expecter for " + mGeckoEvent);
}
public synchronized void blockUntilClear(long millis) {
if (millis <= 0) {
throw new IllegalArgumentException("millis must be > 0");
}
// wait for at least one event
long startTime = SystemClock.uptimeMillis();
long endTime = 0;
while (!mEventReceived) {
try {
this.wait(MAX_WAIT_MS);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
break;
}
endTime = SystemClock.uptimeMillis();
if (!mEventReceived && (endTime - startTime >= MAX_WAIT_MS)) {
mAsserter.ok(false, "GeckoEventExpecter", "blockUtilClear timeout");
return;
}
}
// now wait for a period of millis where we don't get an event
startTime = SystemClock.uptimeMillis();
while (true) {
try {
this.wait(millis);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
break;
}
endTime = SystemClock.uptimeMillis();
if (endTime - startTime >= millis) {
// success
break;
}
// we got a notify() before we could wait long enough, so we need to start over
startTime = endTime;
}
try {
mUnregisterGEL.invoke(null, mRegistrationParams);
} catch (IllegalAccessException e) {
FennecNativeDriver.log(LogLevel.ERROR, e);
} catch (InvocationTargetException e) {
FennecNativeDriver.log(LogLevel.ERROR, e);
}
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"unblocked on expecter for " + mGeckoEvent);
}
@ -152,13 +207,6 @@ public class FennecNativeActions implements Actions {
}
void notifyOfEvent() {
try {
mUnregisterGEL.invoke(null, mRegistrationParams);
} catch (IllegalAccessException e) {
FennecNativeDriver.log(LogLevel.ERROR, e);
} catch (InvocationTargetException e) {
FennecNativeDriver.log(LogLevel.ERROR, e);
}
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"received event " + mGeckoEvent);
synchronized (this) {
@ -168,7 +216,7 @@ public class FennecNativeActions implements Actions {
}
}
public EventExpecter expectGeckoEvent(String geckoEvent) {
public RepeatedEventExpecter expectGeckoEvent(String geckoEvent) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"waiting for "+geckoEvent);
try {

View File

@ -129,13 +129,21 @@ public class testPasswordEncrypt extends BaseTest {
jsonPref.put("type", "string");
jsonPref.put("value", passwd);
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
} catch (Exception ex) { }
} catch (Exception ex) {
mAsserter.ok(false, "exception in toggleMasterPassword", ex.toString());
}
// Wait for confirmation of the pref change before proceeding with the test.
JSONArray getPrefData = new JSONArray();
getPrefData.put("privacy.masterpassword.enabled");
Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("Preferences:Data");
Actions.RepeatedEventExpecter contentEventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendGeckoEvent("Preferences:Get", getPrefData.toString());
contentEventExpecter.blockForEvent();
// Receiving a Preferences:Data event is not conclusive evidence that *our*
// preference has been set -- another component may be changing preferences
// at the same time. Mitigate this risk by waiting for a Preference:Data
// and then waiting for a period of time in which no more Preference:Data
// events are received.
contentEventExpecter.blockUntilClear(2000);
}
public void tearDown() throws Exception {