diff options
5 files changed, 86 insertions, 27 deletions
diff --git a/main/src/cgeo/geocaching/AbstractLoggingActivity.java b/main/src/cgeo/geocaching/AbstractLoggingActivity.java index 1868491..bca5db1 100644 --- a/main/src/cgeo/geocaching/AbstractLoggingActivity.java +++ b/main/src/cgeo/geocaching/AbstractLoggingActivity.java @@ -23,7 +23,7 @@ public abstract class AbstractLoggingActivity extends AbstractActionBarActivity getMenuInflater().inflate(R.menu.abstract_logging_activity, menu); final SubMenu menuLog = menu.findItem(R.id.menu_templates).getSubMenu(); - for (final LogTemplate template : LogTemplateProvider.getTemplates()) { + for (final LogTemplate template : LogTemplateProvider.getTemplatesWithSignature()) { menuLog.add(0, template.getItemId(), 0, template.getResourceId()); } @@ -36,7 +36,7 @@ public abstract class AbstractLoggingActivity extends AbstractActionBarActivity } @Override - public boolean onPrepareOptionsMenu(Menu menu) { + public boolean onPrepareOptionsMenu(final Menu menu) { boolean smileyVisible = false; final Geocache cache = getLogContext().getCache(); if (cache != null && ConnectorFactory.getConnector(cache).equals(GCConnector.getInstance())) { @@ -53,7 +53,7 @@ public abstract class AbstractLoggingActivity extends AbstractActionBarActivity } @Override - public boolean onOptionsItemSelected(MenuItem item) { + public boolean onOptionsItemSelected(final MenuItem item) { final int id = item.getItemId(); final LogTemplate template = LogTemplateProvider.getTemplate(id); @@ -73,7 +73,7 @@ public abstract class AbstractLoggingActivity extends AbstractActionBarActivity protected abstract LogContext getLogContext(); - protected void insertIntoLog(String newText, final boolean moveCursor) { + protected void insertIntoLog(final String newText, final boolean moveCursor) { final EditText log = (EditText) findViewById(R.id.log); ActivityMixin.insertAtPosition(log, newText, moveCursor); } diff --git a/main/src/cgeo/geocaching/settings/TemplateTextPreference.java b/main/src/cgeo/geocaching/settings/TemplateTextPreference.java index 667b02b..1f420ef 100644 --- a/main/src/cgeo/geocaching/settings/TemplateTextPreference.java +++ b/main/src/cgeo/geocaching/settings/TemplateTextPreference.java @@ -58,7 +58,7 @@ public class TemplateTextPreference extends DialogPreference { public void onClick(View button) { AlertDialog.Builder alert = new AlertDialog.Builder(TemplateTextPreference.this.getContext()); alert.setTitle(R.string.init_signature_template_button); - final ArrayList<LogTemplate> templates = LogTemplateProvider.getTemplates(); + final ArrayList<LogTemplate> templates = LogTemplateProvider.getTemplatesWithoutSignature(); String[] items = new String[templates.size()]; for (int i = 0; i < templates.size(); i++) { items[i] = settingsActivity.getResources().getString(templates.get(i).getResourceId()); diff --git a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java index 2d4fc80..1ddede5 100644 --- a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java +++ b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java @@ -36,9 +36,9 @@ public final class LogTemplateProvider { private Geocache cache; private Trackable trackable; private boolean offline = false; - private LogEntry logEntry; + private final LogEntry logEntry; - public LogContext(final Geocache cache, LogEntry logEntry) { + public LogContext(final Geocache cache, final LogEntry logEntry) { this(cache, logEntry, false); } @@ -47,7 +47,7 @@ public final class LogTemplateProvider { this.logEntry = logEntry; } - public LogContext(final Geocache cache, LogEntry logEntry, final boolean offline) { + public LogContext(final Geocache cache, final LogEntry logEntry, final boolean offline) { this.cache = cache; this.offline = offline; this.logEntry = logEntry; @@ -104,8 +104,11 @@ public final class LogTemplateProvider { } } - public static ArrayList<LogTemplate> getTemplates() { - ArrayList<LogTemplate> templates = new ArrayList<LogTemplateProvider.LogTemplate>(); + /** + * @return all templates, but not the signature template itself + */ + public static ArrayList<LogTemplate> getTemplatesWithoutSignature() { + final ArrayList<LogTemplate> templates = new ArrayList<LogTemplateProvider.LogTemplate>(); templates.add(new LogTemplate("DATE", R.string.init_signature_template_date) { @Override @@ -171,11 +174,11 @@ public final class LogTemplateProvider { @Override public String getValue(final LogContext context) { - Trackable trackable = context.getTrackable(); + final Trackable trackable = context.getTrackable(); if (trackable != null) { return trackable.getOwner(); } - Geocache cache = context.getCache(); + final Geocache cache = context.getCache(); if (cache != null) { return cache.getOwnerDisplayName(); } @@ -184,12 +187,12 @@ public final class LogTemplateProvider { }); templates.add(new LogTemplate("NAME", R.string.init_signature_template_name) { @Override - public String getValue(LogContext context) { - Trackable trackable = context.getTrackable(); + public String getValue(final LogContext context) { + final Trackable trackable = context.getTrackable(); if (trackable != null) { return trackable.getName(); } - Geocache cache = context.getCache(); + final Geocache cache = context.getCache(); if (cache != null) { return cache.getName(); } @@ -199,12 +202,12 @@ public final class LogTemplateProvider { templates.add(new LogTemplate("URL", R.string.init_signature_template_url) { @Override - public String getValue(LogContext context) { - Trackable trackable = context.getTrackable(); + public String getValue(final LogContext context) { + final Trackable trackable = context.getTrackable(); if (trackable != null) { return trackable.getBrowserUrl(); } - Geocache cache = context.getCache(); + final Geocache cache = context.getCache(); if (cache != null) { return cache.getBrowserUrl(); } @@ -213,25 +216,37 @@ public final class LogTemplateProvider { }); templates.add(new LogTemplate("LOG", R.string.init_signature_template_log) { @Override - public String getValue(LogContext context) { - LogEntry logEntry = context.getLogEntry(); + public String getValue(final LogContext context) { + final LogEntry logEntry = context.getLogEntry(); if (logEntry != null) { return logEntry.getDisplayText(); } return StringUtils.EMPTY; } }); + return templates; + } + + /** + * @return all templates, including the signature template + */ + public static ArrayList<LogTemplate> getTemplatesWithSignature() { + final ArrayList<LogTemplate> templates = getTemplatesWithoutSignature(); templates.add(new LogTemplate("SIGNATURE", R.string.init_signature) { @Override - public String getValue(LogContext context) { - return StringUtils.defaultString(Settings.getSignature()); + public String getValue(final LogContext context) { + final String nestedTemplate = StringUtils.defaultString(Settings.getSignature()); + if (StringUtils.contains(nestedTemplate, "SIGNATURE")) { + return "invalid signature template"; + } + return LogTemplateProvider.applyTemplates(nestedTemplate, context); } }); return templates; } public static LogTemplate getTemplate(final int itemId) { - for (LogTemplate template : getTemplates()) { + for (final LogTemplate template : getTemplatesWithSignature()) { if (template.getItemId() == itemId) { return template; } @@ -244,7 +259,7 @@ public final class LogTemplateProvider { return StringUtils.EMPTY; } String result = signature; - for (LogTemplate template : getTemplates()) { + for (final LogTemplate template : getTemplatesWithSignature()) { result = template.apply(result, context); } return result; diff --git a/tests/src/cgeo/geocaching/settings/TestSettings.java b/tests/src/cgeo/geocaching/settings/TestSettings.java index 2e6809f..279f658 100644 --- a/tests/src/cgeo/geocaching/settings/TestSettings.java +++ b/tests/src/cgeo/geocaching/settings/TestSettings.java @@ -48,4 +48,8 @@ public final class TestSettings extends Settings { putString(R.string.pref_twitter_trackable_message, template); } + public static void setSignature(final String signature) { + putString(R.string.pref_signature, signature); + } + } diff --git a/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java b/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java index a7dcb2b..77832b0 100644 --- a/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java +++ b/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java @@ -2,6 +2,8 @@ package cgeo.geocaching.utils; import static org.assertj.core.api.Assertions.assertThat; +import cgeo.geocaching.settings.Settings; +import cgeo.geocaching.settings.TestSettings; import cgeo.geocaching.utils.LogTemplateProvider.LogContext; import java.util.Calendar; @@ -10,13 +12,51 @@ import junit.framework.TestCase; public class LogTemplateProviderTest extends TestCase { - public static void testApplyTemplates() { + public static void testApplyTemplatesNone() { final String noTemplates = " no templates "; - assertEquals(noTemplates, LogTemplateProvider.applyTemplates(noTemplates, new LogContext(null, null, true))); + final String signature = LogTemplateProvider.applyTemplates(noTemplates, new LogContext(null, null, true)); + assertThat(signature).isEqualTo(noTemplates); + } + public static void testApplyTemplates() { // This test can occasionally fail if the current year changes right after the next line. final String currentYear = Integer.toString(Calendar.YEAR); - assertThat(LogTemplateProvider.applyTemplates("[DATE]", new LogContext(null, null, true)).contains(currentYear)).isTrue(); + final String signature = LogTemplateProvider.applyTemplates("[DATE]", new LogContext(null, null, true)); + assertThat(signature).contains(currentYear); + } + + /** + * signature itself can contain templates, therefore nested applying is necessary + */ + public static void testApplySignature() { + String oldSignature = Settings.getSignature(); + try { + TestSettings.setSignature("[DATE]"); + String currentDate = LogTemplateProvider.applyTemplates(Settings.getSignature(), new LogContext(null, null, true)); + final String signatureTemplate = "Signature [SIGNATURE]"; + final String signature = LogTemplateProvider.applyTemplates(signatureTemplate, new LogContext(null, null, true)); + assertThat(signature).isEqualTo("Signature " + currentDate); + + final String currentYear = Integer.toString(Calendar.YEAR); + assertThat(signature).contains(currentYear); + } finally { + TestSettings.setSignature(oldSignature); + } + } + + /** + * signature must not contain itself as template + */ + public static void testApplyInvalidSignature() { + String oldSignature = Settings.getSignature(); + try { + final String signatureTemplate = "[SIGNATURE]"; + TestSettings.setSignature(signatureTemplate); + final String signature = LogTemplateProvider.applyTemplates(signatureTemplate, new LogContext(null, null, true)); + assertThat(signature).isEqualTo("invalid signature template"); + } finally { + TestSettings.setSignature(oldSignature); + } } } |
