Skip to content

Commit 90dd53c

Browse files
committed
[java] Checking for changes in frozen preferences in XPI-based FirefoxDriver only
1 parent d9aa681 commit 90dd53c

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

java/client/src/org/openqa/selenium/firefox/FirefoxProfile.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ public void cleanTemporaryModel() {
380380
clean(model);
381381
}
382382

383+
public void checkForChangesInFrozenPreferences() {
384+
additionalPrefs.checkForChangesInFrozenPreferences();
385+
}
386+
383387
/**
384388
* Call this to cause the current profile to be written to disk. The profile directory is
385389
* returned. Note that this profile directory is a temporary one and will be deleted when the JVM

java/client/src/org/openqa/selenium/firefox/Preferences.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
package org.openqa.selenium.firefox;
1919

20-
import static com.google.common.base.Preconditions.checkArgument;
2120
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static com.google.common.base.Preconditions.checkState;
2222
import static org.openqa.selenium.json.Json.MAP_TYPE;
2323

2424
import com.google.common.annotations.VisibleForTesting;
@@ -142,7 +142,6 @@ private void readPreferences(Reader reader) throws IOException {
142142
}
143143

144144
public void setPreference(String key, String value) {
145-
checkPreference(key, value);
146145
if (isStringified(value)) {
147146
throw new IllegalArgumentException(
148147
String.format("Preference values must be plain strings: %s: %s",
@@ -152,12 +151,10 @@ public void setPreference(String key, String value) {
152151
}
153152

154153
public void setPreference(String key, boolean value) {
155-
checkPreference(key, value);
156154
allPrefs.put(key, value);
157155
}
158156

159157
public void setPreference(String key, int value) {
160-
checkPreference(key, value);
161158
allPrefs.put(key, value);
162159
}
163160

@@ -220,9 +217,13 @@ public void putAll(Map<String, Object> frozenPreferences) {
220217
allPrefs.putAll(frozenPreferences);
221218
}
222219

220+
void checkForChangesInFrozenPreferences() {
221+
allPrefs.forEach((this::checkPreference));
222+
}
223+
223224
private void checkPreference(String key, Object value) {
224225
checkNotNull(value);
225-
checkArgument(!immutablePrefs.containsKey(key) ||
226+
checkState(!immutablePrefs.containsKey(key) ||
226227
(immutablePrefs.containsKey(key) && value.equals(immutablePrefs.get(key))),
227228
"Preference %s may not be overridden: frozen value=%s, requested value=%s",
228229
key, immutablePrefs.get(key), value);
@@ -233,10 +234,10 @@ private void checkPreference(String key, Object value) {
233234
} else if (value instanceof Integer) {
234235
n = (Integer) value;
235236
} else {
236-
throw new IllegalArgumentException(String.format(
237+
throw new IllegalStateException(String.format(
237238
"%s value must be a number: %s", MAX_SCRIPT_RUN_TIME_KEY, value.getClass().getName()));
238239
}
239-
checkArgument(n == 0 || n >= DEFAULT_MAX_SCRIPT_RUN_TIME,
240+
checkState(n == 0 || n >= DEFAULT_MAX_SCRIPT_RUN_TIME,
240241
"%s must be == 0 || >= %s",
241242
MAX_SCRIPT_RUN_TIME_KEY,
242243
DEFAULT_MAX_SCRIPT_RUN_TIME);

java/client/src/org/openqa/selenium/firefox/xpi/XpiDriverService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public void start() throws IOException {
123123
try {
124124
profile.setPreference(PORT_PREFERENCE, port);
125125
addWebDriverExtension(profile);
126+
profile.checkForChangesInFrozenPreferences();
126127
profileDir = profile.layoutOnDisk();
127128

128129
ImmutableMap.Builder<String, String> envBuilder = new ImmutableMap.Builder<String, String>()

java/client/test/org/openqa/selenium/firefox/FirefoxProfileTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.firefox;
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2122
import static org.junit.Assert.fail;
2223

2324
import org.junit.Before;
@@ -134,16 +135,17 @@ public void shouldSetDefaultPreferences() throws Exception {
134135
}
135136

136137
@Test
138+
public void shouldAllowSettingFrozenPreferences() throws Exception {
139+
profile.setPreference("network.http.phishy-userpass-length", 1024);
140+
assertPreferenceValueEquals("network.http.phishy-userpass-length", 1024);
141+
}
137142

138-
public void shouldNotResetFrozenPreferences() throws Exception {
139-
try {
140-
profile.setPreference("network.http.phishy-userpass-length", 1024);
141-
fail("Should not be able to reset a frozen preference");
142-
} catch (IllegalArgumentException ex) {
143-
// expected
144-
}
145-
146-
assertPreferenceValueEquals("network.http.phishy-userpass-length", 255);
143+
@Test
144+
public void shouldAllowCheckingForChangesInFrozenPreferences() throws Exception {
145+
profile.setPreference("network.http.phishy-userpass-length", 1024);
146+
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(
147+
() -> profile.checkForChangesInFrozenPreferences()
148+
).withMessageContaining("network.http.phishy-userpass-length");
147149
}
148150

149151
@Test

0 commit comments

Comments
 (0)