Class: Applitools::Region

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/applitools/core/region.rb

Direct Known Subclasses

AccessibilityRegion, FloatingRegion

Constant Summary collapse

EMPTY =
Region.new(0, 0, 0, 0)
DEFAULT_SUBREGIONS_INTERSECTION =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, top, width, height) ⇒ Region

Returns a new instance of Region.



20
21
22
23
24
25
26
# File 'lib/applitools/core/region.rb', line 20

def initialize(left, top, width, height)
  @left = left.round
  @top = top.round
  @width = width.round
  @height = height.round
  @padding = Applitools::PaddingBounds::ZERO_PADDING
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



7
8
9
# File 'lib/applitools/core/region.rb', line 7

def height
  @height
end

#leftObject Also known as: x

Returns the value of attribute left.



7
8
9
# File 'lib/applitools/core/region.rb', line 7

def left
  @left
end

#padding(padding = nil) ⇒ Applitools::Region

Sets padding for a current region. If called without any argument, all paddings will be set to 0

Parameters:

Returns:



146
147
148
# File 'lib/applitools/core/region.rb', line 146

def padding
  @padding
end

#topObject Also known as: y

Returns the value of attribute top.



7
8
9
# File 'lib/applitools/core/region.rb', line 7

def top
  @top
end

#widthObject

Returns the value of attribute width.



7
8
9
# File 'lib/applitools/core/region.rb', line 7

def width
  @width
end

Class Method Details

.from_location_size(location, size) ⇒ Object



15
16
17
# File 'lib/applitools/core/region.rb', line 15

def from_location_size(location, size)
  new location.x, location.y, size.width, size.height
end

.sub_regions_with_fixed_size(container_region, sub_region) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/applitools/core/region.rb', line 166

def sub_regions_with_fixed_size(container_region, sub_region)
  Applitools::ArgumentGuard.not_nil container_region, 'container_region'
  Applitools::ArgumentGuard.not_nil sub_region, 'sub_region'

  Applitools::ArgumentGuard.greater_than_zero(sub_region.width, 'sub_region.width')
  Applitools::ArgumentGuard.greater_than_zero(sub_region.height, 'sub_region.height')

  sub_region_width = sub_region.width
  sub_region_height = sub_region.height

  # Normalizing.
  sub_region_width = container_region.width if sub_region_width > container_region.width
  sub_region_height = container_region.height if sub_region_height > container_region.height

  if sub_region_width == container_region.width && sub_region_height == container_region.height
    return Enumerator(1) do |y|
      y << [sub_region, EMPTY]
    end
  end

  current_top = container_region.top
  bottom = container_region.top + container_region.height - 1
  right = container_region.left + container_region.width - 1
  Enumerator.new do |y|
    while current_top <= bottom
      current_top = (bottom - sub_region_height) + 1 if current_top + sub_region_height > bottom
      current_left = container_region.left
      while current_left <= right
        current_left = (rught - sub_region_width) + 1 if current_left + sub_region_width > right
        y << [new(current_left, current_top, sub_region_width, sub_region_height), EMPTY]
        current_left += sub_region_width
      end
      current_top += sub_region_height
    end
  end
end

.sub_regions_with_varying_size(container_region, sub_region, intersection = DEFAULT_SUBREGIONS_INTERSECTION) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/applitools/core/region.rb', line 203

def sub_regions_with_varying_size(container_region, sub_region, intersection = DEFAULT_SUBREGIONS_INTERSECTION)
  Applitools::ArgumentGuard.not_nil container_region, 'container_region'
  Applitools::ArgumentGuard.not_nil sub_region, 'sub_region'

  Applitools::ArgumentGuard.greater_than_zero(sub_region.width, 'sub_region.width')
  Applitools::ArgumentGuard.greater_than_zero(sub_region.height, 'sub_region.height')

  current_top = container_region.top
  bottom = container_region.top + container_region.height
  right = container_region.left + container_region.width
  top_intersect = 0

  Enumerator.new do |y|
    while current_top < bottom
      current_bottom = current_top + sub_region.height
      current_bottom = bottom if current_bottom > bottom
      current_left = container_region.left
      left_intersect = 0
      while current_left < right
        current_right = current_left + sub_region.width
        current_right = right if current_right > right

        current_height = current_bottom - current_top
        current_width = current_right - current_left

        y << [
          new(current_left, current_top, current_width, current_height),
          new(left_intersect, top_intersect, left_intersect, top_intersect)
        ]

        current_left += sub_region.width - intersection * 2
        left_intersect = intersection if left_intersect.zero?
      end
      current_top += sub_region.height - intersection * 2
      top_intersect = intersection if top_intersect.zero?
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/applitools/core/region.rb', line 133

def ==(other)
  return false unless other.is_a? self.class
  size_location_match = left == other.left && top == other.top && width == other.width && height == other.height
  padding_match = padding_left == other.padding_left &&
    padding_top == other.padding_top &&
    padding_right == other.padding_right &&
    padding_bottom == other.padding_bottom
  size_location_match && padding_match
end

#[](value) ⇒ Object



108
109
110
# File 'lib/applitools/core/region.rb', line 108

def [](value)
  send value if respond_to? value
end

#bottomObject



58
59
60
# File 'lib/applitools/core/region.rb', line 58

def bottom
  top + height
end

#contains?(other_left, other_top) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/applitools/core/region.rb', line 85

def contains?(other_left, other_top)
  other_left >= left && other_left <= right && other_top >= top && other_top <= bottom
end

#current_paddingObject



153
154
155
# File 'lib/applitools/core/region.rb', line 153

def current_padding
  @padding
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/applitools/core/region.rb', line 37

def empty?
  @left == EMPTY.left && @top == EMPTY.top && @width == EMPTY.width && @height == EMPTY.height
end

#intersect(other) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/applitools/core/region.rb', line 67

def intersect(other)
  unless intersecting?(other)
    make_empty
    return self
  end

  i_left = left >= other.left ? left : other.left
  i_right = right <= other.right ? right : other.right
  i_top = top >= other.top ? top : other.top
  i_bottom = bottom <= other.bottom ? bottom : other.bottom

  @left = i_left
  @top = i_top
  @width = i_right - i_left
  @height = i_bottom - i_top
  self
end

#intersecting?(other) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/applitools/core/region.rb', line 62

def intersecting?(other)
  ((left <= other.left && other.left <= right) || (other.left <= left && left <= other.right)) &&
    ((top <= other.top && other.top <= bottom) || (other.top <= top && top <= other.bottom))
end

#locationObject



45
46
47
# File 'lib/applitools/core/region.rb', line 45

def location
  Applitools::Location.new left, top
end

#location=(other_location) ⇒ Object



49
50
51
52
# File 'lib/applitools/core/region.rb', line 49

def location=(other_location)
  self.left = other_location.left
  self.top = other_location.top
end

#make_emptyObject



30
31
32
33
34
35
# File 'lib/applitools/core/region.rb', line 30

def make_empty
  @left = EMPTY.left
  @top = EMPTY.top
  @width = EMPTY.width
  @height = EMPTY.height
end

#middle_offsetObject



89
90
91
92
93
# File 'lib/applitools/core/region.rb', line 89

def middle_offset
  mid_x = width / 2
  mid_y = height / 2
  Applitools::Location.for(mid_x.round, mid_y.round)
end

#rightObject



54
55
56
# File 'lib/applitools/core/region.rb', line 54

def right
  left + width
end

#scale_it!(scale_factor) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/applitools/core/region.rb', line 95

def scale_it!(scale_factor)
  @left = (@left * scale_factor).to_i
  @top = (@top * scale_factor).to_i
  @width = (@width * scale_factor).to_i
  @height = (@height * scale_factor).to_i
  self
end

#sizeObject



41
42
43
# File 'lib/applitools/core/region.rb', line 41

def size
  Applitools::RectangleSize.new width, height
end

#size_equals?(region) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/applitools/core/region.rb', line 129

def size_equals?(region)
  width == region.width && height == region.height
end

#sub_regions(subregion_size, is_fixed_size = false) ⇒ Object



103
104
105
106
# File 'lib/applitools/core/region.rb', line 103

def sub_regions(subregion_size, is_fixed_size = false)
  return self.class.sub_regions_with_fixed_size self, subregion_size if is_fixed_size
  self.class.sub_regions_with_varying_size self, subregion_size
end

#to_hashObject Also known as: json_data



112
113
114
115
116
117
118
119
120
121
# File 'lib/applitools/core/region.rb', line 112

def to_hash
  {
    x: left,
    y: top,
    left: left,
    top: top,
    height: height,
    width: width
  }
end

#to_sObject



125
126
127
# File 'lib/applitools/core/region.rb', line 125

def to_s
  "(#{left}, #{top}), #{width} x #{height}"
end

#with_paddingObject



157
158
159
160
161
162
# File 'lib/applitools/core/region.rb', line 157

def with_padding
  Applitools::Region.from_location_size(
    Applitools::Location.new(left - padding_left, top - padding_top),
    Applitools::RectangleSize.new(width + padding_left + padding_right, height + padding_top + padding_bottom)
  )
end