Class: Dommy::Navigator

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/navigator.rb

Overview

window.navigator — exposes browser-agent metadata plus clipboard / permissions sub-objects. Dommy returns sensible defaults (Dommy as user agent, "en" language, online=true) that tests can override.

Constant Summary collapse

DEFAULT_USER_AGENT =
"Mozilla/5.0 (Dommy) Ruby"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(window) ⇒ Navigator

Returns a new instance of Navigator.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dommy/navigator.rb', line 14

def initialize(window)
  @window = window
  @user_agent = DEFAULT_USER_AGENT
  @language = "en"
  @languages = ["en"].freeze
  @platform = "Dommy"
  @vendor = "Dommy"
  @on_line = true
  @cookie_enabled = true
  @hardware_concurrency = 8 # logical CPU count reported to JS
  @max_touch_points = 0     # 0 => not a touch device
  @clipboard = Clipboard.new(window)
  @permissions = Permissions.new(window)
  @geolocation = Geolocation.new(window)
  @vibration_log = []
  @wake_lock = WakeLock.new(window)
  @locks = LockManager.new(window)
  @storage = StorageManager.new(window)
end

Instance Attribute Details

#clipboardObject (readonly)

Returns the value of attribute clipboard.



34
35
36
# File 'lib/dommy/navigator.rb', line 34

def clipboard
  @clipboard
end

Returns the value of attribute cookie_enabled.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def cookie_enabled
  @cookie_enabled
end

#geolocationObject (readonly)

Returns the value of attribute geolocation.



34
35
36
# File 'lib/dommy/navigator.rb', line 34

def geolocation
  @geolocation
end

#hardware_concurrencyObject

Returns the value of attribute hardware_concurrency.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def hardware_concurrency
  @hardware_concurrency
end

#languageObject

Returns the value of attribute language.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def language
  @language
end

#languagesObject

Returns the value of attribute languages.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def languages
  @languages
end

#locksObject (readonly)

Returns the value of attribute locks.



34
35
36
# File 'lib/dommy/navigator.rb', line 34

def locks
  @locks
end

#max_touch_pointsObject

Returns the value of attribute max_touch_points.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def max_touch_points
  @max_touch_points
end

#on_lineObject

Returns the value of attribute on_line.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def on_line
  @on_line
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



34
35
36
# File 'lib/dommy/navigator.rb', line 34

def permissions
  @permissions
end

#platformObject

Returns the value of attribute platform.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def platform
  @platform
end

#storageObject (readonly)

Returns the value of attribute storage.



34
35
36
# File 'lib/dommy/navigator.rb', line 34

def storage
  @storage
end

#user_agentObject

Returns the value of attribute user_agent.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def user_agent
  @user_agent
end

#vendorObject

Returns the value of attribute vendor.



11
12
13
# File 'lib/dommy/navigator.rb', line 11

def vendor
  @vendor
end

#wake_lockObject (readonly)

Returns the value of attribute wake_lock.



34
35
36
# File 'lib/dommy/navigator.rb', line 34

def wake_lock
  @wake_lock
end

Instance Method Details

#[](key) ⇒ Object



82
83
84
# File 'lib/dommy/navigator.rb', line 82

def [](key)
  __js_get__(key.to_s)
end

#[]=(k, v) ⇒ Object



86
87
88
# File 'lib/dommy/navigator.rb', line 86

def []=(k, v)
  __js_set__(k.to_s, v)
end

#__js_call__(method, args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dommy/navigator.rb', line 133

def __js_call__(method, args)
  case method
  when "share"
    share(args[0])
  when "canShare"
    can_share(args[0])
  when "vibrate"
    vibrate(args[0])
  when "getBattery"
    get_battery
  when "sendBeacon"
    send_beacon(args[0], args[1])
  end
end

#__js_get__(key) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dommy/navigator.rb', line 90

def __js_get__(key)
  case key
  when "userAgent"
    @user_agent
  when "language"
    @language
  when "languages"
    @languages
  when "platform"
    @platform
  when "vendor"
    @vendor
  when "onLine"
    @on_line
  when "cookieEnabled"
    @cookie_enabled
  when "hardwareConcurrency"
    @hardware_concurrency
  when "maxTouchPoints"
    @max_touch_points
  when "clipboard"
    @clipboard
  when "permissions"
    @permissions
  when "geolocation"
    @geolocation
  when "wakeLock"
    @wake_lock
  when "locks"
    @locks
  when "storage"
    @storage
  else
    # An unknown navigator property is genuinely absent: JS `undefined` value
    # AND `"x" in navigator` false — so feature detection like
    # `navigator.scheduling !== undefined` / `"serviceWorker" in navigator`
    # both take the not-supported path instead of dereferencing null.
    Bridge::ABSENT
  end
end

#__js_set__(_key, _value) ⇒ Object



148
149
150
# File 'lib/dommy/navigator.rb', line 148

def __js_set__(_key, _value)
  Bridge::UNHANDLED
end

#__test_last_shared__Object



70
71
72
# File 'lib/dommy/navigator.rb', line 70

def __test_last_shared__
  @last_shared
end

#__test_vibration_log__Object



66
67
68
# File 'lib/dommy/navigator.rb', line 66

def __test_vibration_log__
  @vibration_log.dup
end

#can_share(_data = nil) ⇒ Object Also known as: canShare



43
44
45
# File 'lib/dommy/navigator.rb', line 43

def can_share(_data = nil)
  true
end

#get_batteryObject Also known as: getBattery

Battery Status API stub. Returns a Promise resolving to a fixed BatteryManager snapshot.



76
77
78
# File 'lib/dommy/navigator.rb', line 76

def get_battery
  PromiseValue.resolve(@window, BatteryManager.new)
end

#send_beacon(_url, _data = nil) ⇒ Object Also known as: sendBeacon

navigator.sendBeacon(url, data) — fire-and-forget POST used for analytics. Dommy does not emit the request (no background egress); it just reports success so callers that feature-detect sendBeacon take the beacon path.



52
53
54
# File 'lib/dommy/navigator.rb', line 52

def send_beacon(_url, _data = nil)
  true
end

#share(data = nil) ⇒ Object

Web Share API. Returns a Promise; tests can inspect __test_last_shared__ to verify what was offered.



38
39
40
41
# File 'lib/dommy/navigator.rb', line 38

def share(data = nil)
  @last_shared = data
  PromiseValue.resolve(@window, nil)
end

#vibrate(pattern) ⇒ Object

Vibration API. No-op in dommy, but the requested pattern is recorded so tests can assert "we asked to vibrate".



60
61
62
63
64
# File 'lib/dommy/navigator.rb', line 60

def vibrate(pattern)
  list = pattern.is_a?(Array) ? pattern : [pattern]
  @vibration_log << list.map(&:to_i)
  true
end