Class: Accessibility::Console::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/project/motion-accessibility-console/tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tree

Returns a new instance of Tree.



7
8
9
10
11
# File 'lib/project/motion-accessibility-console/tree.rb', line 7

def initialize(options={})
@view=options[:view]
@subviews=options[:subviews]||[]
@superview=options[:superview]
end

Instance Attribute Details

#subviewsObject

Returns the value of attribute subviews.



5
6
7
# File 'lib/project/motion-accessibility-console/tree.rb', line 5

def subviews
  @subviews
end

#superviewObject

Returns the value of attribute superview.



5
6
7
# File 'lib/project/motion-accessibility-console/tree.rb', line 5

def superview
  @superview
end

#viewObject

Returns the value of attribute view.



5
6
7
# File 'lib/project/motion-accessibility-console/tree.rb', line 5

def view
  @view
end

Class Method Details

.accessible_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/project/motion-accessibility-console/tree.rb', line 86

def self.accessible_view?(view)
result= view.accessibility_element?||view.accessibility_label||view.accessibility_value||view.accessibility_traits
(result)?true:false
end

.build(view = nil, superview = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/project/motion-accessibility-console/tree.rb', line 106

def self.build(view=nil, superview=nil)
A11y::Test::Data[:quiet]=true
tree=self.new
view=UIApplication.sharedApplication.keyWindow if view.nil?
subviews=[]
if A11y::Element.container?(view)
  view.each_accessibility_element do |element|
subview_tree=self.build(element, tree)
subviews<<subview_tree
  end
  elsif view.respond_to?(:subviews)
view.subviews.each do |subview|
subview_tree=self.build(subview, tree)
if self.ignore_view?(subview)
subview_tree.subviews.each {|v| v.superview=tree}
subviews=subviews+subview_tree.subviews
else
subviews<<subview_tree
end
end
end
tree.view=view
tree.subviews=subviews
tree.superview=superview
A11y::Test::Data[:quiet]=false
tree
end

.ignore_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/project/motion-accessibility-console/tree.rb', line 91

def self.ignore_view?(view)
return true if view.subviews.empty?&&!view.accessible?
if view.superview
sv=view.superview
while sv&&self.ignore_view?(sv)
sv=sv.superview
end
return true if A11y::Console.touchable_type(sv)
return true if view.class==UIImageView&&A11y::Ignored_ImageViews.member?(sv.class.to_s)
end
class_name=view.class.to_s
return true if class_name=~/^_/
A11y::Ignored_Views.member?(class_name)
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/project/motion-accessibility-console/tree.rb', line 21

def ==(other)
  return false if other.nil?
return false if self.superview&&other.superview&&self.superview.view!=other.superview.view
return false unless self.view==other.view
return false unless self.subviews.size==other.subviews.size
self.subviews.each_index {|index| return false unless self.subviews[index]==other.subviews[index]}
return true
end

#[](n) ⇒ Object



30
31
32
33
34
# File 'lib/project/motion-accessibility-console/tree.rb', line 30

def [](n)
  a=[@view]
  a+=@subviews if @subviews
  a[n]
end

#browsable_nodesObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/project/motion-accessibility-console/tree.rb', line 36

def browsable_nodes
nodes=[@superview]
if @subviews
if A11y::Reverse_Views.member?(@view.class.to_s)
nodes+=@subviews.reverse
else
nodes+=@subviews
end
end
nodes
end

#copyObject



13
14
15
16
17
18
19
# File 'lib/project/motion-accessibility-console/tree.rb', line 13

def copy
other=A11y::Console::Tree.new
other.superview=self.superview if superview
other.view=self.view if view
self.subviews.each {|subview| other.subviews<<subview.clone}
other
end

#display_view(index = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/project/motion-accessibility-console/tree.rb', line 57

def display_view(index=nil)
display=Array.new
control=@view.class.to_s
control=nil if A11y::View_Names[control]
control="Superview #{control}" if index==0
control.sub!(/UITableViewCell(AccessibilityElement)?/, "Table cell")
if @view.class==UITableViewCell
name=view.text
elsif @view.class==UITableViewCellAccessibilityElement
name=view.tableViewCell.text
elsif @view.class==UITextField
name=@view.text
else
name=A11y::View_Names[@view.class.to_s]||@view.accessibility_value||@view.accessibility_label if @view.accessibility_element?||view.superclass==UIControl
end
display<<index.to_s
display<<"Touchable" if A11y::Console.touchable_type(@view)
display<<control if control
display<<name if name
if index
if index>0 and  not(@subviews.empty?)
indicator="with #{@subviews.length} subview"
indicator+="s" if @subviews.length>1
end
   display<<indicator
end
display.join(" ")
end

#find(request) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/project/motion-accessibility-console/tree.rb', line 134

def find(request)
found=nil
if request.kind_of?(Fixnum)
raise "Invalid number" unless request>=0&&request<browsable_nodes.length
found=browsable_nodes[request]
elsif request.kind_of?(String)
results=[]
browsable_nodes.each do |node|
next if node.nil?
next unless node.view.accessibility_label
pattern=Regexp.new(request,true)
compare=node.view.accessibility_label=~pattern
next if compare.nil?
if node.view.accessibility_label.downcase==request.downcase
return node
else
results<<node
end
end
raise "\"#{request}\" could refer to more than one view." if results.length>1
found=results.first
else
raise "Unknown request: #{request}: #{request.class}" unless request.respond_to?(:superview)
found=request
end
found
end

#inspectObject



48
49
50
51
52
53
54
55
# File 'lib/project/motion-accessibility-console/tree.rb', line 48

def inspect
nodes=@subviews.collect {|tree| tree.inspect}
return @view.class.to_s if @subviews.empty?
result="[#{@view.class}"
result+=" #{nodes.join(" ")}" unless nodes.empty?
result+="]"
result
end