@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 featuresNotInjectedWhenRemoved() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.removeFeature("foo"); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertFalse("Removed script still 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 featuresNotInjectedWhenRemoved() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.removeFeature("foo"); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertFalse("Removed script still 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 featuresNotInjectedWhenRemoved() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.removeFeature("foo"); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertFalse("Removed script still 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")); }
private Gadget makeGadgetWithSpec(String gadgetXml) throws GadgetException { GadgetSpec spec = new GadgetSpec(SPEC_URL, gadgetXml); Gadget gadget = new Gadget() .setContext(context) .setPreloads(ImmutableList.<PreloadedData>of()) .setSpec(spec) .setGadgetFeatureRegistry(featureRegistry); // Convenience: by default expect no features requested, by gadget or extern. // expectFeatureCalls(...) resets featureRegistry if called again. expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); return gadget; }
@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 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();")); }
private Gadget makeGadgetWithSpec(String gadgetXml) throws GadgetException { GadgetSpec spec = new GadgetSpec(SPEC_URL, gadgetXml); Gadget gadget = new Gadget() .setContext(context) .setPreloads(ImmutableList.<PreloadedData>of()) .setSpec(spec) .setCurrentView(spec.getView(GadgetSpec.DEFAULT_VIEW)) .setGadgetFeatureRegistry(featureRegistry); // Convenience: by default expect no features requested, by gadget or extern. // expectFeatureCalls(...) resets featureRegistry if called again. expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); //Convenience: by default expect that the gadget is allowed to render reset(gadgetAdminStore); expect(gadgetAdminStore.checkFeatureAdminInfo(isA(Gadget.class))).andReturn(true); expect(gadgetAdminStore.isAllowedFeature(isA(Feature.class), isA(Gadget.class))) .andReturn(true).anyTimes(); replay(gadgetAdminStore); return gadget; }
private Gadget makeGadgetWithSpec(String gadgetXml) throws GadgetException { GadgetSpec spec = new GadgetSpec(SPEC_URL, gadgetXml); Gadget gadget = new Gadget() .setContext(context) .setPreloads(ImmutableList.<PreloadedData>of()) .setSpec(spec) .setCurrentView(spec.getView(GadgetSpec.DEFAULT_VIEW)) .setGadgetFeatureRegistry(featureRegistry); // Convenience: by default expect no features requested, by gadget or extern. // expectFeatureCalls(...) resets featureRegistry if called again. expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); //Convenience: by default expect that the gadget is allowed to render reset(gadgetAdminStore); expect(gadgetAdminStore.checkFeatureAdminInfo(isA(Gadget.class))).andReturn(true); expect(gadgetAdminStore.isAllowedFeature(isA(Feature.class), isA(Gadget.class))) .andReturn(true).anyTimes(); replay(gadgetAdminStore); return gadget; }
@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 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); }