Skip to content

Commit

Permalink
[java] Merging three different preference storages into the single one.
Browse files Browse the repository at this point in the history
These three storages were implemented in days of yore when there were no
autoboxing in Java, and today they are archaic
  • Loading branch information
barancev committed Sep 30, 2019
1 parent 1cd3276 commit 4954772
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 96 deletions.
54 changes: 9 additions & 45 deletions java/client/src/org/openqa/selenium/firefox/FirefoxOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.logging.Level;

/**
* Manage firefox specific settings in a way that geckodriver can understand.
Expand All @@ -59,9 +59,7 @@ public class FirefoxOptions extends AbstractDriverOptions<FirefoxOptions> {
public final static String FIREFOX_OPTIONS = "moz:firefoxOptions";

private List<String> args = new ArrayList<>();
private Map<String, Boolean> booleanPrefs = new TreeMap<>();
private Map<String, Integer> intPrefs = new TreeMap<>();
private Map<String, String> stringPrefs = new TreeMap<>();
private Map<String, Object> preferences = new HashMap<>();
private FirefoxDriverLogLevel logLevel;
private Binary binary;
private boolean legacy;
Expand Down Expand Up @@ -116,9 +114,7 @@ public FirefoxOptions(Capabilities source) {
FirefoxOptions that = (FirefoxOptions) raw;

addArguments(that.args);
that.booleanPrefs.forEach(this::addPreference);
that.intPrefs.forEach(this::addPreference);
that.stringPrefs.forEach(this::addPreference);
that.preferences.forEach(this::addPreference);
setLegacy(that.legacy);

if (that.logLevel != null) { setLogLevel(that.logLevel); }
Expand All @@ -140,15 +136,7 @@ public FirefoxOptions(Capabilities source) {
}
if (that.containsKey("prefs")) {
Map<String, Object> prefs = (Map<String, Object>) that.get("prefs");
prefs.forEach((k, v) -> {
if (v instanceof String) {
stringPrefs.put(k, (String) v);
} else if (v instanceof Number) {
intPrefs.put(k, ((Number) v).intValue());
} else if (v instanceof Boolean) {
booleanPrefs.put(k, (Boolean) v);
}
});
preferences.putAll(prefs);
}
if (that.containsKey("binary")) { setBinary((String) that.get("binary")); }
if (that.containsKey("log")) {
Expand Down Expand Up @@ -233,18 +221,8 @@ public FirefoxOptions addArguments(List<String> arguments) {
return this;
}

public FirefoxOptions addPreference(String key, boolean value) {
booleanPrefs.put(requireNonNull(key), value);
return this;
}

public FirefoxOptions addPreference(String key, int value) {
intPrefs.put(requireNonNull(key), value);
return this;
}

public FirefoxOptions addPreference(String key, String value) {
stringPrefs.put(requireNonNull(key), value);
public FirefoxOptions addPreference(String key, Object value) {
preferences.put(requireNonNull(key), value);
return this;
}

Expand Down Expand Up @@ -312,26 +290,14 @@ public Map<String, Object> asMap() {
}

if (profile != null) {
for (Map.Entry<String, Boolean> pref : booleanPrefs.entrySet()) {
profile.setPreference(pref.getKey(), pref.getValue());
}
for (Map.Entry<String, Integer> pref : intPrefs.entrySet()) {
profile.setPreference(pref.getKey(), pref.getValue());
}
for (Map.Entry<String, String> pref : stringPrefs.entrySet()) {
profile.setPreference(pref.getKey(), pref.getValue());
}
preferences.forEach(profile::setPreference);
try {
w3cOptions.put("profile", profile.toJson());
} catch (IOException e) {
throw new WebDriverException(e);
}
} else {
ImmutableMap.Builder<String, Object> allPrefs = ImmutableMap.builder();
allPrefs.putAll(booleanPrefs);
allPrefs.putAll(intPrefs);
allPrefs.putAll(stringPrefs);
w3cOptions.put("prefs", allPrefs.build());
w3cOptions.put("prefs", new HashMap<>(preferences));
}

toReturn.put(FIREFOX_OPTIONS, w3cOptions.build());
Expand All @@ -349,9 +315,7 @@ public FirefoxOptions merge(Capabilities capabilities) {
protected int amendHashCode() {
return Objects.hash(
args,
booleanPrefs,
intPrefs,
stringPrefs,
preferences,
logLevel,
binary,
legacy,
Expand Down
31 changes: 1 addition & 30 deletions java/client/src/org/openqa/selenium/firefox/FirefoxProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,36 +209,7 @@ private String deriveExtensionName(String originalName) {
return name;
}

/**
* Set a preference for this particular profile. The value will be properly quoted before use.
* Note that if a value looks as if it is a quoted string (that is, starts with a quote character
* and ends with one too) an IllegalArgumentException is thrown: Firefox fails to start properly
* when some values are set to this.
*
* @param key The key
* @param value The new value.
*/
public void setPreference(String key, String value) {
additionalPrefs.setPreference(key, value);
}

/**
* Set a preference for this particular profile.
*
* @param key The key
* @param value The new value.
*/
public void setPreference(String key, boolean value) {
additionalPrefs.setPreference(key, value);
}

/**
* Set a preference for this particular profile.
*
* @param key The key
* @param value The new value.
*/
public void setPreference(String key, int value) {
public void setPreference(String key, Object value) {
additionalPrefs.setPreference(key, value);
}

Expand Down
30 changes: 9 additions & 21 deletions java/client/src/org/openqa/selenium/firefox/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,18 @@ private void readDefaultPreferences(Reader defaultsReader) {
}
}

private void setPreference(String key, Object value) {
public void setPreference(String key, Object value) {
if (value instanceof String) {
setPreference(key, (String) value);
if (isStringified((String) value)) {
throw new IllegalArgumentException(
String.format("Preference values must be plain strings: %s: %s",
key, value));
}
allPrefs.put(key, (String) value);
} else if (value instanceof Boolean) {
setPreference(key, ((Boolean) value).booleanValue());
allPrefs.put(key, ((Boolean) value).booleanValue());
} else {
setPreference(key, ((Number) value).intValue());
allPrefs.put(key, ((Number) value).intValue());
}
}

Expand All @@ -134,23 +139,6 @@ private void readPreferences(Reader reader) throws IOException {
}
}

public void setPreference(String key, String value) {
if (isStringified(value)) {
throw new IllegalArgumentException(
String.format("Preference values must be plain strings: %s: %s",
key, value));
}
allPrefs.put(key, value);
}

public void setPreference(String key, boolean value) {
allPrefs.put(key, value);
}

public void setPreference(String key, int value) {
allPrefs.put(key, value);
}

public void addTo(Preferences prefs) {
// TODO(simon): Stop being lazy
prefs.allPrefs.putAll(allPrefs);
Expand Down

0 comments on commit 4954772

Please sign in to comment.