Bug 1459299 - 1. Add evaluateChromeJS for chrome privilege code; r=snorp

Add GeckoSessionTestRule.evaluateChromeJS for JS code that requires
chrome privileges, such as setting prefs.

MozReview-Commit-ID: G7NUKukWTT8

--HG--
extra : rebase_source : c0d4684ba5a9d4735e30b49f584e8e0222210c87
This commit is contained in:
Jim Chen 2018-05-11 10:25:03 -04:00
parent c2489bc039
commit f0ce2a7b38
4 changed files with 58 additions and 2 deletions

View File

@ -101,7 +101,7 @@ open class BaseSessionTest(noErrorCollector: Boolean = false) {
fun GeckoSession.synthesizeTap(x: Int, y: Int) =
sessionRule.synthesizeTap(this, x, y)
fun GeckoSession.evaluateJS(js: String) =
fun GeckoSession.evaluateJS(js: String): Any? =
sessionRule.evaluateJS(this, js)
infix fun Any?.dot(prop: Any): Any? =

View File

@ -1268,4 +1268,21 @@ class GeckoSessionTestRuleTest : BaseSessionTest(noErrorCollector = true) {
fun evaluateJS_throwOnSyntaxError() {
sessionRule.session.evaluateJS("<{[")
}
@WithDevToolsAPI
@Test(expected = RuntimeException::class)
fun evaluateJS_throwOnChromeAccess() {
sessionRule.session.evaluateJS("ChromeUtils")
}
@WithDevToolsAPI
@Test fun evaluateChromeJS() {
assertThat("Should be able to access ChromeUtils",
sessionRule.evaluateChromeJS("ChromeUtils"), notNullValue())
}
@Test(expected = AssertionError::class)
fun evaluateChromeJS_throwOnNotWithDevTools() {
sessionRule.evaluateChromeJS("0")
}
}

View File

@ -203,4 +203,17 @@ public final class RDPConnection implements Closeable {
final Actor actor = getActor(reply);
return (actor != null) ? (Tab) actor : new Tab(this, reply);
}
/**
* Get the actor for the chrome process. The returned Tab object acts like a tab but has
* chrome privileges.
*
* @return Tab actor representing the process.
*/
public Tab getChromeProcess() {
final JSONObject reply = mRoot.sendPacket("{\"type\":\"getProcess\"}", "form")
.optJSONObject("form");
final Actor actor = getActor(reply);
return (actor != null) ? (Tab) actor : new Tab(this, reply);
}
}

View File

@ -607,6 +607,7 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
protected boolean mClosedSession;
protected boolean mWithDevTools;
protected Map<GeckoSession, Tab> mRDPTabs;
protected Tab mRDPChromeProcess;
public GeckoSessionTestRule() {
mDefaultSettings = new GeckoSessionSettings();
@ -996,6 +997,7 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
mLastWaitEnd = 0;
mTimeoutMillis = 0;
mRDPTabs = null;
mRDPChromeProcess = null;
}
@Override
@ -1631,12 +1633,36 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
* @param session Session containing the target page.
* @param js JavaScript expression.
* @return Result of evaluating the expression.
* @see #evaluateChromeJS
*/
public Object evaluateJS(final @NonNull GeckoSession session, final @NonNull String js) {
assertThat("Must enable RDP using @WithDevToolsAPI", mRDPTabs, notNullValue());
assertThat("Must enable RDP using @WithDevToolsAPI",
mWithDevTools, equalTo(true));
final Tab tab = mRDPTabs.get(session);
assertThat("Session should have tab object", tab, notNullValue());
return tab.getConsole().evaluateJS(js);
}
/**
* Evaluate a JavaScript expression in the context of a chrome window and return the result.
* RDP must be enabled first using the {@link WithDevToolsAPI} annotation. Results are
* converted the same way as {@link #evaluateJS}.
*
* @param js JavaScript expression.
* @return Result of evaluating the expression.
* @see #evaluateJS
*/
public Object evaluateChromeJS(final @NonNull String js) {
assertThat("Must enable RDP using @WithDevToolsAPI",
mWithDevTools, equalTo(true));
if (mRDPChromeProcess == null) {
mRDPChromeProcess = sRDPConnection.getChromeProcess();
assertThat("Should have chrome process object",
mRDPChromeProcess, notNullValue());
mRDPChromeProcess.attach();
}
return mRDPChromeProcess.getConsole().evaluateJS(js);
}
}