brauser 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/.travis.yml +8 -0
- data/.yardopts +1 -0
- data/Gemfile +9 -0
- data/README.md +27 -0
- data/Rakefile +18 -0
- data/brauser.gemspec +34 -0
- data/doc/Brauser.html +129 -0
- data/doc/Brauser/Browser.html +4392 -0
- data/doc/Brauser/Hooks.html +125 -0
- data/doc/Brauser/Hooks/RubyOnRails.html +332 -0
- data/doc/Brauser/Query.html +1315 -0
- data/doc/Brauser/Version.html +189 -0
- data/doc/_index.html +177 -0
- data/doc/class_list.html +53 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +57 -0
- data/doc/css/style.css +328 -0
- data/doc/file.README.html +102 -0
- data/doc/file_list.html +55 -0
- data/doc/frames.html +28 -0
- data/doc/index.html +102 -0
- data/doc/js/app.js +214 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +412 -0
- data/doc/top-level-namespace.html +112 -0
- data/lib/brauser.rb +14 -0
- data/lib/brauser/browser.rb +794 -0
- data/lib/brauser/hooks.rb +31 -0
- data/lib/brauser/query.rb +118 -0
- data/lib/brauser/version.rb +24 -0
- data/spec/brauser/browser_spec.rb +685 -0
- data/spec/brauser/hooks_spec.rb +27 -0
- data/spec/brauser/query_spec.rb +123 -0
- data/spec/brauser_spec.rb +6 -0
- data/spec/coverage_helper.rb +20 -0
- data/spec/frameworks_helper.rb +24 -0
- data/spec/spec_helper.rb +18 -0
- metadata +226 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the brauser gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Brauser
|
8
|
+
# A set of Hooks for adding brauser to web frameworks.
|
9
|
+
module Hooks
|
10
|
+
# Hook for integration with Ruby on Rails.
|
11
|
+
module RubyOnRails
|
12
|
+
# Includes brauser in ActionController.
|
13
|
+
#
|
14
|
+
# @param base [Class] The base controller class.
|
15
|
+
def self.included(base)
|
16
|
+
base.send :helper_method, :browser
|
17
|
+
end
|
18
|
+
|
19
|
+
# Detects the current browser.
|
20
|
+
#
|
21
|
+
# @param force [Boolean] If to force detection.
|
22
|
+
# @return [Browser] The detected browser.
|
23
|
+
def browser(force = false)
|
24
|
+
@browser = nil if force
|
25
|
+
@browser ||= Browser.new(request.headers["User-Agent"], request.headers["Accept-Language"])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ActionController::Base.send(:include, Brauser::Hooks::RubyOnRails) if defined?(Rails)
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the brauser gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
# A framework agnostic browser detection and querying helper.
|
8
|
+
module Brauser
|
9
|
+
# A query to a browser. This class enables concatenation, like:
|
10
|
+
#
|
11
|
+
# ```ruby
|
12
|
+
# Brauser::Browser.new.is(:msie).v(">= 7").on?(:windows)
|
13
|
+
# ```
|
14
|
+
#
|
15
|
+
# To end concatenation, use the `?` form of the queries or call `.result`.
|
16
|
+
class Query
|
17
|
+
# The current browser.
|
18
|
+
attr_accessor :target
|
19
|
+
|
20
|
+
# The current result.
|
21
|
+
attr_accessor :result
|
22
|
+
|
23
|
+
# Creates a new query.
|
24
|
+
#
|
25
|
+
# @param target [Browser] The current browser.
|
26
|
+
# @param result [Boolean] The current result.
|
27
|
+
def initialize(target, result = true)
|
28
|
+
@target = target
|
29
|
+
@result = result
|
30
|
+
end
|
31
|
+
|
32
|
+
# Checks if the browser is a specific name and optionally of a specific version and platform.
|
33
|
+
#
|
34
|
+
# @see #version?
|
35
|
+
# @see #on?
|
36
|
+
#
|
37
|
+
# @param names [Symbol|Array] A list of specific names to match. Also, this meta-names are supported: `:capable` and `:tablet`.
|
38
|
+
# @param versions [Hash] An hash with specific version to match against. Need to be in any form that {#v} understands.
|
39
|
+
# @param platforms [Symbol|Array] A list of specific platform to match. Valid values are all those possible for the platform attribute.
|
40
|
+
# @return [Query] The query itself.
|
41
|
+
def is(names = [], versions = {}, platforms = [])
|
42
|
+
@result = self.is?(names, versions, platforms)
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
# Checks if the browser is a specific name and optionally of a specific version and platform.
|
47
|
+
#
|
48
|
+
# This version returns a boolean and it is equal to append a call to {#result} to the method {#is}.
|
49
|
+
#
|
50
|
+
# @see #version?
|
51
|
+
# @see #on?
|
52
|
+
#
|
53
|
+
# @param names [Symbol|Array] A list of specific names to match. Also, this meta-names are supported: `:capable` and `:tablet`.
|
54
|
+
# @param versions [Hash] An hash with specific version to match against. Need to be in any form that {#v} understands.
|
55
|
+
# @param platforms [Symbol|Array] A list of specific platform to match. Valid values are all those possible for the platform attribute.
|
56
|
+
# @return [Boolean] `true` if current browser matches, `false` otherwise.
|
57
|
+
def is?(names = [], versions = {}, platforms = [])
|
58
|
+
@result ? @target.is?(names, versions, platforms) : @result
|
59
|
+
end
|
60
|
+
|
61
|
+
# Checks if the brower is a specific version.
|
62
|
+
#
|
63
|
+
# @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against, in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
|
64
|
+
# @return [Query] The query itself.
|
65
|
+
def v(versions = {})
|
66
|
+
@result = self.v?(versions)
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
# Checks if the brower is a specific version.
|
71
|
+
#
|
72
|
+
# This version returns a boolean and it is equal to append a call to {#result} to the method {#v}.
|
73
|
+
#
|
74
|
+
# @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against, in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
|
75
|
+
# @return [Boolean] `true` if current browser matches, `false` otherwise.
|
76
|
+
def v?(versions = {})
|
77
|
+
@result ? @target.v?(versions) : @result
|
78
|
+
end
|
79
|
+
|
80
|
+
# Check if the browser is on a specific platform.
|
81
|
+
#
|
82
|
+
# @param platforms [Symbol|Array] A list of specific platform to match.
|
83
|
+
# @return [Query] The query itself.
|
84
|
+
def on(platforms = [])
|
85
|
+
@result = self.on?(platforms)
|
86
|
+
self
|
87
|
+
end
|
88
|
+
|
89
|
+
# Check if the browser is on a specific platform.
|
90
|
+
#
|
91
|
+
# This version returns a boolean and it is equal to append a call to {#result} to the method {#on}.
|
92
|
+
#
|
93
|
+
# @param platforms [Symbol|Array] A list of specific platform to match.
|
94
|
+
# @return [Boolean] `true` if current browser matches, `false` otherwise.
|
95
|
+
def on?(platforms = [])
|
96
|
+
@result ? @target.on?(platforms) : @result
|
97
|
+
end
|
98
|
+
|
99
|
+
# Check if the browser accepts the specified languages.
|
100
|
+
#
|
101
|
+
# @param langs [String|Array] A list of languages to match against.
|
102
|
+
# @return [Query] The query itself.
|
103
|
+
def accepts(langs = [])
|
104
|
+
@result = self.accepts?(langs)
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
# Check if the browser accepts the specified languages.
|
109
|
+
#
|
110
|
+
# This version returns a boolean and it is equal to append a call to {#result} to the method {#accepts}.
|
111
|
+
#
|
112
|
+
# @param langs [String|Array] A list of languages to match against.
|
113
|
+
# @return [Boolean] `true` if current browser matches, `false` otherwise.
|
114
|
+
def accepts?(langs = [])
|
115
|
+
@result ? @target.accepts?(langs) : @result
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the brauser gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Brauser
|
8
|
+
# The current version of brauser, according to semantic versioning.
|
9
|
+
#
|
10
|
+
# @see http://semver.org
|
11
|
+
module Version
|
12
|
+
# The major version.
|
13
|
+
MAJOR = 1
|
14
|
+
|
15
|
+
# The minor version.
|
16
|
+
MINOR = 0
|
17
|
+
|
18
|
+
# The patch version.
|
19
|
+
PATCH = 0
|
20
|
+
|
21
|
+
# The current version of brauser.
|
22
|
+
STRING = [MAJOR, MINOR, PATCH].compact.join(".")
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,685 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the brauser gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
describe Brauser::Browser do
|
10
|
+
class Brauser::Query
|
11
|
+
def true_query?
|
12
|
+
@result == true
|
13
|
+
end
|
14
|
+
|
15
|
+
def false_query?
|
16
|
+
@result == false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:browser){Brauser::Browser.new}
|
21
|
+
|
22
|
+
describe ".register_default_browsers" do
|
23
|
+
it "should call .register_browser many times" do
|
24
|
+
Brauser::Browser.should_receive(:register_browser).at_least(1)
|
25
|
+
Brauser::Browser.register_default_browsers
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return a good return code" do
|
29
|
+
expect(Brauser::Browser.register_default_browsers).to be_true
|
30
|
+
|
31
|
+
Brauser::Browser.stub(:register_browser).and_return(true)
|
32
|
+
expect(Brauser::Browser.register_default_browsers).to be_false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe ".register_default_platforms" do
|
37
|
+
it "should call .register_platform many times" do
|
38
|
+
Brauser::Browser.should_receive(:register_platform).at_least(1)
|
39
|
+
Brauser::Browser.register_default_platforms
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return a good return code" do
|
43
|
+
expect(Brauser::Browser.register_default_platforms).to be_true
|
44
|
+
|
45
|
+
Brauser::Browser.stub(:register_platform).and_return(true)
|
46
|
+
expect(Brauser::Browser.register_default_platforms).to be_false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe ".register_default_languages" do
|
51
|
+
it "should call .register_language many times" do
|
52
|
+
Brauser::Browser.should_receive(:register_language).at_least(1)
|
53
|
+
Brauser::Browser.register_default_languages
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return a good return code" do
|
57
|
+
expect(Brauser::Browser.register_default_languages).to be_true
|
58
|
+
|
59
|
+
Brauser::Browser.stub(:register_language).and_return(true)
|
60
|
+
expect(Brauser::Browser.register_default_languages).to be_false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe ".browsers" do
|
65
|
+
it "should return the list of browsers" do
|
66
|
+
Brauser::Browser.register_default_browsers
|
67
|
+
|
68
|
+
expect(Brauser::Browser.browsers).to be_a(Hash)
|
69
|
+
expect(Brauser::Browser.browsers[:chrome]).to eq([/((chrome)|(chromium))/i, /(.+Chrom[a-z]+\/)([a-z0-9.]+)/i, "Google Chrome"])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe ".platforms" do
|
74
|
+
it "should return the list of platforms" do
|
75
|
+
Brauser::Browser.register_default_platforms
|
76
|
+
|
77
|
+
expect(Brauser::Browser.platforms).to be_a(Hash)
|
78
|
+
expect(Brauser::Browser.platforms[:osx]).to eq([/mac|macintosh|mac os x/i, "Apple MacOS X"])
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe ".languages" do
|
84
|
+
it "should return the list of languages" do
|
85
|
+
Brauser::Browser.register_default_languages
|
86
|
+
|
87
|
+
expect(Brauser::Browser.languages).to be_a(Hash)
|
88
|
+
expect(Brauser::Browser.languages["it"]).to eq("Italian")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe ".register_browser" do
|
93
|
+
before(:each) do
|
94
|
+
Brauser::Browser.instance_variable_set("@browsers", nil)
|
95
|
+
Brauser::Browser.instance_variable_set("@browsers_indexes", nil)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should initialize data" do
|
99
|
+
expect(Brauser::Browser.instance_variable_get("@browsers")).to be_nil
|
100
|
+
expect(Brauser::Browser.instance_variable_get("@browsers_indexes")).to be_nil
|
101
|
+
Brauser::Browser.register_browser([])
|
102
|
+
expect(Brauser::Browser.instance_variable_get("@browsers")).to be_a(Array)
|
103
|
+
expect(Brauser::Browser.instance_variable_get("@browsers_indexes")).to be_a(Hash)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should return good return values" do
|
107
|
+
expect(Brauser::Browser.register_browser([])).to be_false
|
108
|
+
expect(Brauser::Browser.register_browser("NAME", //i, //i, "LABEL")).to be_true
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should work with a single entry" do
|
112
|
+
expect(Brauser::Browser.register_browser("NAME", //i, //i, "LABEL")).to be_true
|
113
|
+
expect(Brauser::Browser.instance_variable_get("@browsers")).to eq([[:NAME, //i, //i, "LABEL"]])
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should work with multiple entries" do
|
117
|
+
expect(Brauser::Browser.register_browser([["NAME 1", //i, //i, "LABEL 1"], ["NAME 2", //i, //i, "LABEL 2"]])).to be_true
|
118
|
+
expect(Brauser::Browser.instance_variable_get("@browsers")).to eq([[:"NAME 1", //i, //i, "LABEL 1"], [:"NAME 2", //i, //i, "LABEL 2"]])
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should update indexes" do
|
122
|
+
Brauser::Browser.register_browser("NAME 1", //i, //i, "LABEL 1")
|
123
|
+
Brauser::Browser.register_browser("NAME 2", //i, //i, "LABEL 2")
|
124
|
+
|
125
|
+
expect(Brauser::Browser.instance_variable_get("@browsers_indexes")[:"NAME 1"]).to eq(0)
|
126
|
+
expect(Brauser::Browser.instance_variable_get("@browsers_indexes")[:"NAME 2"]).to eq(1)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should update existing entries" do
|
130
|
+
Brauser::Browser.register_browser("NAME 1", //i, //i, "LABEL 1")
|
131
|
+
Brauser::Browser.register_browser("NAME 2", //i, //i, "LABEL 2")
|
132
|
+
Brauser::Browser.register_browser("NAME 3", //i, //i, "LABEL 3")
|
133
|
+
|
134
|
+
expect(Brauser::Browser.register_browser("NAME 2", //i, //i, "LABEL 4")).to be_true
|
135
|
+
expect(Brauser::Browser.instance_variable_get("@browsers_indexes").length).to eq(3)
|
136
|
+
expect(Brauser::Browser.instance_variable_get("@browsers_indexes")[:"NAME 2"]).to eq(1)
|
137
|
+
expect(Brauser::Browser.instance_variable_get("@browsers").length).to eq(3)
|
138
|
+
expect(Brauser::Browser.instance_variable_get("@browsers")[1].last).to eq("LABEL 4")
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe ".register_platform" do
|
143
|
+
before(:each) do
|
144
|
+
Brauser::Browser.instance_variable_set("@platforms", nil)
|
145
|
+
Brauser::Browser.instance_variable_set("@platforms_indexes", nil)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should initialize data" do
|
149
|
+
expect(Brauser::Browser.instance_variable_get("@platforms")).to be_nil
|
150
|
+
expect(Brauser::Browser.instance_variable_get("@platforms_indexes")).to be_nil
|
151
|
+
Brauser::Browser.register_platform([])
|
152
|
+
expect(Brauser::Browser.instance_variable_get("@platforms")).to be_a(Array)
|
153
|
+
expect(Brauser::Browser.instance_variable_get("@platforms_indexes")).to be_a(Hash)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should return good return values" do
|
157
|
+
expect(Brauser::Browser.register_platform([])).to be_false
|
158
|
+
expect(Brauser::Browser.register_platform("NAME", //i, "LABEL")).to be_true
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should work with a single entry" do
|
162
|
+
expect(Brauser::Browser.register_platform("NAME", //i, "LABEL")).to be_true
|
163
|
+
expect(Brauser::Browser.instance_variable_get("@platforms")).to eq([[:"NAME", //i, "LABEL"]])
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should work with multiple entries" do
|
167
|
+
expect(Brauser::Browser.register_platform([["NAME 1", //i, "LABEL 1"], ["NAME 2", //i, "LABEL 2"]])).to be_true
|
168
|
+
expect(Brauser::Browser.instance_variable_get("@platforms")).to eq([[:"NAME 1", //i, "LABEL 1"], [:"NAME 2", //i, "LABEL 2"]])
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should update indexes" do
|
172
|
+
Brauser::Browser.register_platform("NAME 1", //i, "LABEL 1")
|
173
|
+
Brauser::Browser.register_platform("NAME 2", //i, "LABEL 2")
|
174
|
+
|
175
|
+
expect(Brauser::Browser.instance_variable_get("@platforms_indexes")[:"NAME 1"]).to eq(0)
|
176
|
+
expect(Brauser::Browser.instance_variable_get("@platforms_indexes")[:"NAME 2"]).to eq(1)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should update existing entries" do
|
180
|
+
Brauser::Browser.register_platform("NAME 1", //i, "LABEL 1")
|
181
|
+
Brauser::Browser.register_platform("NAME 2", //i, "LABEL 2")
|
182
|
+
Brauser::Browser.register_platform("NAME 3", //i, "LABEL 3")
|
183
|
+
|
184
|
+
expect(Brauser::Browser.register_platform("NAME 2", //i, "LABEL 4")).to be_true
|
185
|
+
expect(Brauser::Browser.instance_variable_get("@platforms_indexes").length).to eq(3)
|
186
|
+
expect(Brauser::Browser.instance_variable_get("@platforms_indexes")[:"NAME 2"]).to eq(1)
|
187
|
+
expect(Brauser::Browser.instance_variable_get("@platforms").length).to eq(3)
|
188
|
+
expect(Brauser::Browser.instance_variable_get("@platforms")[1].last).to eq("LABEL 4")
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe ".register_language" do
|
193
|
+
before(:each) do
|
194
|
+
Brauser::Browser.instance_variable_set("@languages", nil)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should initialize data" do
|
198
|
+
expect(Brauser::Browser.instance_variable_get("@languages")).to be_nil
|
199
|
+
Brauser::Browser.register_language([])
|
200
|
+
expect(Brauser::Browser.instance_variable_get("@languages")).to be_a(Hash)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should return good return values" do
|
204
|
+
expect(Brauser::Browser.register_language([])).to be_false
|
205
|
+
expect(Brauser::Browser.register_language("cc", "LABEL")).to be_true
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should work with a single entry" do
|
209
|
+
expect(Brauser::Browser.register_language("cc", "LABEL")).to be_true
|
210
|
+
expect(Brauser::Browser.instance_variable_get("@languages")).to eq({"cc" => "LABEL"})
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should work with multiple entries" do
|
214
|
+
expect(Brauser::Browser.register_language({"c1" => "LABEL 1", "c2" => "LABEL 2"})).to be_true
|
215
|
+
expect(Brauser::Browser.instance_variable_get("@languages")).to eq({"c1" => "LABEL 1", "c2" => "LABEL 2"})
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should update existing entries" do
|
219
|
+
Brauser::Browser.register_language("c1", "LABEL 1")
|
220
|
+
Brauser::Browser.register_language("c2", "LABEL 2")
|
221
|
+
Brauser::Browser.register_language("c3", "LABEL 3")
|
222
|
+
|
223
|
+
expect(Brauser::Browser.register_language("c2", "LABEL 4")).to be_true
|
224
|
+
expect(Brauser::Browser.instance_variable_get("@languages").length).to eq(3)
|
225
|
+
expect(Brauser::Browser.instance_variable_get("@languages")["c2"]).to eq("LABEL 4")
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe ".compare_versions" do
|
230
|
+
it "should correctly compare versions" do
|
231
|
+
expect(Brauser::Browser.compare_versions(nil, :eq, nil)).to be_false
|
232
|
+
|
233
|
+
expect(Brauser::Browser.compare_versions("3", :eq, nil)).to be_false
|
234
|
+
expect(Brauser::Browser.compare_versions("3", :eq, "7")).to be_false
|
235
|
+
expect(Brauser::Browser.compare_versions("7.1", :eq, "7")).to be_false
|
236
|
+
expect(Brauser::Browser.compare_versions("7.1.2", :eq, "7.1.2")).to be_true
|
237
|
+
|
238
|
+
expect(Brauser::Browser.compare_versions("3", :lt, "3")).to be_false
|
239
|
+
expect(Brauser::Browser.compare_versions("3", :lt, "3.4")).to be_true
|
240
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :lt, "3.4.5")).to be_false
|
241
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :lt, "3.4.6")).to be_true
|
242
|
+
expect(Brauser::Browser.compare_versions("3.4.beta", :lt, "3.4")).to be_true
|
243
|
+
expect(Brauser::Browser.compare_versions("3.4.alpha", :lt, "3.4beta")).to be_true
|
244
|
+
|
245
|
+
expect(Brauser::Browser.compare_versions("3", :lte, "3")).to be_true
|
246
|
+
expect(Brauser::Browser.compare_versions("3", :lte, "3.4")).to be_true
|
247
|
+
expect(Brauser::Browser.compare_versions("4", :lte, "3.4")).to be_false
|
248
|
+
expect(Brauser::Browser.compare_versions("4.1", :lte, "3.4")).to be_false
|
249
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :lte, "3.4.5")).to be_true
|
250
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :lte, "3.4.4")).to be_false
|
251
|
+
expect(Brauser::Browser.compare_versions("3.4.beta", :lte, "3.4")).to be_true
|
252
|
+
|
253
|
+
expect(Brauser::Browser.compare_versions("3", :gt, "3")).to be_false
|
254
|
+
expect(Brauser::Browser.compare_versions("3", :gt, "3.4")).to be_false
|
255
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :gt, "3.4.3")).to be_true
|
256
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :gt, "3.4.5")).to be_false
|
257
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :gt, "3.4.6")).to be_false
|
258
|
+
expect(Brauser::Browser.compare_versions("3.4.beta", :gt, "3.4")).to be_false
|
259
|
+
expect(Brauser::Browser.compare_versions("3.4.alpha", :gt, "3.4beta")).to be_false
|
260
|
+
|
261
|
+
expect(Brauser::Browser.compare_versions("3", :gte, "3")).to be_true
|
262
|
+
expect(Brauser::Browser.compare_versions("3", :gte, "3.4")).to be_false
|
263
|
+
expect(Brauser::Browser.compare_versions("4", :gte, "3.4")).to be_true
|
264
|
+
expect(Brauser::Browser.compare_versions("4.1", :gte, "3.4")).to be_true
|
265
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :gte, "3.4.5")).to be_true
|
266
|
+
expect(Brauser::Browser.compare_versions("3.4.5", :gte, "3.4.4")).to be_true
|
267
|
+
expect(Brauser::Browser.compare_versions("3.4.beta", :gte, "3.4")).to be_false
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
describe "#parse_agent" do
|
272
|
+
def recognize(agent = nil, only_rv = false)
|
273
|
+
if agent.present? then
|
274
|
+
rv = browser.parse_agent(agent)
|
275
|
+
!only_rv ? [browser.name, browser.version, browser.platform] : rv
|
276
|
+
else
|
277
|
+
only_rv ? true : []
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should return true for known names" do
|
282
|
+
expect(recognize("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.82 Safari/537.1", true)).to be_true
|
283
|
+
expect(recognize("Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2", true)).to be_true
|
284
|
+
end
|
285
|
+
|
286
|
+
it "should return false for unknown names" do
|
287
|
+
expect(recognize("UNKNOWN", true)).to be_false
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should detect the correct name, version and platform" do
|
291
|
+
# Google Chrome
|
292
|
+
expect(recognize("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.82 Safari/537.1")).to eq([:chrome, "21.0.1180.82", :osx])
|
293
|
+
expect(recognize("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5")).to eq([:chrome, "19.0.1084.9", :linux])
|
294
|
+
expect(recognize("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1")).to eq([:chrome, "13.0.782.215", :windows])
|
295
|
+
|
296
|
+
# Mozilla Firefox
|
297
|
+
expect(recognize("Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2")).to eq([:firefox, "15.0a2", :windows])
|
298
|
+
expect(recognize("Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1")).to eq([:firefox, "14.0.1", :linux])
|
299
|
+
expect(recognize("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b8) Gecko/20100101 Firefox/4.0b8")).to eq([:firefox, "4.0b8", :osx])
|
300
|
+
|
301
|
+
# Apple Safari
|
302
|
+
expect(recognize("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10")).to eq([:safari, "5.1.3", :osx])
|
303
|
+
expect(recognize("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-en) AppleWebKit/533.16 (KHTML, like Gecko) Version/4.1 Safari/533.16")).to eq([:safari, "4.1", :windows])
|
304
|
+
|
305
|
+
# Opera Mobile
|
306
|
+
expect(recognize("Opera/9.80 (Android 2.3.3; Linux; Opera Mobi/ADR-1111101157; U; es-ES) Presto/2.9.201 Version/11.50")).to eq([:opera_mobile, "11.50", :android])
|
307
|
+
expect(recognize("Mozilla/5.0 (S60; SymbOS; Opera Mobi/SYB-1103211396; U; es-LA; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 Opera 11.00")).to eq([:opera_mobile, "11.00", :symbian])
|
308
|
+
|
309
|
+
# Opera
|
310
|
+
expect(recognize("Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00")).to eq([:opera, "12.00", :windows])
|
311
|
+
expect(recognize("Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/5.0 Opera 11.11")).to eq([:opera, "11.11", :windows])
|
312
|
+
expect(recognize("Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52")).to eq([:opera, "11.52", :osx])
|
313
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; de) Opera 11.51")).to eq([:opera, "11.51", :windows])
|
314
|
+
|
315
|
+
# Microsoft Internet Explorer in compatibility view
|
316
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)")).to eq([:msie_compatibility, "10.0", :windows])
|
317
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET CLR 1.1.4322; .NET4.0C; Tablet PC 2.0)")).to eq([:msie_compatibility, "9.0", :windows])
|
318
|
+
expect(recognize("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Zune 3.0)")).to eq([:msie_compatibility, "8.0", :windows])
|
319
|
+
|
320
|
+
# Microsoft Internet Explorer
|
321
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64;0)")).to eq([:msie, "10.0", :windows])
|
322
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET CLR 1.1.4322; .NET4.0C; Tablet PC 2.0)")).to eq([:msie, "9.0", :windows])
|
323
|
+
expect(recognize("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Zune 3.0)")).to eq([:msie, "8.0", :windows])
|
324
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0; fr-FR)")).to eq([:msie, "7.0", :windows])
|
325
|
+
expect(recognize("Mozilla/4.01 (compatible; MSIE 6.0; Windows NT 5.1)")).to eq([:msie, "6.0", :windows])
|
326
|
+
|
327
|
+
# Netscape
|
328
|
+
expect(recognize("Mozilla/5.0 (Windows; U; Win 9x 4.90; SG; rv:1.9.2.4) Gecko/20101104 Netscape/9.1.0285")).to eq([:netscape, "9.1.0285", :windows])
|
329
|
+
expect(recognize("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.8pre) Gecko/20071001 Firefox/2.0.0.7 Navigator/9.0RC1")).to eq([:netscape, "9.0RC1", :osx])
|
330
|
+
|
331
|
+
# Apple iPhone
|
332
|
+
expect(recognize("Mozilla/5.0 (iPhone; U; fr; CPU iPhone OS 4_2_1 like Mac OS X; fr) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5")).to eq([:iphone, "5.0.2", :ios])
|
333
|
+
expect(recognize("Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B5097d Safari/6531.22.7")).to eq([:iphone, "4.0.5", :ios])
|
334
|
+
|
335
|
+
# Apple iPad
|
336
|
+
expect(recognize("Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko ) Version/5.1 Mobile/9B176 Safari/7534.48.3")).to eq([:ipad, "5.1", :ios])
|
337
|
+
expect(recognize("Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; nl-nl) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5")).to eq([:ipad, "5.0.2", :ios])
|
338
|
+
|
339
|
+
# Apple iPod Touch
|
340
|
+
expect(recognize("Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5")).to eq([:ipod, "5.0.2", :ios])
|
341
|
+
expect(recognize("Mozilla/5.0 (iPod; U; CPU iPhone OS 3_0 like Mac OS X; ja-jp) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16")).to eq([:ipod, "4.0", :ios])
|
342
|
+
|
343
|
+
# Android
|
344
|
+
expect(recognize("Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")).to eq([:android, "4.0.3", :android])
|
345
|
+
expect(recognize("Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; HTC_DesireS_S510e Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")).to eq([:android, "2.3.3", :android])
|
346
|
+
|
347
|
+
# RIM Blackberry
|
348
|
+
expect(recognize("Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+")).to eq([:blackberry, "7.1.0.346", :blackberry])
|
349
|
+
expect(recognize("Mozilla/5.0 (BlackBerry; U; BlackBerry 9700; pt) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.546 Mobile Safari/534.8+")).to eq([:blackberry, "6.0.0.546", :blackberry])
|
350
|
+
|
351
|
+
# Windows Phone
|
352
|
+
expect(recognize("Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i917)")).to eq([:windows_phone, "9.0", :windows_phone])
|
353
|
+
expect(recognize("Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; DELL; Venue Pro")).to eq([:windows_phone, "7.0", :windows_phone])
|
354
|
+
|
355
|
+
# Symbian
|
356
|
+
expect(recognize("Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaC6-00/20.0.042; Profile/MIDP-2.1 Configuration/CLDC-1.1; zh-hk) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.2.6.9 3gpp-gba")).to eq([:mobile, "7.2.6.9", :symbian])
|
357
|
+
|
358
|
+
# Amazon Kindle
|
359
|
+
expect(recognize("Mozilla/4.0 (compatible; Linux 2.6.22) NetFront/3.4 Kindle/2.0 (screen 600x800)")).to eq([:kindle, "2.0", :kindle])
|
360
|
+
expect(recognize("Mozilla/4.0 (compatible; Linux 2.6.10) NetFront/3.3 Kindle/1.0 (screen 600x800)")).to eq([:kindle, "1.0", :kindle])
|
361
|
+
|
362
|
+
# Sony Playstation Portable
|
363
|
+
expect(recognize("PSP (PlayStation Portable); 2.00")).to eq([:psp, "2.00", :psp])
|
364
|
+
expect(recognize("Mozilla/4.0 (PSP (PlayStation Portable); 2.00)")).to eq([:psp, "2.00", :psp])
|
365
|
+
|
366
|
+
# Sony Playstation 3
|
367
|
+
expect(recognize("Mozilla/5.0 (PLAYSTATION 3; 3.55)")).to eq([:ps3, "3.55", :ps3])
|
368
|
+
expect(recognize("Mozilla/5.0 (PLAYSTATION 3; 1.70)")).to eq([:ps3, "1.70", :ps3])
|
369
|
+
|
370
|
+
# Nintendo Wii
|
371
|
+
expect(recognize("Opera/9.30 (Nintendo Wii; U; ; 2071; Wii Shop Channel/1.0; en)")).to eq([:opera, "2071", :wii])
|
372
|
+
expect(recognize("Opera/9.30 (Nintendo Wii; U; ; 2047-7;pt-br)")).to eq([:opera, "2047", :wii])
|
373
|
+
|
374
|
+
# Mobile browsers
|
375
|
+
expect(recognize("Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; Sprint;PPC-i830; PPC; 240x320)")).to eq([:mobile, "4.0", :windows])
|
376
|
+
|
377
|
+
# Generic Webkit
|
378
|
+
expect(recognize("Midori/0.2.0 (X11; Linux i686; U; de-de) WebKit/531.2+")).to eq([:webkit, "531.2", :linux])
|
379
|
+
expect(recognize("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-de) AppleWebKit/531.21.8 (KHTML, like Gecko) NetNewsWire/3.2.3")).to eq([:webkit, "531.21.8", :osx])
|
380
|
+
|
381
|
+
# Generic Gecko
|
382
|
+
expect(recognize("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9a3pre) Gecko/20070330")).to eq([:gecko, "1.9a3pre", :linux])
|
383
|
+
expect(recognize("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko")).to eq([:gecko, "1.9.2a1pre", :windows])
|
384
|
+
|
385
|
+
# QuickTime
|
386
|
+
expect(recognize("QuickTime/7.6.2 (qtver=7.6.2;os=Windows NT 5.1Service Pack 3)")).to eq([:quicktime, "7.6.2", :windows])
|
387
|
+
expect(recognize("QuickTime/7.6 (qtver=7.6;cpu=IA32;os=Mac 10,5,7)")).to eq([:quicktime, "7.6", :osx])
|
388
|
+
|
389
|
+
# CoreMedia
|
390
|
+
expect(recognize("Apple iPhone v1.1.1 CoreMedia v1.0.0.3A110a")).to eq([:coremedia, "1.0.0.3A110a", :ios])
|
391
|
+
expect(recognize("Apple Mac OS X v10.6.6 CoreMedia v1.0.0.10J567")).to eq([:coremedia, "1.0.0.10J567", :osx])
|
392
|
+
|
393
|
+
# GENERICS
|
394
|
+
browser.class.register_browser(:generic, "NAME", "NAME", "NONE")
|
395
|
+
browser.class.register_platform(:generic, "NAME", "NONE")
|
396
|
+
expect(recognize("NAME")).to eq([:generic, "NAME", :generic])
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
describe "#parse_accept_language" do
|
401
|
+
it "should parse languages" do
|
402
|
+
expect(browser.parse_accept_language).to eq([])
|
403
|
+
expect(browser.parse_accept_language(nil)).to eq([])
|
404
|
+
expect(browser.parse_accept_language("IT")).to eq(["it"])
|
405
|
+
expect(browser.parse_accept_language("IT;q=0.7, EN;q=0.3")).to eq(["it", "en"])
|
406
|
+
expect(browser.parse_accept_language("It;q=0.7, eN;q=0.3")).to eq(["it", "en"])
|
407
|
+
expect(browser.parse_accept_language("IT;q=0.7, ")).to eq(["it"])
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
describe "#readable_name" do
|
412
|
+
before(:each) do
|
413
|
+
Brauser::Browser.register_default_browsers
|
414
|
+
end
|
415
|
+
|
416
|
+
it "should return the correct name" do
|
417
|
+
browser.instance_variable_set("@name", :msie)
|
418
|
+
expect(browser.readable_name).to eq("Microsoft Internet Explorer")
|
419
|
+
|
420
|
+
browser.instance_variable_set("@name", :chrome)
|
421
|
+
expect(browser.readable_name).to eq("Google Chrome")
|
422
|
+
end
|
423
|
+
|
424
|
+
it "should return a default name" do
|
425
|
+
browser.instance_variable_set("@name", :none)
|
426
|
+
expect(browser.readable_name).to eq("Unknown Browser")
|
427
|
+
end
|
428
|
+
end
|
429
|
+
|
430
|
+
describe "#platform_name" do
|
431
|
+
before(:each) do
|
432
|
+
Brauser::Browser.register_default_platforms
|
433
|
+
end
|
434
|
+
|
435
|
+
it "should return the correct name" do
|
436
|
+
browser.instance_variable_set("@platform", :windows)
|
437
|
+
expect(browser.platform_name).to eq("Microsoft Windows")
|
438
|
+
|
439
|
+
browser.instance_variable_set("@platform", :ios)
|
440
|
+
expect(browser.platform_name).to eq("Apple iOS")
|
441
|
+
end
|
442
|
+
|
443
|
+
it "should return a default name" do
|
444
|
+
browser.instance_variable_set("@platform", :none)
|
445
|
+
expect(browser.platform_name).to eq("Unknown Platform")
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
describe "#is" do
|
450
|
+
it "should at first call #parse_agent" do
|
451
|
+
browser.instance_variable_set("@name", nil)
|
452
|
+
browser.should_receive(:parse_agent)
|
453
|
+
browser.is
|
454
|
+
end
|
455
|
+
|
456
|
+
it "should recognized names" do
|
457
|
+
browser.instance_variable_set("@name", :chrome)
|
458
|
+
expect(browser.is).to be_true_query
|
459
|
+
expect(browser.is(nil)).to be_true_query
|
460
|
+
expect(browser.is(:chrome)).to be_true_query
|
461
|
+
expect(browser.is(:capable)).to be_true_query
|
462
|
+
|
463
|
+
browser.instance_variable_set("@name", :ipad)
|
464
|
+
expect(browser.is([:tablet, :blackberry])).to be_false_query
|
465
|
+
|
466
|
+
browser.instance_variable_set("@name", :msie)
|
467
|
+
browser.instance_variable_set("@version", "7.0")
|
468
|
+
expect(browser.is(:capable)).to be_false_query
|
469
|
+
browser.instance_variable_set("@version", "9.0")
|
470
|
+
expect(browser.is(:capable)).to be_true_query
|
471
|
+
|
472
|
+
browser.should_receive(:v?).exactly(2).and_return(true)
|
473
|
+
browser.should_receive(:on?).and_return(false)
|
474
|
+
expect(browser.is(:capable, {:gte => 8})).to be_true_query
|
475
|
+
expect(browser.is(:capable, {:gt => 10}, [:windows])).to be_false_query
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
describe "#is?" do
|
480
|
+
it "should call the query and then fetch the result" do
|
481
|
+
browser.instance_variable_set("@name", :msie)
|
482
|
+
|
483
|
+
browser.should_receive("is").exactly(2).and_return(Brauser::Query.new(browser, true))
|
484
|
+
::Brauser::Query.any_instance.should_receive(:result).exactly(2).and_return(true)
|
485
|
+
|
486
|
+
expect(browser.is?(:chrome)).to be_true
|
487
|
+
expect(browser.is?(:msie)).to be_true
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
describe "#v" do
|
492
|
+
it "should at first call #parse_agent" do
|
493
|
+
browser.instance_variable_set("@version", nil)
|
494
|
+
browser.should_receive(:parse_agent)
|
495
|
+
browser.v
|
496
|
+
end
|
497
|
+
|
498
|
+
it "should compare browser versions" do
|
499
|
+
browser.instance_variable_set("@version", "3.4.5")
|
500
|
+
|
501
|
+
expect(browser.v).to be_true_query
|
502
|
+
expect(browser.v(nil)).to be_true_query
|
503
|
+
expect(browser.v(:lt => 7)).to be_true_query
|
504
|
+
expect(browser.v(:lte => 3)).to be_false_query
|
505
|
+
expect(browser.v(:eq => 3)).to be_false_query
|
506
|
+
expect(browser.v(:gte => 3)).to be_true_query
|
507
|
+
expect(browser.v(:gt => 4)).to be_false_query
|
508
|
+
expect(browser.v(:gt => 3.5)).to be_false_query
|
509
|
+
expect(browser.v(:foo => "3")).to be_false_query
|
510
|
+
expect(browser.v(">= 3.5")).to be_false_query
|
511
|
+
expect(browser.v("< 7 && > 3")).to be_true_query
|
512
|
+
expect(browser.v("< 7 && > 3 && FOO NO")).to be_false_query
|
513
|
+
expect(browser.v("<= 7 && >= 3 && FOO NO")).to be_false_query
|
514
|
+
expect(browser.v("= 7 && == 3 && FOO NO")).to be_false_query
|
515
|
+
end
|
516
|
+
end
|
517
|
+
|
518
|
+
describe "#v?" do
|
519
|
+
it "should call the query and then fetch the result" do
|
520
|
+
browser.instance_variable_set("@version", "7.0")
|
521
|
+
|
522
|
+
browser.should_receive("v").exactly(2).and_return(Brauser::Query.new(browser, true))
|
523
|
+
::Brauser::Query.any_instance.should_receive(:result).exactly(2).and_return(true)
|
524
|
+
|
525
|
+
expect(browser.v?(">= 8")).to be_true
|
526
|
+
expect(browser.v?(">= 7")).to be_true
|
527
|
+
end
|
528
|
+
|
529
|
+
end
|
530
|
+
|
531
|
+
describe "#on" do
|
532
|
+
it "should at first call #parse_agent" do
|
533
|
+
browser.instance_variable_set("@platform", nil)
|
534
|
+
browser.should_receive(:parse_agent)
|
535
|
+
browser.on
|
536
|
+
end
|
537
|
+
|
538
|
+
it "should detect platforms" do
|
539
|
+
browser.instance_variable_set("@platform", :windows)
|
540
|
+
expect(browser.on).to be_true_query
|
541
|
+
expect(browser.on(:windows)).to be_true_query
|
542
|
+
expect(browser.on([:osx, :linux])).to be_false_query
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
describe "#on?" do
|
547
|
+
it "should call the query and then fetch the result" do
|
548
|
+
browser.instance_variable_set("@platform", :windows)
|
549
|
+
|
550
|
+
browser.should_receive("on").exactly(2).and_return(Brauser::Query.new(browser, true))
|
551
|
+
::Brauser::Query.any_instance.should_receive(:result).exactly(2).and_return(true)
|
552
|
+
|
553
|
+
expect(browser.on?(:osx)).to be_true
|
554
|
+
expect(browser.on?(:windows)).to be_true
|
555
|
+
end
|
556
|
+
end
|
557
|
+
|
558
|
+
describe "#accepts" do
|
559
|
+
it "should at first call #parse_accept_language" do
|
560
|
+
browser.instance_variable_set("@languages", nil)
|
561
|
+
browser.should_receive(:parse_accept_language)
|
562
|
+
browser.accepts
|
563
|
+
end
|
564
|
+
|
565
|
+
it "should detect platforms" do
|
566
|
+
browser.instance_variable_set("@languages", [])
|
567
|
+
expect(browser.accepts).to be_false_query
|
568
|
+
expect(browser.accepts("it")).to be_false_query
|
569
|
+
expect(browser.accepts(["it", "en"])).to be_false_query
|
570
|
+
|
571
|
+
browser.instance_variable_set("@languages", ["it", "en"])
|
572
|
+
expect(browser.accepts(nil)).to be_false_query
|
573
|
+
expect(browser.accepts([])).to be_false_query
|
574
|
+
expect(browser.accepts("it")).to be_true_query
|
575
|
+
expect(browser.accepts(["it", "en"])).to be_true_query
|
576
|
+
expect(browser.accepts(["it", "es"])).to be_true_query
|
577
|
+
expect(browser.accepts(["es", "en"])).to be_true_query
|
578
|
+
expect(browser.accepts("es")).to be_false_query
|
579
|
+
expect(browser.accepts(["es", "de"])).to be_false_query
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
describe "#accepts?" do
|
584
|
+
it "should call the query and then fetch the result" do
|
585
|
+
browser.instance_variable_set("@language", ["it"])
|
586
|
+
|
587
|
+
browser.should_receive("accepts").exactly(2).and_return(Brauser::Query.new(browser, true))
|
588
|
+
::Brauser::Query.any_instance.should_receive(:result).exactly(2).and_return(true)
|
589
|
+
|
590
|
+
expect(browser.accepts?("it")).to be_true
|
591
|
+
expect(browser.accepts?("en")).to be_true
|
592
|
+
end
|
593
|
+
end
|
594
|
+
|
595
|
+
describe "accepts dynamic finders by" do
|
596
|
+
it "calling the right method" do
|
597
|
+
browser.should_receive(:is?).with("opera_mobile", {}, []).and_return(true)
|
598
|
+
browser.should_receive(:v?).with("< 3").and_return(true)
|
599
|
+
browser.should_receive(:on?).with("windows").and_return(true)
|
600
|
+
|
601
|
+
expect(browser.is_opera_mobile__v_lt_3__on_windows?).to be_true
|
602
|
+
end
|
603
|
+
|
604
|
+
it "returning as query" do
|
605
|
+
expect(browser.is_opera_mobile__v_lt_3__on_windows).to be_a(Brauser::Query)
|
606
|
+
end
|
607
|
+
|
608
|
+
it "returning as boolean" do
|
609
|
+
expect(browser.is_opera_mobile__v_lt_3__on_windows?).to be_false
|
610
|
+
end
|
611
|
+
|
612
|
+
it "correctly analyzing version" do
|
613
|
+
browser.should_receive(:is?).with("opera_mobile", {}, []).at_least(1).and_return(true)
|
614
|
+
|
615
|
+
browser.should_receive(:v?).with("<= 3").and_return(true)
|
616
|
+
expect(browser.is_opera_mobile__v_lte_3).to be_true
|
617
|
+
|
618
|
+
browser.should_receive(:v?).with("< 3 && >= 3").and_return(false)
|
619
|
+
expect(browser.is_opera_mobile__v_lt_3_and_gte_3?).to be_false
|
620
|
+
|
621
|
+
browser.should_receive(:v?).with("&& >= 3").and_return(false)
|
622
|
+
expect(browser.is_opera_mobile__v_and_gte_3?).to be_false
|
623
|
+
|
624
|
+
browser.should_receive(:v?).with("< 3 &&").and_return(false)
|
625
|
+
expect(browser.is_opera_mobile__v_lt_3_and?).to be_false
|
626
|
+
|
627
|
+
browser.should_receive(:v?).with("> 2").and_return(true)
|
628
|
+
expect(browser.is_opera_mobile__v_gt_2?).to be_true
|
629
|
+
|
630
|
+
browser.should_receive(:v?).with("== 3.4.5alpha.is.3").and_return(false)
|
631
|
+
expect(browser.is_opera_mobile__v_eq_3_4_5alpha_is_3?).to be_false
|
632
|
+
end
|
633
|
+
|
634
|
+
it "calling methods unless the result is valid and invalidating a query" do
|
635
|
+
browser.should_receive(:is?).with("opera_mobile", {}, []).and_return(true)
|
636
|
+
browser.should_not_receive(:v?)
|
637
|
+
browser.should_not_receive(:on?)
|
638
|
+
|
639
|
+
expect{ browser.is_opera_mobile__vv_lt_3__on_windows? }.to raise_error(NoMethodError)
|
640
|
+
end
|
641
|
+
|
642
|
+
it "raising an exception for invalid finder" do
|
643
|
+
browser.should_receive(:is?).with("opera_mobile", {}, []).and_return(true)
|
644
|
+
|
645
|
+
expect{ browser._is__a? }.to raise_error(NoMethodError)
|
646
|
+
expect{ browser.aa? }.to raise_error(NoMethodError)
|
647
|
+
expect{ browser.isa_opera_mobile__vv_lt_3__on_windows? }.to raise_error(NoMethodError)
|
648
|
+
expect{ browser.is_opera_mobile__vv_lt_3__on_windows? }.to raise_error(NoMethodError)
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
describe "#classes" do
|
653
|
+
before(:each) do
|
654
|
+
browser.instance_variable_set("@name", :chrome)
|
655
|
+
browser.instance_variable_set("@version", "1.2.A.4")
|
656
|
+
browser.instance_variable_set("@platform", :osx)
|
657
|
+
end
|
658
|
+
|
659
|
+
it "should return requested classes" do
|
660
|
+
expect(browser.classes(false)).to eq(["chrome", "version-1", "version-1_2", "version-1_2_A", "version-1_2_A_4", "platform-osx"])
|
661
|
+
expect(browser.classes(false, "name-")).to eq(["name-chrome", "version-1", "version-1_2", "version-1_2_A", "version-1_2_A_4", "platform-osx"])
|
662
|
+
expect(browser.classes(false, true, "v-")).to eq(["chrome", "v-1", "v-1_2", "v-1_2_A", "v-1_2_A_4", "platform-osx"])
|
663
|
+
expect(browser.classes(false, true, true, "p-")).to eq(["chrome", "version-1", "version-1_2", "version-1_2_A", "version-1_2_A_4", "p-osx"])
|
664
|
+
expect(browser.classes(false, false)).to eq(["version-1", "version-1_2", "version-1_2_A", "version-1_2_A_4", "platform-osx"])
|
665
|
+
expect(browser.classes(false, true, false)).to eq(["chrome", "platform-osx"])
|
666
|
+
expect(browser.classes(false, true, true, false)).to eq(["chrome", "version-1", "version-1_2", "version-1_2_A", "version-1_2_A_4"])
|
667
|
+
end
|
668
|
+
|
669
|
+
it "should return as a string" do
|
670
|
+
expect(browser.classes).to eq("chrome version-1 version-1_2 version-1_2_A version-1_2_A_4 platform-osx")
|
671
|
+
expect(browser.classes("@")).to eq("chrome@version-1@version-1_2@version-1_2_A@version-1_2_A_4@platform-osx")
|
672
|
+
end
|
673
|
+
|
674
|
+
it "should transform name" do
|
675
|
+
expect(browser.classes(" ", true, false, false) { |name| name.to_s.upcase }).to eq("CHROME")
|
676
|
+
end
|
677
|
+
end
|
678
|
+
|
679
|
+
describe "#to_s" do
|
680
|
+
it "should forward to #classes" do
|
681
|
+
browser.should_receive(:classes)
|
682
|
+
browser.to_s
|
683
|
+
end
|
684
|
+
end
|
685
|
+
end
|