diff --git a/mobile/android/android-components/components/browser/search/src/main/java/mozilla/components/browser/search/SearchSuggestionClient.kt b/mobile/android/android-components/components/browser/search/src/main/java/mozilla/components/browser/search/SearchSuggestionClient.kt new file mode 100644 index 000000000000..f971a20a7fcb --- /dev/null +++ b/mobile/android/android-components/components/browser/search/src/main/java/mozilla/components/browser/search/SearchSuggestionClient.kt @@ -0,0 +1,108 @@ +package +mozilla +. +components +. +browser +. +search +import +kotlinx +. +coroutines +. +experimental +. +async +import +kotlinx +. +coroutines +. +experimental +. +Deferred +import +mozilla +. +components +. +browser +. +search +. +parser +. +googleParser +typealias +SearchSuggestionFetcher += +suspend +( +url +: +String +) +- +> +String +? +class +SearchSuggestionClient +( +private +val +searchEngine +: +SearchEngine +private +val +fetcher +: +SearchSuggestionFetcher +) +{ +suspend +fun +getSuggestions +( +query +: +String +) +: +List +< +String +> +? +{ +val +suggestionsURL += +searchEngine +. +buildSuggestionsURL +( +query +) +? +: +return +null +return +fetcher +( +suggestionsURL +) +? +. +let +{ +googleParser +( +it +) +} +} +} diff --git a/mobile/android/android-components/components/browser/search/src/main/java/mozilla/components/browser/search/parser/SuggestionParser.kt b/mobile/android/android-components/components/browser/search/src/main/java/mozilla/components/browser/search/parser/SuggestionParser.kt new file mode 100644 index 000000000000..2b5fae580940 --- /dev/null +++ b/mobile/android/android-components/components/browser/search/src/main/java/mozilla/components/browser/search/parser/SuggestionParser.kt @@ -0,0 +1,156 @@ +package +mozilla +. +components +. +browser +. +search +. +parser +import +org +. +json +. +JSONArray +fun +JSONArray +. +asSequence +( +) +: +Sequence +< +Any +> +{ +return +object +: +Sequence +< +Any +> +{ +override +fun +iterator +( +) += +object +: +Iterator +< +Any +> +{ +val +it += +( +0 +until +this +asSequence +. +length +( +) +) +. +iterator +( +) +override +fun +next +( +) +: +Any +{ +val +i += +it +. +next +( +) +return +this +asSequence +. +get +( +i +) +} +override +fun +hasNext +( +) += +it +. +hasNext +( +) +} +} +} +typealias +Parser += +( +String +) +- +> +List +< +String +> +val +googleParser +: +Parser += +{ +input +- +> +JSONArray +( +input +) +. +getJSONArray +( +1 +) +. +asSequence +( +) +. +map +{ +it +as +? +String +} +. +filterNotNull +( +) +. +toList +( +) +} diff --git a/mobile/android/android-components/components/browser/search/src/test/java/mozilla/components/browser/search/SearchSuggestionClientTest.kt b/mobile/android/android-components/components/browser/search/src/test/java/mozilla/components/browser/search/SearchSuggestionClientTest.kt new file mode 100644 index 000000000000..0054ac6ca107 --- /dev/null +++ b/mobile/android/android-components/components/browser/search/src/test/java/mozilla/components/browser/search/SearchSuggestionClientTest.kt @@ -0,0 +1,315 @@ +/ +* +This +Source +Code +Form +is +subject +to +the +terms +of +the +Mozilla +Public +* +License +v +. +2 +. +0 +. +If +a +copy +of +the +MPL +was +not +distributed +with +this +* +file +You +can +obtain +one +at +http +: +/ +/ +mozilla +. +org +/ +MPL +/ +2 +. +0 +/ +. +* +/ +package +mozilla +. +components +. +browser +. +search +import +kotlinx +. +coroutines +. +experimental +. +runBlocking +import +org +. +json +. +JSONArray +import +org +. +junit +. +Test +import +org +. +junit +. +runner +. +RunWith +import +org +. +junit +. +Assert +. +assertEquals +import +org +. +robolectric +. +RobolectricTestRunner +import +org +. +robolectric +. +RuntimeEnvironment +RunWith +( +RobolectricTestRunner +: +: +class +) +class +SearchSuggestionClientTest +{ +Test +fun +Get +a +list +of +results +based +on +a +search +engine +( +) +{ +val +searchEngine += +SearchEngineParser +( +) +. +load +( +RuntimeEnvironment +. +application +. +assets +" +google +" +" +searchplugins +/ +google +- +nocodes +. +xml +" +) +val +client += +SearchSuggestionClient +( +searchEngine +{ +" +[ +\ +" +firefox +\ +" +[ +\ +" +firefox +\ +" +\ +" +firefox +for +mac +\ +" +\ +" +firefox +quantum +\ +" +\ +" +firefox +update +\ +" +\ +" +firefox +esr +\ +" +\ +" +firefox +focus +\ +" +\ +" +firefox +addons +\ +" +\ +" +firefox +extensions +\ +" +\ +" +firefox +nightly +\ +" +\ +" +firefox +clear +cache +\ +" +] +] +" +} +) +runBlocking +{ +val +results += +client +. +getSuggestions +( +" +firefox +" +) +val +expectedResults += +listOf +( +" +firefox +" +" +firefox +for +mac +" +" +firefox +quantum +" +" +firefox +update +" +" +firefox +esr +" +" +firefox +focus +" +" +firefox +addons +" +" +firefox +extensions +" +" +firefox +nightly +" +" +firefox +clear +cache +" +) +assertEquals +( +expectedResults +results +) +} +} +} diff --git a/mobile/android/android-components/components/browser/search/src/test/java/mozilla/components/browser/search/parser/SuggestionParserTest.kt b/mobile/android/android-components/components/browser/search/src/test/java/mozilla/components/browser/search/parser/SuggestionParserTest.kt new file mode 100644 index 000000000000..ecce5f0fa064 --- /dev/null +++ b/mobile/android/android-components/components/browser/search/src/test/java/mozilla/components/browser/search/parser/SuggestionParserTest.kt @@ -0,0 +1,252 @@ +/ +* +This +Source +Code +Form +is +subject +to +the +terms +of +the +Mozilla +Public +* +License +v +. +2 +. +0 +. +If +a +copy +of +the +MPL +was +not +distributed +with +this +* +file +You +can +obtain +one +at +http +: +/ +/ +mozilla +. +org +/ +MPL +/ +2 +. +0 +/ +. +* +/ +package +mozilla +. +components +. +browser +. +search +. +parser +import +org +. +junit +. +Assert +. +assertEquals +import +org +. +junit +. +Test +import +org +. +junit +. +runner +. +RunWith +import +org +. +robolectric +. +RobolectricTestRunner +RunWith +( +RobolectricTestRunner +: +: +class +) +class +SuggestionParserTest +{ +Test +fun +Parse +a +search +suggestion +result +( +) +{ +val +json += +" +[ +\ +" +firefox +\ +" +[ +\ +" +firefox +\ +" +\ +" +firefox +for +mac +\ +" +\ +" +firefox +quantum +\ +" +\ +" +firefox +update +\ +" +\ +" +firefox +esr +\ +" +\ +" +firefox +focus +\ +" +\ +" +firefox +addons +\ +" +\ +" +firefox +extensions +\ +" +\ +" +firefox +nightly +\ +" +\ +" +firefox +clear +cache +\ +" +] +] +" +val +results += +googleParser +( +json +) +val +expectedResults += +listOf +( +" +firefox +" +" +firefox +for +mac +" +" +firefox +quantum +" +" +firefox +update +" +" +firefox +esr +" +" +firefox +focus +" +" +firefox +addons +" +" +firefox +extensions +" +" +firefox +nightly +" +" +firefox +clear +cache +" +) +assertEquals +( +expectedResults +results +) +} +}