// Actually generates the mock gadget. Used for error (null value) tests.
protected Gadget mockGadget(String targetUrl, boolean isTypeUrl, String currentViewStr, String lang,
String country, boolean isDebug, boolean ignoreCache, boolean sanitize, boolean cajoled,
Map<String, String> specPrefs, Map<String, String> inPrefs, boolean needsPrefSubst, List<String> features) {
View currentView = createMock(View.class);
View secondView = createMock(View.class);
ModulePrefs modulePrefs = createMock(ModulePrefs.class);
GadgetSpec spec = createMock(GadgetSpec.class);
GadgetContext context = createMock(GadgetContext.class);
Gadget gadget = createMock(Gadget.class);
// Base URL/view.
Uri targetUri = Uri.parse(targetUrl);
if (isTypeUrl) {
expect(currentView.getType()).andReturn(ContentType.URL).anyTimes();
expect(currentView.getHref()).andReturn(targetUri).anyTimes();
expect(secondView.getType()).andReturn(ContentType.HTML).anyTimes();
expect(spec.getUrl()).andReturn(targetUri).anyTimes();
} else {
expect(currentView.getType()).andReturn(ContentType.HTML).anyTimes();
expect(spec.getUrl()).andReturn(targetUri).anyTimes();
expect(secondView.getType()).andReturn(ContentType.URL).anyTimes();
expect(secondView.getHref()).andReturn(targetUri).anyTimes();
}
expect(currentView.getName()).andReturn(currentViewStr).anyTimes();
expect(secondView.getName()).andReturn(ANOTHER_VIEW).anyTimes();
// Basic context info
Locale locale = new Locale(lang, country);
expect(context.getUrl()).andReturn(SPEC_URI).anyTimes();
expect(context.getContainer()).andReturn(CONTAINER).anyTimes();
expect(context.getLocale()).andReturn(locale).anyTimes();
expect(context.getDebug()).andReturn(isDebug).anyTimes();
expect(context.getIgnoreCache()).andReturn(ignoreCache).anyTimes();
expect(context.getToken()).andReturn(null).anyTimes();
expect(context.getSanitize()).andReturn(sanitize).anyTimes();
expect(context.getCajoled()).andReturn(cajoled).anyTimes();
// All Features (doesn't distinguish between transitive and not)
expect(gadget.getAllFeatures()).andReturn(features).anyTimes();
Map<String, Feature> featureMap = Maps.newLinkedHashMap();
for (String feature : features) {
featureMap.put(feature, null);
}
expect(gadget.getViewFeatures()).andReturn(featureMap).anyTimes();
expect(modulePrefs.getFeatures()).andReturn(featureMap).anyTimes();
// User prefs
Map<String, UserPref> specPrefMap = Maps.newLinkedHashMap();
for (Map.Entry<String, String> specPref : specPrefs.entrySet()) {
UserPref up = createMock(UserPref.class);
expect(up.getName()).andReturn(specPref.getKey()).anyTimes();
expect(up.getDefaultValue()).andReturn(specPref.getValue()).anyTimes();
replay(up);
specPrefMap.put(up.getName(),up);
}
expect(spec.getUserPrefs()).andReturn(specPrefMap).anyTimes();
UserPrefs ctxPrefs = new UserPrefs(inPrefs);
expect(context.getUserPrefs()).andReturn(ctxPrefs).anyTimes();
expect(context.getParameter(Param.REFRESH.getKey())).andReturn(null).anyTimes();
expect(currentView.needsUserPrefSubstitution()).andReturn(needsPrefSubst).anyTimes();
expect(secondView.needsUserPrefSubstitution()).andReturn(!needsPrefSubst).anyTimes();
Map<String, View> views = Maps.newHashMap();
views.put(VIEW, currentView);
views.put(ANOTHER_VIEW, secondView);