Class: Dommy::Navigator

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(window) ⇒ Navigator

Returns a new instance of Navigator.



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

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
  @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.



31
32
33
# File 'lib/dommy/navigator.rb', line 31

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.



31
32
33
# File 'lib/dommy/navigator.rb', line 31

def geolocation
  @geolocation
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.



31
32
33
# File 'lib/dommy/navigator.rb', line 31

def locks
  @locks
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.



31
32
33
# File 'lib/dommy/navigator.rb', line 31

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.



31
32
33
# File 'lib/dommy/navigator.rb', line 31

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.



31
32
33
# File 'lib/dommy/navigator.rb', line 31

def wake_lock
  @wake_lock
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(k, v) ⇒ Object



74
75
76
# File 'lib/dommy/navigator.rb', line 74

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

#__js_call__(method, args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dommy/navigator.rb', line 109

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
  end
end

#__js_get__(key) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dommy/navigator.rb', line 78

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 "clipboard"
    @clipboard
  when "permissions"
    @permissions
  when "geolocation"
    @geolocation
  when "wakeLock"
    @wake_lock
  when "locks"
    @locks
  when "storage"
    @storage
  end
end

#__js_set__(_key, _value) ⇒ Object



122
123
124
# File 'lib/dommy/navigator.rb', line 122

def __js_set__(_key, _value)
  nil
end

#__last_shared__Object



58
59
60
# File 'lib/dommy/navigator.rb', line 58

def __last_shared__
  @last_shared
end

#__vibration_log__Object



54
55
56
# File 'lib/dommy/navigator.rb', line 54

def __vibration_log__
  @vibration_log.dup
end

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



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

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.



64
65
66
# File 'lib/dommy/navigator.rb', line 64

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

#share(data = nil) ⇒ Object

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



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

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”.



48
49
50
51
52
# File 'lib/dommy/navigator.rb', line 48

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