class Juxtapose::MacBaconStrategy

Attributes

context[RW]

Public Class Methods

new(context) click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 4
def initialize(context)
  self.context = context
end

Public Instance Methods

current_spec_description() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 12
def current_spec_description
  "#{context.name}-#{Thread.current["CURRENT_SPEC_DESCRIPTION"]}"
end
device_name() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 16
def device_name
  name = [UIDevice.currentDevice.model.gsub(/\s+Simulator/, '').downcase]
  name << 'retina' if retina?
  name << '5' if iphone5?
  name << '6' if iphone6?
  name << '6-plus' if iphone6plus?
  name.join('-')
end
save_current(filename) click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 25
def save_current(filename)
  application = UIApplication.sharedApplication
  windows = application.windows

  currentOrientation = application.statusBarOrientation

  scale = UIScreen.mainScreen.scale
  size = UIScreen.mainScreen.bounds.size

  if [UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight].include? currentOrientation
    size = CGSizeMake(size.height, size.width);
  end

  UIGraphicsBeginImageContextWithOptions(size, false, scale)
  context = UIGraphicsGetCurrentContext()

  if currentOrientation == UIInterfaceOrientationLandscapeLeft
    CGContextTranslateCTM(context, size.width / 2.0, size.height / 2.0)
    CGContextRotateCTM(context, (Math::PI/2))
    CGContextTranslateCTM(context, - size.height / 2.0, - size.width / 2.0)
  elsif currentOrientation == UIInterfaceOrientationLandscapeRight
    CGContextTranslateCTM(context, size.width / 2.0, size.height / 2.0)
    CGContextRotateCTM(context, -(Math::PI/2))
    CGContextTranslateCTM(context, - size.height / 2.0, - size.width / 2.0)
  elsif currentOrientation == UIInterfaceOrientationPortraitUpsideDown
    CGContextTranslateCTM(context, size.width / 2.0, size.height / 2.0)
    CGContextRotateCTM(context, Math::PI)
    CGContextTranslateCTM(context, -size.width / 2.0, -size.height / 2.0)
  end

  windows.each do |window|
    next if window.layer.presentationLayer.nil?

    CGContextSaveGState(context)
    CGContextTranslateCTM(context, window.center.x, window.center.y)
    CGContextConcatCTM(context, window.transform)
    CGContextTranslateCTM(context,
                          - window.bounds.size.width * window.layer.anchorPoint.x,
                          - window.bounds.size.height * window.layer.anchorPoint.y)

    window.layer.presentationLayer.renderInContext(UIGraphicsGetCurrentContext())

    CGContextRestoreGState(context)
  end

  image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()

  UIImagePNGRepresentation(image).writeToFile(filename, atomically: true)
end
spec_dir() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 76
def spec_dir
  "spec/screens"
end
version() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 8
def version
  @version ||= "ios_#{UIDevice.currentDevice.systemVersion}"
end

Private Instance Methods

height() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 89
def height
  resolution.size.height
end
iphone5?() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 93
def iphone5?
  height == 568.0 || (height == 320.0 && width == 568.0)
end
iphone6?() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 97
def iphone6?
  height == 667.0 || height == 375.0
end
iphone6plus?() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 101
def iphone6plus?
  height == 736.0 || height == 414
end
resolution() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 81
def resolution
  @resolution ||= UIScreen.mainScreen.bounds
end
retina?() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 105
def retina?
  UIScreen.mainScreen.scale > 1
end
width() click to toggle source
# File lib/juxtapose/strategy/mac_bacon_strategy.rb, line 85
def width
  resolution.size.width
end