Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JBIDE-13338 BrowserSim: integrate weinre functionality to BrowserSim #33

Merged
merged 2 commits into from Feb 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -27,15 +27,19 @@ public class DevicesList extends Observable {
private Boolean truncateWindow;
private Point location;
private String screenshotsFolder;
private String weinreScriptUrl;
private String weinreClientUrl;

public DevicesList(List<Device> devices, int selectedDeviceIndex, boolean useSkins, Boolean truncateWindow,
Point location, String screenshotsFolder) {
Point location, String screenshotsFolder, String weinreScriptUrl, String weinreClientUrl) {
this.devices = devices;
this.selectedDeviceIndex = selectedDeviceIndex;
this.useSkins = useSkins;
this.truncateWindow = truncateWindow;
this.location = location;
this.screenshotsFolder = screenshotsFolder;
this.weinreScriptUrl = weinreScriptUrl;
this.weinreClientUrl = weinreClientUrl;
}

public List<Device> getDevices() {
Expand Down Expand Up @@ -93,4 +97,20 @@ public void setScreenshotsFolder(String screenshotsFolder) {
this.screenshotsFolder = screenshotsFolder;
setChanged();
}

public String getWeinreScriptUrl() {
return weinreScriptUrl;
}

public void setWeinreScriptUrl(String weinreServer) {
this.weinreScriptUrl = weinreServer;
}

public String getWeinreClientUrl() {
return weinreClientUrl;
}

public void setWeinreClientUrl(String weinreClientUrl) {
this.weinreClientUrl = weinreClientUrl;
}
}
Expand Up @@ -39,7 +39,9 @@ public class DevicesListStorage {
private static final String STANDALONE_PREFERENCES_FOLDER = ".browsersim";
private static final String USER_PREFERENCES_FOLDER = "org.jboss.tools.vpe.browsersim";
private static final String PREFERENCES_FILE = "devices.cfg";
private static final int CURRENT_CONFIG_VERSION = 8;
private static final String DEFAULT_WEINRE_SCRIPT_URL = "http://debug.phonegap.com/target/target-script-min.js";
private static final String DEFAULT_WEINRE_CLIENT_URL = "http://debug.phonegap.com/client/";
private static final int CURRENT_CONFIG_VERSION = 9;

public static void saveUserDefinedDevicesList(DevicesList devicesList, Point location) {
File configFolder = new File(getConfigFolderPath());
Expand Down Expand Up @@ -81,7 +83,8 @@ public static DevicesList loadDefaultDevicesList() {
Device device = new Device("Default", 1024, 768, 1.0, null, null);
List<Device> devices = new ArrayList<Device>();
devices.add(device);
devicesList = new DevicesList(devices, 0, true, null, null, getDefaultScreenshotsFolderPath());
devicesList = new DevicesList(devices, 0, true, null, null, getDefaultScreenshotsFolderPath(),
getDefaultWeinreScriptUrl(), getDefaultWeinreClientUrl());
}

return devicesList;
Expand All @@ -95,6 +98,14 @@ private static String getDefaultScreenshotsFolderPath() {
return USER_HOME;
}

private static String getDefaultWeinreScriptUrl() {
return DEFAULT_WEINRE_SCRIPT_URL;
}

private static String getDefaultWeinreClientUrl() {
return DEFAULT_WEINRE_CLIENT_URL;
}

private static void saveDevicesList(DevicesList devicesList, File file, Point location) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));

Expand All @@ -106,6 +117,8 @@ private static void saveDevicesList(DevicesList devicesList, File file, Point lo
String truncateWindowString = truncateWindow == null ? "" : truncateWindow.toString();
writer.write("TruncateWindow=" + truncateWindowString + "\n");
writer.write("ScreenshotsFolder=" + devicesList.getScreenshotsFolder() + "\n");
writer.write("WeinreScriptUrl=" + devicesList.getWeinreScriptUrl() + "\n");
writer.write("WeinreClientUrl=" + devicesList.getWeinreClientUrl() + "\n");

for (Device device : devicesList.getDevices()) {
writer.write( encode(device.getName() ));
Expand Down Expand Up @@ -143,6 +156,8 @@ private static DevicesList loadDevicesList(InputStream inputStream) throws IOExc
boolean useSkins = true;
Boolean truncateWindow = true;
String screenshotsFolder = "";
String weinreScriptUrl = "";
String weinreClientUrl = "";
try {
String nextLine;
int configVersion = 0;
Expand Down Expand Up @@ -203,6 +218,30 @@ private static DevicesList loadDevicesList(InputStream inputStream) throws IOExc
}
}

if ((nextLine = reader.readLine()) != null) {
Pattern pattern = Pattern.compile("WeinreScriptUrl=(.*)");
Matcher matcher = pattern.matcher(nextLine);
if (matcher.matches()) {
if ( "".equals(matcher.group(1)) ) {
weinreScriptUrl = getDefaultWeinreScriptUrl();
} else {
weinreScriptUrl = matcher.group(1);
}
}
}

if ((nextLine = reader.readLine()) != null) {
Pattern pattern = Pattern.compile("WeinreClientUrl=(.*)");
Matcher matcher = pattern.matcher(nextLine);
if (matcher.matches()) {
if ( "".equals(matcher.group(1)) ) {
weinreClientUrl = getDefaultWeinreClientUrl();
} else {
weinreClientUrl = matcher.group(1);
}
}
}

Pattern devicePattern = Pattern.compile("^(.*)\\t([0-9]+)\\t([0-9]+)\\t([0-9]*\\.?[0-9]*)\\t(.+)?\\t(.+)?$");

devices = new ArrayList<Device>();
Expand Down Expand Up @@ -239,7 +278,8 @@ private static DevicesList loadDevicesList(InputStream inputStream) throws IOExc
if (devices == null || devices.size() <= selectedDeviceIndex) {
return null;
} else {
return new DevicesList(devices, selectedDeviceIndex, useSkins, truncateWindow, location, screenshotsFolder);
return new DevicesList(devices, selectedDeviceIndex, useSkins, truncateWindow, location, screenshotsFolder,
weinreScriptUrl, weinreClientUrl);
}
}

Expand Down
@@ -1,9 +1,11 @@
ConfigVersion=8
ConfigVersion=9
Location=
SelectedDeviceIndex=4
UseSkins=true
TruncateWindow=
ScreenshotsFolder=
WeinreScriptUrl=
WeinreClientUrl=
Desktop (Default User-Agent) 1024 768 1
Apple iPad 2 768 1024 1 Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5 iPhone 4
Apple iPhone 3 320 480 1 Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7 iPhone 3
Expand All @@ -14,4 +16,4 @@ Samsung Galaxy S II 480 800 1.5 Mozilla/5.0 (Linux; U; Android 2.3; en-us; GT-I9
Samsung Galaxy Nexus 720 1280 2 Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML like Gecko) Version/4.0 Mobile Safari/534.30 Galaxy Note II
Galaxy Note II 800 1280 2 Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19 Galaxy Note II
Galaxy S III 720 1280 2 Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19 Galaxy S III
Samsung Galaxy Tab 10.1 800 1280 1.5 Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; GT-P7100 Build/HRI83) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 MobileSafari/534.13 Galaxy Note II
Samsung Galaxy Tab 10.1 800 1280 1.5 Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; GT-P7100 Build/HRI83) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 MobileSafari/534.13 Galaxy Note II
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.UUID;

import javax.xml.bind.DatatypeConverter;

Expand Down Expand Up @@ -50,6 +51,7 @@
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
Expand Down Expand Up @@ -454,7 +456,7 @@ public void menuShown(MenuEvent e) {
addDevicesMenuItems(contextMenu);
addUseSkinsItem(contextMenu);
addPreferencesItem(contextMenu);

new MenuItem(contextMenu, SWT.BAR);
addTurnMenuItems(contextMenu);

Expand Down Expand Up @@ -524,6 +526,7 @@ private void addFileMenuItems(Menu menu) {

private void addToolsMenuItems(Menu menu) {
addFireBugLiteItem(menu);
addWeinreItem(menu);
addScreenshotMenuItem(menu);
}

Expand Down Expand Up @@ -601,6 +604,40 @@ public void widgetSelected(SelectionEvent e) {
});
}

public void addWeinreItem(Menu menu) {
MenuItem weinre = new MenuItem(menu, SWT.PUSH);
weinre.setText(Messages.BrowserSim_WEINRE);
weinre.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String url = (String) skin.getBrowser().evaluate("if(window.WeinreServerURL && window.WeinreServerId) {return window.WeinreServerURL + 'client/#' + window.WeinreServerId} else {return null}");
if (url == null) {
String id = UUID.randomUUID().toString();
skin.getBrowser().execute("var head = document.head;"
+ "var script = document.createElement('script');"
+ "head.appendChild(script);"
+ "script.src='" + devicesListHolder.getDevicesList().getWeinreScriptUrl() + "#" + id + "'");

url = devicesListHolder.getDevicesList().getWeinreClientUrl() + "#" + id;
}

Display display = skin.getBrowser().getDisplay();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Weinre Inspector");
final Browser browser;
try {
browser = new Browser(shell, SWT.WEBKIT);
} catch (SWTError e2) {
System.out.println("Could not instantiate Browser: " + e2.getMessage());
display.dispose();
return;
}
shell.open();
browser.setUrl(url);
}
});
}

public void addAboutItem(Menu menu) {
MenuItem about = new MenuItem(menu, SWT.PUSH);
about.setText(Messages.BrowserSim_ABOUT);
Expand Down Expand Up @@ -959,7 +996,6 @@ else if (skin != null && skin.getShell() != null) {
}
return new Shell();
}


private void addMacOsMenuApplicationHandler(CocoaUIEnhancer enhancer) {

Expand Down
Expand Up @@ -271,6 +271,23 @@ public void handleEvent(Event event) {
}
});

Group weinreGroup = new Group(shell, SWT.NONE);
weinreGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
weinreGroup.setText("Weinre");
weinreGroup.setLayout(new GridLayout(2, false));

Label weinreScriptUrlLabel = new Label(weinreGroup, SWT.NONE);
weinreScriptUrlLabel.setText("Script URL:");
final Text weinreScriptUrlText = new Text(weinreGroup, SWT.BORDER);
weinreScriptUrlText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
weinreScriptUrlText.setText(oldDevicesList.getWeinreScriptUrl());

Label weinreClientUrlLabel = new Label(weinreGroup, SWT.NONE);
weinreClientUrlLabel.setText("Client URL:");
final Text weinreClientUrlText = new Text(weinreGroup, SWT.BORDER);
weinreClientUrlText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
weinreClientUrlText.setText(oldDevicesList.getWeinreClientUrl());

Composite compositeOkCancel = new Composite(shell, SWT.NONE);
compositeOkCancel.setLayout(new GridLayout(2, true));
compositeOkCancel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 2, 1));
Expand All @@ -283,10 +300,12 @@ public void handleEvent(Event event) {
public void widgetSelected(SelectionEvent e) {
DevicesList defaultDevicesList = DevicesListStorage.loadDefaultDevicesList();
devices = defaultDevicesList.getDevices();
selectedDeviceIndex = defaultDevicesList.getSelectedDeviceIndex();
useSkins = defaultDevicesList.getUseSkins();
selectedDeviceIndex = defaultDevicesList.getSelectedDeviceIndex();
useSkins = defaultDevicesList.getUseSkins();
truncateWindow = defaultDevicesList.getTruncateWindow();
screenshotsPath.setText(defaultDevicesList.getScreenshotsFolder());
weinreScriptUrlText.setText(defaultDevicesList.getWeinreScriptUrl());
weinreClientUrlText.setText(defaultDevicesList.getWeinreClientUrl());
updateDevices();
}
});
Expand All @@ -295,10 +314,10 @@ public void widgetSelected(SelectionEvent e) {
buttonOk.setText(Messages.ManageDevicesDialog_OK);
buttonOk.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
shell.setDefaultButton(buttonOk);
buttonOk.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
buttonOk.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resultDevicesList = new DevicesList(devices, selectedDeviceIndex, useSkins, truncateWindow,
oldDevicesList.getLocation(), screenshotsPath.getText());
oldDevicesList.getLocation(), screenshotsPath.getText(), weinreScriptUrlText.getText(), weinreClientUrlText.getText());
shell.close();
}
});
Expand Down
Expand Up @@ -37,6 +37,7 @@ public class Messages {
public static String BrowserSim_TURN_RIGHT;
public static String BrowserSim_USE_SKINS;
public static String BrowserSim_VIEW_PAGE_SOURCE;
public static String BrowserSim_WEINRE;
public static String EditDeviceDialog_CANCEL;
public static String EditDeviceDialog_EDIT_DEVICE;
public static String EditDeviceDialog_HEIGHT;
Expand Down
Expand Up @@ -16,6 +16,7 @@ BrowserSim_TURN_LEFT=Turn Left
BrowserSim_TURN_RIGHT=Turn Right
BrowserSim_USE_SKINS=Use Skins
BrowserSim_VIEW_PAGE_SOURCE=View page source
BrowserSim_WEINRE=Weinre
EditDeviceDialog_CANCEL=Cancel
EditDeviceDialog_EDIT_DEVICE=Edit Device
EditDeviceDialog_HEIGHT=Height:
Expand Down