Skip to content

Commit

Permalink
Add Google Search App support (fnando#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
milandobrota authored and fnando committed Jul 8, 2020
1 parent 631b481 commit ebc508b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Add method `Browser::Base#unknown?`
- Fix issue with `Browser::Base#safari?` matching full version
- Add Maxthon detection
- Add Google Search App detection

## 4.2.0

Expand Down
5 changes: 5 additions & 0 deletions lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def maxthon?(expected_version = nil)
Maxthon.new(ua).match? && detect_version?(full_version, expected_version)
end

# Detect if browser is Google Search App
def google_search_app?(expected_version = nil)
ua =~ /GSA/ && detect_version?(full_version, expected_version)
end

def webkit_full_version
ua[%r{AppleWebKit/([\d.]+)}, 1] || "0.0"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/browser/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require_relative "duck_duck_go"
require_relative "samsung_browser"
require_relative "maxthon"
require_relative "google_search_app"

require_relative "bot"
require_relative "bot/empty_user_agent_matcher"
Expand Down Expand Up @@ -77,6 +78,7 @@ def self.matchers
DuckDuckGo, # must be placed before Chrome and Safari
SamsungBrowser, # must be placed before Chrome and Safari
Maxthon, # must be placed before Chrome and Safari
GoogleSearchApp, # must be placed before Chrome and Safari
Chrome,
Safari,
Unknown
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def match?
!yandex? &&
!sputnik? &&
!samsung_browser? &&
!maxthon?
!maxthon? &&
!google_search_app?
end
end
end
21 changes: 21 additions & 0 deletions lib/browser/google_search_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Browser
class GoogleSearchApp < Chrome
def id
:google_search_app
end

def name
"Google Search App"
end

def full_version
ua[%r{GSA/([\d.]+\d)}, 1] || super
end

def match?
ua =~ /GSA/
end
end
end
3 changes: 2 additions & 1 deletion lib/browser/safari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def match?
!duck_duck_go? &&
!yandex? &&
!sputnik? &&
!maxthon?
!maxthon? &&
!google_search_app?
end
end
end
3 changes: 3 additions & 0 deletions test/ua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ FIREFOX_IOS: "Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/
FIREFOX_MODERN: "Mozilla/5.0 (X11; Ubuntu; Linux armv7l; rv:17.0) Gecko/20100101 Firefox/17.0"
FIREFOX_OS: "Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0"
FIREFOX_TABLET: "Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0"
GOOGLE_SEARCH_APP_ANDROID: "Mozilla/5.0 (Linux; Android 10; SM-G960U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.117 Mobile Safari/537.36 GSA/11.6.8.21.arm64"
GOOGLE_SEARCH_APP_IPAD: "Mozilla/5.0 (iPad; CPU OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) GSA/102.0.304944559 Mobile/15E148 Safari/605.1"
GOOGLE_SEARCH_APP_IPHONE: "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) GSA/105.0.307913796 Mobile/17D50 Safari/604.1"
IE10: "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0; EIE10;ENUSMSN)"
IE10_COMPAT: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; EIE10;ENUSMSN)"
IE10_X64_WINX64: "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)"
Expand Down
54 changes: 54 additions & 0 deletions test/unit/google_search_app_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require "test_helper"

class GoogleSearchAppTest < Minitest::Test
test "detects Google Search App (Android)" do
browser = Browser.new(Browser["GOOGLE_SEARCH_APP_ANDROID"])

assert browser.webkit?
assert browser.google_search_app?
assert browser.platform.android?
assert_equal :google_search_app, browser.id
assert_equal "11", browser.version
assert_equal "11.6.8.21", browser.full_version
assert_equal "Google Search App", browser.name
refute browser.chrome?
refute browser.safari?
end

test "detects Google Search App (iPad)" do
browser = Browser.new(Browser["GOOGLE_SEARCH_APP_IPAD"])

assert browser.webkit?
assert browser.google_search_app?
assert browser.device.ipad?
assert browser.platform.ios?
assert_equal :google_search_app, browser.id
assert_equal "102", browser.version
assert_equal "102.0.304944559", browser.full_version
assert_equal "Google Search App", browser.name
refute browser.chrome?
refute browser.safari?
end

test "detects Google Search App (iPhone)" do
browser = Browser.new(Browser["GOOGLE_SEARCH_APP_IPHONE"])

assert browser.webkit?
assert browser.google_search_app?
assert browser.device.iphone?
assert browser.platform.ios?
assert_equal :google_search_app, browser.id
assert_equal "105", browser.version
assert_equal "105.0.307913796", browser.full_version
assert_equal "Google Search App", browser.name
refute browser.chrome?
refute browser.safari?
end

test "detects version by range (iPad)" do
browser = Browser.new(Browser["GOOGLE_SEARCH_APP_IPAD"])
assert browser.google_search_app?(%w[>=102 <103])
end
end

0 comments on commit ebc508b

Please sign in to comment.