Bug 1278245 - If a space is found, we assume this is a search query. r?sebastian
MozReview-Commit-ID: EfenqXSlaWj
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
@@ -39,25 +39,25 @@ public class StringUtils {
*
* wasSearchQuery specifies whether text was a search query before the latest change
* in text. In ambiguous cases where the new text can be either a search or a URL,
* wasSearchQuery is returned
*/
public static boolean isSearchQuery(String text, boolean wasSearchQuery) {
// We remove leading and trailing white spaces when decoding URLs
text = text.trim();
- if (text.length() == 0)
+ if (text.length() == 0) {
return wasSearchQuery;
-
+ }
int colon = text.indexOf(':');
int dot = text.indexOf('.');
int space = text.indexOf(' ');
- // If a space is found before any dot and colon, we assume this is a search query
- if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) {
+ // If a space is found in a trimmed string, we assume this is a search query(Bug 1278245)
+ if (space > -1) {
return true;
}
// Otherwise, if a dot or a colon is found, we assume this is a URL
if (dot > -1 || colon > -1) {
return false;
}
// Otherwise, text is ambiguous, and we keep its status unchanged
return wasSearchQuery;
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
@@ -99,9 +99,47 @@ public class TestStringUtils {
assertEquals("www.mozilla.org", StringUtils.join(".", Arrays.asList("www", "mozilla", "org")));
assertEquals("hello", StringUtils.join("", Collections.singletonList("hello")));
assertEquals("helloworld", StringUtils.join("", Arrays.asList("hello", "world")));
assertEquals("hello world", StringUtils.join(" ", Arrays.asList("hello", "world")));
assertEquals("m::o::z::i::l::l::a", StringUtils.join("::", Arrays.asList("m", "o", "z", "i", "l", "l", "a")));
}
+
+ @Test
+ public void testIsSearchQuery(){
+ boolean any = true;
+ // test trim
+ assertFalse(StringUtils.isSearchQuery("",false));
+ assertTrue(StringUtils.isSearchQuery("",true));
+
+ // test space
+ assertTrue(StringUtils.isSearchQuery(" apple pen ",any));
+ assertTrue(StringUtils.isSearchQuery("pineapple pen",any));
+ assertTrue(StringUtils.isSearchQuery(": :",any));
+ assertTrue(StringUtils.isSearchQuery(". .",any));
+ assertTrue(StringUtils.isSearchQuery("gcm site:stackoverflow.com",any));
+ assertTrue(StringUtils.isSearchQuery("/mnt/etc/resolv.conf does not exist",true));
+
+ // test colon
+ assertFalse(StringUtils.isSearchQuery(":",any));
+ assertFalse(StringUtils.isSearchQuery("site:stackoverflow.com",any));
+ assertFalse(StringUtils.isSearchQuery("http:mozilla.com",any));
+ assertFalse(StringUtils.isSearchQuery("http://mozilla.com",any));
+ assertFalse(StringUtils.isSearchQuery("http:/mozilla.com",any));
+
+ // test dot
+ assertFalse(StringUtils.isSearchQuery(".",any));
+ assertFalse(StringUtils.isSearchQuery("cd..",any));
+ assertFalse(StringUtils.isSearchQuery("cd...",any));
+ assertFalse(StringUtils.isSearchQuery("mozilla.com",any));
+
+
+ // test ambiguous
+ String ambiguous = "~!@#$%^&*()_+`34567890-=qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"ZXCVBNM<>?zxcvbnm,./";
+ ambiguous = ambiguous.replace(" ","").replace(".","").replace(":","");
+ assertTrue(StringUtils.isSearchQuery(ambiguous,true));
+ assertFalse(StringUtils.isSearchQuery(ambiguous,false));
+
+
+ }
}