Skip to content

Commit

Permalink
making the server side content l10n work.
Browse files Browse the repository at this point in the history
  • Loading branch information
freynaud committed May 20, 2012
1 parent e66eac3 commit 2e1a034
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,34 @@
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.uiautomation.ios.exceptions.InvalidCriteriaException;

public abstract class AbstractCriteria implements Criteria {



@SuppressWarnings("unchecked")
public static <T extends Criteria> T parse(JSONObject serialized) throws Exception {
return parse(serialized, null);
}

@SuppressWarnings("unchecked")
public static <T extends Criteria> T parse(JSONObject serialized, CriteriaDecorator decorator)
throws Exception {
int nbKeys = serialized.length();
switch (nbKeys) {
case KEYS_IN_COMPOSED_CRITERIA:
String key = (String) serialized.keys().next();
CompositionType type = CompositionType.valueOf(key);
return (T) buildComposedCriteria(serialized, type);
return (T) buildComposedCriteria(serialized, type, decorator);
case KEYS_IN_PROPERTY_CRITERIA:
String method = serialized.getString("method");
String tmp =
method.substring(0, 1).toUpperCase() + method.toLowerCase().substring(1) + "Criteria";
String clazz = AbstractCriteria.class.getPackage().getName() + "." + tmp;
Class<? extends PropertyEqualCriteria> c =
(Class<? extends PropertyEqualCriteria>) Class.forName(clazz);
return (T) buildPropertyBaseCriteria(serialized, c);
return (T) buildPropertyBaseCriteria(serialized, c, decorator);
default:
throw new InvalidCriteriaException("can't find the type : " + serialized.toString());
}
Expand All @@ -52,8 +56,8 @@ public static <T extends Criteria> T parse(JSONObject serialized) throws Excepti
private static final int KEYS_IN_PROPERTY_CRITERIA = 3;


private static ComposedCriteria buildComposedCriteria(JSONObject serialized, CompositionType type)
throws Exception {
private static ComposedCriteria buildComposedCriteria(JSONObject serialized,
CompositionType type, CriteriaDecorator decorator) throws Exception {
JSONArray array = serialized.getJSONArray(type.toString());
if (type == CompositionType.NOT && array.length() != 1) {
throw new InvalidCriteriaException("negative criteria apply to 1 criteria only " + serialized);
Expand All @@ -62,7 +66,7 @@ private static ComposedCriteria buildComposedCriteria(JSONObject serialized, Com

for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
Criteria crit = parse(c);
Criteria crit = parse(c, decorator);
criterias.add(crit);
}

Expand All @@ -71,11 +75,13 @@ private static ComposedCriteria buildComposedCriteria(JSONObject serialized, Com

Constructor<?> c = type.getAssociatedClass().getConstructor(argsClass);
ComposedCriteria crit = (ComposedCriteria) c.newInstance(args);
crit.addDecorator(decorator);
crit.decorate();
return crit;
}

private static PropertyEqualCriteria buildPropertyBaseCriteria(JSONObject serialized,
Class<? extends PropertyEqualCriteria> clazz) throws Exception {
Class<? extends PropertyEqualCriteria> clazz, CriteriaDecorator decorator) throws Exception {
String expected = serialized.getString("expected");
String strategy = serialized.getString("strategy");

Expand All @@ -84,6 +90,8 @@ private static PropertyEqualCriteria buildPropertyBaseCriteria(JSONObject serial

Constructor<?> c = clazz.getConstructor(argsClass);
PropertyEqualCriteria crit = (PropertyEqualCriteria) c.newInstance(args);
crit.addDecorator(decorator);
crit.decorate();
return crit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public abstract class DecorableCriteria extends AbstractCriteria {

@Override
public void addDecorator(CriteriaDecorator decorator) {
decorators.add(decorator);
if (decorator != null) {
decorators.add(decorator);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.uiautomation.ios.UIAModels.predicate.AbstractCriteria;
import org.uiautomation.ios.UIAModels.predicate.Criteria;
import org.uiautomation.ios.UIAModels.predicate.CriteriaDecorator;
import org.uiautomation.ios.communication.WebDriverLikeCommand;
import org.uiautomation.ios.communication.WebDriverLikeRequest;
import org.uiautomation.ios.server.application.IOSApplication;
import org.uiautomation.ios.server.application.ServerSideL10NDecorator;
import org.uiautomation.ios.server.command.Handler;
import org.uiautomation.ios.server.command.impl.CustomUIAScriptHandler;
import org.uiautomation.ios.server.command.impl.DefaultUIAScriptHandler;
Expand Down Expand Up @@ -119,13 +124,13 @@ public static CommandMapping get(WebDriverLikeCommand wdlc) {
throw new RuntimeException("not mapped : " + wdlc);
}

public String jsMethod(JSONObject payload) {
public String jsMethod(JSONObject payload,IOSApplication aut) {
if (jsMethod == null) {
throw new RuntimeException("JS method missing from mapping.");
}

if (payload != null) {
payload = serverSidePayloadChanges(payload);
payload = serverSidePayloadChanges(payload,aut);
String res = jsMethod;
Iterator<String> iter = payload.keys();
while (iter.hasNext()) {
Expand All @@ -143,13 +148,18 @@ public String jsMethod(JSONObject payload) {
}


private CriteriaDecorator getDecorator(IOSApplication aut){
ServerSideL10NDecorator decorator = new ServerSideL10NDecorator(aut);
return decorator;
}

private JSONObject serverSidePayloadChanges(JSONObject payload) {
System.out.println(payload.toString());
if (payload.has("stategy")){
private JSONObject serverSidePayloadChanges(JSONObject payload, IOSApplication aut) {
if (payload.has("criteria")){
try {
System.out.println("got "+payload.getString("strategy"));
} catch (JSONException e) {
JSONObject json = payload.getJSONObject("criteria");
Criteria decorated = AbstractCriteria.parse(json,getDecorator(aut));
payload.put("criteria", decorated.getJSONRepresentation().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Localizable createFromLegacyName(String appleMapping) throws IOSAu

public static Localizable createFromNewName(String name) throws IOSAutomationException {
for (Localizable l10n : Localizable.values()) {
if (l10n.newFormat.equals(name)) {
if (l10n.newFormat.equals(name) || l10n.toString().equals(name)) {
return l10n;
}
}
Expand All @@ -64,7 +64,7 @@ public static Localizable createFromNewName(String name) throws IOSAutomationExc
// ISO-639-1 (two-letter) or ISO-639-2 (three-letter)
public static boolean isNewName(String name) {
for (Localizable l10n : Localizable.values()) {
if (l10n.newFormat.equals(name)) {
if (l10n.newFormat.equals(name) || l10n.toString().equals(name)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.uiautomation.ios.server.application;

import org.uiautomation.ios.UIAModels.predicate.Criteria;
import org.uiautomation.ios.UIAModels.predicate.CriteriaDecorator;
import org.uiautomation.ios.UIAModels.predicate.MatchingStrategy;
import org.uiautomation.ios.UIAModels.predicate.PropertyEqualCriteria;
import org.uiautomation.ios.exceptions.InvalidCriteriaException;

public class ServerSideL10NDecorator implements CriteriaDecorator {

private final IOSApplication app;

public ServerSideL10NDecorator(IOSApplication app) {
this.app = app;
}

@Override
public void decorate(Criteria c) {
if (!isElligible(c)) {
return;
}
PropertyEqualCriteria criteria = (PropertyEqualCriteria) c;
String oldValue = criteria.getValue();
LanguageDictionary dict = app.getDictionary(app.getCurrentLanguage());
String newValue = dict.getContentForKey(oldValue);
if (newValue == null) {
throw new InvalidCriteriaException("No entry for key " + oldValue + " in dictionary for "
+ app.getCurrentLanguage());
}

criteria.setValue(LanguageDictionary.getRegexPattern(newValue));
criteria.setStrategy(MatchingStrategy.regex);
}


private boolean isElligible(Criteria c) {
if (c instanceof PropertyEqualCriteria) {
PropertyEqualCriteria crit = (PropertyEqualCriteria) c;
return crit.getMatchingStrategy() == MatchingStrategy.serverL10N;
}
return false;
}


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.uiautomation.ios.server.application;

import org.uiautomation.ios.UIAModels.predicate.Criteria;
import org.uiautomation.ios.UIAModels.predicate.CriteriaDecorator;
import org.uiautomation.ios.UIAModels.predicate.MatchingStrategy;
import org.uiautomation.ios.UIAModels.predicate.NameCriteria;
import org.uiautomation.ios.UIAModels.predicate.PropertyEqualCriteria;
import org.uiautomation.ios.exceptions.InvalidCriteriaException;

public class ServerSideL10NFactory {

Expand All @@ -21,44 +17,4 @@ public NameCriteria nameCriteria(String serverKey) {
criteria.decorate();
return criteria;
}



class ServerSideL10NDecorator implements CriteriaDecorator {

private final IOSApplication app;

public ServerSideL10NDecorator(IOSApplication app) {
this.app = app;
}

@Override
public void decorate(Criteria c) {
if (!isElligible(c)) {
return;
}
PropertyEqualCriteria criteria = (PropertyEqualCriteria) c;
String oldValue = criteria.getValue();
LanguageDictionary dict = app.getDictionary(app.getCurrentLanguage());
String newValue = dict.getContentForKey(oldValue);
if (newValue == null) {
throw new InvalidCriteriaException("No entry for key " + oldValue + " in dictionary for "
+ app.getCurrentLanguage());
}

criteria.setValue(LanguageDictionary.getRegexPattern(newValue));
criteria.setStrategy(MatchingStrategy.regex);
}


private boolean isElligible(Criteria c) {
if (c instanceof PropertyEqualCriteria) {
PropertyEqualCriteria crit = (PropertyEqualCriteria) c;
return crit.getMatchingStrategy() == MatchingStrategy.serverL10N;
}
return false;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.uiautomation.ios.communication.WebDriverLikeCommand;
import org.uiautomation.ios.communication.WebDriverLikeRequest;
import org.uiautomation.ios.server.CommandMapping;
import org.uiautomation.ios.server.application.IOSApplication;
import org.uiautomation.ios.server.command.UIAScriptHandler;
import org.uiautomation.ios.server.instruments.SessionsManager;

Expand Down Expand Up @@ -67,6 +68,10 @@ public boolean isAUIASimpleObject(Class<?> type) {
return false;
}

private IOSApplication getAUT() {
return getSessionsManager().getCurrentApplication();
}

// TODO freynaud extract.
private static final String stringTemplate =
"var target = UIAutomation.cache.get(:reference);var myStringResult = target:jsMethod ;UIAutomation.createJSONResponse(':sessionId',0,myStringResult)";
Expand All @@ -80,7 +85,7 @@ public boolean isAUIASimpleObject(Class<?> type) {
private String jsForRemoteObject(JSONObject payload) {
WebDriverLikeCommand command = getRequest().getGenericCommand();
CommandMapping mapping = CommandMapping.get(command);
String method = mapping.jsMethod(payload);
String method = mapping.jsMethod(payload, getAUT());

String js =
objectTemplate.replace(":jsMethod", method)
Expand All @@ -96,7 +101,7 @@ private String jsForRemoteObject(JSONObject payload) {
private String jsForString(JSONObject payload) {
WebDriverLikeCommand command = getRequest().getGenericCommand();
CommandMapping mapping = CommandMapping.get(command);
String method = mapping.jsMethod(payload);
String method = mapping.jsMethod(payload, getAUT());

String js =
stringTemplate.replace(":jsMethod", method)
Expand All @@ -116,7 +121,7 @@ private String jsForString(JSONObject payload) {
private String jsForVoid(JSONObject payload) {
WebDriverLikeCommand command = getRequest().getGenericCommand();
CommandMapping mapping = CommandMapping.get(command);
String method = mapping.jsMethod(payload);
String method = mapping.jsMethod(payload, getAUT());

String js =
voidTemplate.replace(":jsMethod", method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
import org.uiautomation.ios.communication.IOSDevice;
import org.uiautomation.ios.communication.Session;
import org.uiautomation.ios.exceptions.IOSAutomationSetupException;
import org.uiautomation.ios.server.application.IOSApplication;
import org.uiautomation.ios.server.application.LanguageDictionary;
import org.uiautomation.ios.server.application.Localizable;


public class SessionsManager {

private Session currentSession;
private IOSApplication currentApplication;



private InstrumentsManager instrumentsManager;

public SessionsManager() throws IOSAutomationSetupException {
Expand All @@ -43,6 +50,12 @@ public void run() {

public void createSession(IOSCapabilities cap) throws IOSAutomationSetupException {
IOSCapabilities v = completeWithDefaults(cap);

Localizable language = new LanguageDictionary(v.getLanguage()).getLanguage();
String aut = v.getApplication();
currentApplication = new IOSApplication(language, aut);
currentApplication.loadAllContent();

createSession(v.getDevice(), v.getSDKVersion(), v.getLocale(), v.getLanguage(),
new File(v.getApplication()), v.isTimeHack(), v.getExtraSwitches());
}
Expand Down Expand Up @@ -101,6 +114,9 @@ public void deleteSession() throws IOSAutomationSetupException {
currentSession = null;
}

public IOSApplication getCurrentApplication() {
return currentApplication;
}

public void forceStop() {
instrumentsManager.forceStop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.uiautomation.ios.communication.WebDriverLikeRequest;
import org.uiautomation.ios.communication.WebDriverLikeResponse;
import org.uiautomation.ios.server.CommandMapping;
import org.uiautomation.ios.server.application.IOSApplication;
import org.uiautomation.ios.server.command.Handler;

public class IOSServlet extends UIAScriptProxyBasedServlet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private File getContentAndSettingsFolder() {
private File getGlobalPreferenceFile() {
File folder = new File(contentAndSettingsFolder + "/Library/Preferences/");
File global = new File(folder, ".GlobalPreferences.plist");
System.out.println(global);
return global;
}

Expand Down
Loading

0 comments on commit 2e1a034

Please sign in to comment.