@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();")); }
@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 featureConfigurationInjected() 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", "dbg")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); config.data.put(FEATURES_KEY, ImmutableMap.of("foo", "blah")); String rewritten = rewrite(gadget, ""); JSONObject json = getConfigJson(rewritten); assertEquals("blah", json.get("foo")); }
@Test public void featureConfigurationInjected() 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", "dbg")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); config.data.put(FEATURES_KEY, ImmutableMap.of("foo", "blah")); String rewritten = rewrite(gadget, ""); JSONObject json = getConfigJson(rewritten); assertEquals("blah", json.get("foo")); }
@Test public void featureConfigurationInjected() 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", "dbg")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); config.data.put(FEATURES_KEY, ImmutableMap.of("foo", "blah")); String rewritten = rewrite(gadget, ""); JSONObject json = getConfigJson(rewritten); assertEquals("blah", json.get("foo")); }
@Test public void mixedExternalAndInline() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; final Set<String> libs = ImmutableSet.of("bar", "baz"); GadgetContext context = new GadgetContext() { @Override public String getParameter(String name) { if (name.equals("libs")) { return Joiner.on(':').join(libs); } return null; } }; Gadget gadget = makeGadgetWithSpec(gadgetXml).setContext(context); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_debug();")), libs, ImmutableList.of(inline("bar-c", "bar-d"), inline("baz-c", "baz-d"))); String rewritten = rewrite(gadget, ""); Set<String> actual = getInjectedScript(rewritten); Set<String> expected = ImmutableSortedSet.of("bar", "baz"); assertEquals(expected, actual); assertTrue("Requested scripts not inlined.", rewritten.contains("foo_content();")); }
@Test public void mixedExternalAndInline() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; final Set<String> libs = ImmutableSet.of("bar", "baz"); GadgetContext context = new GadgetContext() { @Override public String getParameter(String name) { if (name.equals("libs")) { return Join.join(":", libs); } return null; } }; Gadget gadget = makeGadgetWithSpec(gadgetXml).setContext(context); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_debug();")), libs, ImmutableList.of(inline("bar-c", "bar-d"), inline("baz-c", "baz-d"))); String rewritten = rewrite(gadget, ""); Set<String> actual = getInjectedScript(rewritten); Set<String> expected = ImmutableSortedSet.of("bar", "baz"); assertEquals(expected, actual); assertTrue("Requested scripts not inlined.", rewritten.contains("foo_content();")); }
@Test public void featuresDeclaredBeforeUsed() 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, BODY_CONTENT); 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 declaredPosition = headContent.indexOf("foo_content();"); assertTrue(declaredPosition >= 0); // Anything else here, we added. int usedPosition = headContent.indexOf("gadgets.Prefs.setMessages_"); assertTrue(usedPosition >= 0); assertTrue("Inline JS needs to exist before it is used.", declaredPosition < usedPosition); }
@Test public void featuresInjectedWhenAdded() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.addFeature("foo"); // add non existing feature, gadget.addFeature("do-not-exists"); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_dbg();")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertTrue("Added script not inlined.", rewritten.contains("foo_content();")); }
@Test public void featuresDeclaredBeforeUsed() 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, BODY_CONTENT); 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 declaredPosition = headContent.indexOf("foo_content();"); assertTrue(declaredPosition >= 0); // Anything else here, we added. int usedPosition = headContent.indexOf("gadgets.Prefs.setMessages_"); assertTrue(usedPosition >= 0); assertTrue("Inline JS needs to exist before it is used.", declaredPosition < usedPosition); }
@Test public void featuresDeclaredBeforeUsed() 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, BODY_CONTENT); 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 declaredPosition = headContent.indexOf("foo_content();"); assertTrue(declaredPosition >= 0); // Anything else here, we added. int usedPosition = headContent.indexOf("gadgets.Prefs.setMessages_"); assertTrue(usedPosition >= 0); assertTrue("Inline JS needs to exist before it is used.", declaredPosition < usedPosition); }
@Test public void featuresInjectedWhenAdded() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.addFeature("foo"); // add non existing feature, gadget.addFeature("do-not-exists"); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_dbg();")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertTrue("Added script not inlined.", rewritten.contains("foo_content();")); }
@Test public void forcedFeaturesInjectedExternal() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; final Set<String> libs = ImmutableSortedSet.of("foo", "bar", "baz"); GadgetContext context = new GadgetContext() { @Override public String getParameter(String name) { if (name.equals("libs")) { return Joiner.on(':').join(libs); } return null; } }; Gadget gadget = makeGadgetWithSpec(gadgetXml).setContext(context); FeatureResource fooResource = inline("foo-content", "foo-debug"); expectFeatureCalls(gadget, ImmutableList.of(fooResource), libs, ImmutableList.of(fooResource, inline("bar-c", "bar-d"), inline("baz-c", "baz-d"))); String rewritten = rewrite(gadget, ""); Set<String> actual = getInjectedScript(rewritten); Set<String> expected = ImmutableSortedSet.of("foo", "bar", "baz"); assertEquals(expected, actual); }
@Test public void forcedFeaturesInjectedExternal() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; final Set<String> libs = ImmutableSortedSet.of("foo", "bar", "baz"); GadgetContext context = new GadgetContext() { @Override public String getParameter(String name) { if (name.equals("libs")) { return Joiner.on(':').join(libs); } return null; } }; Gadget gadget = makeGadgetWithSpec(gadgetXml).setContext(context); FeatureResource fooResource = inline("foo-content", "foo-debug"); expectFeatureCalls(gadget, ImmutableList.of(fooResource), libs, ImmutableList.of(fooResource, inline("bar-c", "bar-d"), inline("baz-c", "baz-d"))); String rewritten = rewrite(gadget, ""); Set<String> actual = getInjectedScript(rewritten); Set<String> expected = ImmutableSortedSet.of("foo", "bar", "baz"); assertEquals(expected, actual); }
@Test public void featuresInjectedWhenAdded() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.addFeature("foo"); // add non existing feature, gadget.addFeature("do-not-exists"); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_dbg();")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertTrue("Added script not inlined.", rewritten.contains("foo_content();")); }