private String getBaseElement(String content) { Matcher matcher = DOCUMENT_SPLIT_PATTERN.matcher(content); assertTrue("Output is not valid HTML.", matcher.matches()); Pattern baseElementPattern = Pattern.compile("^<base href=\"(.*?)\">(?:.*)", Pattern.DOTALL); Matcher baseElementMatcher = baseElementPattern.matcher(matcher.group(HEAD_GROUP)); assertTrue("Base element does not exist at the beginning of the head element.", baseElementMatcher.matches()); return baseElementMatcher.group(1); }
private String getBaseElement(String content) { Matcher matcher = DOCUMENT_SPLIT_PATTERN.matcher(content); assertTrue("Output is not valid HTML.", matcher.matches()); Pattern baseElementPattern = Pattern.compile("^<base href=\"(.*?)\">(?:.*)", Pattern.DOTALL); Matcher baseElementMatcher = baseElementPattern.matcher(matcher.group(HEAD_GROUP)); assertTrue("Base element does not exist at the beginning of the head element.", baseElementMatcher.matches()); return baseElementMatcher.group(1); }
private Set<String> getInjectedScript(String content) { Pattern featurePattern = Pattern.compile("(?:.*)<script src=\"\\/js\\/(.*?)\\?jsload=0\"><\\/script>(?:.*)", Pattern.DOTALL); Matcher matcher = featurePattern.matcher(content); assertTrue("Forced scripts not injected.", matcher.matches()); return Sets.newHashSet(matcher.group(1).split(":")); }
private JSONArray getPreloadedJson(String content) throws JSONException { Pattern preloadPattern = Pattern.compile("(?:.*)gadgets\\.io\\.preloaded_=\\[(.*?)\\];(?:.*)", Pattern.DOTALL); Matcher matcher = preloadPattern.matcher(content); assertTrue("gadgets.io.preloaded not set.", matcher.matches()); return new JSONArray('[' + matcher.group(1) + ']'); }
private JSONArray getPreloadedJson(String content) throws JSONException { Pattern preloadPattern = Pattern.compile("(?:.*)gadgets\\.io\\.preloaded_=\\[(.*?)\\];(?:.*)", Pattern.DOTALL); Matcher matcher = preloadPattern.matcher(content); assertTrue("gadgets.io.preloaded not set.", matcher.matches()); return new JSONArray('[' + matcher.group(1) + ']'); }
private Set<String> getInjectedScript(String content) { Pattern featurePattern = Pattern.compile("(?:.*)<script src=\"\\/js\\/(.*?)\\?jsload=0\"><\\/script>(?:.*)", Pattern.DOTALL); Matcher matcher = featurePattern.matcher(content); assertTrue("Forced scripts not injected.", matcher.matches()); return Sets.newHashSet(matcher.group(1).split(":")); }
private JSONObject getConfigJson(String content) throws JSONException { Pattern prefsPattern = Pattern.compile("(?:.*)gadgets\\.config\\.init\\((.*)\\);(?:.*)", Pattern.DOTALL); Matcher matcher = prefsPattern.matcher(content); assertTrue("gadgets.config.init not invoked.", matcher.matches()); return new JSONObject(matcher.group(1)); }
private JSONObject getConfigJson(String content) throws JSONException { Pattern prefsPattern = Pattern.compile("(?:.*)gadgets\\.config\\.init\\((.*)\\);(?:.*)", Pattern.DOTALL); Matcher matcher = prefsPattern.matcher(content); assertTrue("gadgets.config.init not invoked.", matcher.matches()); return new JSONObject(matcher.group(1)); }
private void assertXhrConfigContains(String message, String expected, String content) throws Exception { // TODO: make this test a little more robust. This check ensures that ordering is not taken // into account during config comparison. String prefix = "gadgets.config.init("; int configIdx = content.indexOf(prefix); assertTrue("gadgets.config.init not found in rewritten content", configIdx != -1); int endIdx = content.indexOf(')', configIdx + prefix.length()); assertTrue("unexpected error, gadgets.config.init not closed", endIdx != -1); String configJson = content.substring(configIdx + prefix.length(), endIdx); JSONObject config = new JSONObject(configJson); JSONObject xhrConfig = config.getJSONObject("shindig.xhrwrapper"); JSONObject expectedJson = new JSONObject(expected); JsonAssert.assertJsonObjectEquals(xhrConfig, expectedJson); }
private void assertXhrConfigContains(String message, String expected, String content) throws Exception { // TODO: make this test a little more robust. This check ensures that ordering is not taken // into account during config comparison. String prefix = "gadgets.config.init("; int configIdx = content.indexOf(prefix); assertTrue("gadgets.config.init not found in rewritten content", configIdx != -1); int endIdx = content.indexOf(')', configIdx + prefix.length()); assertTrue("unexpected error, gadgets.config.init not closed", endIdx != -1); String configJson = content.substring(configIdx + prefix.length(), endIdx); JSONObject config = new JSONObject(configJson); JSONObject xhrConfig = config.getJSONObject("shindig.xhrwrapper"); JSONObject expectedJson = new JSONObject(expected); JsonAssert.assertJsonObjectEquals(xhrConfig, expectedJson); }
@Test public void defaultOutput() throws Exception { Gadget gadget = makeDefaultGadget(); String rewritten = rewrite(gadget, BODY_CONTENT); Matcher matcher = DOCUMENT_SPLIT_PATTERN.matcher(rewritten); assertTrue("Output is not valid HTML.", matcher.matches()); assertTrue("Missing opening html tag", matcher.group(BEFORE_HEAD_GROUP). toLowerCase().contains("<html")); assertTrue("Default CSS missing.", matcher.group(HEAD_GROUP).contains(DEFAULT_CSS)); // Not very accurate -- could have just been user prefs. assertTrue("Default javascript not included.", matcher.group(HEAD_GROUP).contains("<script>")); assertTrue("Original document not preserved.", matcher.group(BODY_GROUP).contains(BODY_CONTENT)); assertTrue("gadgets.util.runOnLoadHandlers not invoked.", matcher.group(BODY_GROUP).contains("gadgets.util.runOnLoadHandlers();")); }
@Test public void createDefaultTitleElement() throws Exception{ Gadget gadget = makeDefaultOpenSocial2Gadget(false); String body = "hello, world."; String doc = new StringBuilder() .append("<html><head>") .append("</head><body>") .append(body) .append("</body></html>") .toString(); String rewritten = rewrite(gadget, doc); assertTrue(rewritten.contains("<title>default title</title>")); }
@Test public void createSpecifiedTitleElement() throws Exception{ Gadget gadget = makeDefaultOpenSocial2GadgetWithTitle(false, "Here is title"); String body = "hello, world."; String doc = new StringBuilder() .append("<html><head>") .append("</head><body>") .append(body) .append("</body></html>") .toString(); String rewritten = rewrite(gadget, doc); assertTrue(rewritten.contains("<title>Here is title</title>")); }
@Test public void createSpecifiedTitleElement() throws Exception{ Gadget gadget = makeDefaultOpenSocial2GadgetWithTitle(false, "Here is title"); String body = "hello, world."; String doc = new StringBuilder() .append("<html><head>") .append("</head><body>") .append(body) .append("</body></html>") .toString(); String rewritten = rewrite(gadget, doc); assertTrue(rewritten.contains("<title>Here is title</title>")); }
@Test public void createDefaultTitleElement() throws Exception{ Gadget gadget = makeDefaultOpenSocial2Gadget(false); String body = "hello, world."; String doc = new StringBuilder() .append("<html><head>") .append("</head><body>") .append(body) .append("</body></html>") .toString(); String rewritten = rewrite(gadget, doc); assertTrue(rewritten.contains("<title>default title</title>")); }
@Test public void featuresInjectedBeforeExistingScript() throws Exception { Gadget gadget = makeDefaultGadget(); String rewritten = rewrite(gadget, "<html><head><script src=\"foo.js\"></script></head><body>hello</body></html>"); Matcher matcher = DOCUMENT_SPLIT_PATTERN.matcher(rewritten); assertTrue("Output is not valid HTML.", matcher.matches()); String headContent = matcher.group(HEAD_GROUP); // Locate user script. int userPosition = headContent.indexOf("<script src=\"foo.js\"></script>"); // Anything else here, we added. int ourPosition = headContent.indexOf("<script>"); // TODO: restore when moved to a non-tag-shifting HTML parser (userPosition == -1 in body) // assertTrue("Injected script must come before user script.", ourPosition < userPosition); }
@Test public void bidiSettings() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Locale language_direction='rtl'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); String rewritten = rewrite(gadget, ""); assertTrue("Bi-directional locale settings not preserved.", rewritten.contains("<body dir=\"rtl\">")); }
@Test public void bidiSettings() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Locale language_direction='rtl'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); String rewritten = rewrite(gadget, ""); assertTrue("Bi-directional locale settings not preserved.", rewritten.contains("<body dir=\"rtl\">")); }
@Test public void inlinedFeaturesWhenNothingForced() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_debug();")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertTrue("Requested scripts not inlined.", rewritten.contains("foo_content();")); }
@Test public void inlinedFeaturesWhenNothingForced() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_debug();")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertTrue("Requested scripts not inlined.", rewritten.contains("foo_content();")); }