-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mock protocols and view mocks #19
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
language: objective-c | ||
|
||
osx_image: xcode8.3 | ||
osx_image: xcode9.2 | ||
|
||
env: | ||
global: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// UITraitCollection+ViewMockable.swift | ||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 05/05/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
extension UITraitCollection { | ||
|
||
convenience init(mockParameters: ViewMockable) { | ||
var newTraitCollection: [UITraitCollection] = [ | ||
UITraitCollection(verticalSizeClass: mockParameters.verticalSizeClass), | ||
UITraitCollection(horizontalSizeClass: mockParameters.horizontalSizeClass) | ||
] | ||
|
||
if #available(iOS 10.0, *) { | ||
newTraitCollection.append(UITraitCollection(layoutDirection: mockParameters.layoutDirection)) | ||
|
||
if let mockedPreferredContentSizeCategory = mockParameters.preferredContentSizeCategory { | ||
newTraitCollection.append(UITraitCollection(preferredContentSizeCategory: mockedPreferredContentSizeCategory)) | ||
} | ||
} | ||
|
||
self.init(traitsFrom: newTraitCollection) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// DeviceMockable.swift | ||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 06/03/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
/// A set of properties that provide information about device. | ||
@objc(AGDeviceMockable) | ||
public protocol DeviceMockable { | ||
|
||
/// Device name. | ||
var name: String { get set } | ||
|
||
/// Interface orientation to stub. | ||
var orientation: UIInterfaceOrientation { get set } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why If we want to support only iOS (iPhone and iPad) we should be more specific than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
UIUserInterfaceSizeClasses are defined in
I have never checked interface orientation on CarPlay, but
I don't think we have to limit only to iOS. Car play and tvOS probably can be supported out of box, watchKit requires some work. |
||
|
||
/// User interface idiom to stub. Part of trait collection. | ||
var userInterfaceIdiom: UIUserInterfaceIdiom { get set } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// MockingDeviceView.swift | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably you are right. Please check also remark: #19 (comment) |
||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 10/03/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
public typealias DeviceViewMockable = ViewMockable & DeviceMockable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/// View stubbing layout defining properties and information about device. | ||
/// Some of layout properties (e.g. `safeAreaInsets`, or `traitCollection`) are read-only, | ||
/// so this view can be used as container which helps to force custom layout. | ||
/// Additionally `mockParameters` contains information about mocked device, | ||
/// which can be use to distinguish interface orientation or device model. | ||
@objc(AGMockingDeviceView) | ||
public class MockingDeviceView: UIView { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if
And this is a |
||
|
||
// MARK: - Public methods | ||
|
||
/// Object storing properties to stub. | ||
public var mockParameters: DeviceViewMockable? { | ||
didSet { | ||
guard let mockParameters = mockParameters else { | ||
return | ||
} | ||
|
||
frame.size = mockParameters.size | ||
setUpLayoutMargins() | ||
} | ||
} | ||
|
||
/// Initializes and returns a newly allocated view object with rectangle frame equal to 0,0 origin and `mockParameters.size` size. | ||
/// | ||
/// - Parameter mockParameters: Object with properties to stub. | ||
public init(mockParameters: DeviceViewMockable) { | ||
let newFrame = CGRect(origin: .zero, size: mockParameters.size) | ||
super.init(frame: newFrame) | ||
} | ||
|
||
// MARK: - Overriden methods | ||
|
||
override public init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
setUpLayoutMargins() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
|
||
setUpLayoutMargins() | ||
} | ||
|
||
@available(iOS 11.0, *) | ||
override public var safeAreaInsets: UIEdgeInsets { | ||
return mockParameters?.safeAreaInsets ?? super.safeAreaInsets | ||
} | ||
|
||
override public var traitCollection: UITraitCollection { | ||
guard let mockParameters = mockParameters else { | ||
return super.traitCollection | ||
} | ||
|
||
return UITraitCollection(traitsFrom: [ | ||
super.traitCollection, | ||
.init(mockParameters: mockParameters), | ||
.init(userInterfaceIdiom: mockParameters.userInterfaceIdiom) | ||
]) | ||
} | ||
|
||
// MARK: - Private methods | ||
|
||
private func setUpLayoutMargins() { | ||
|
||
if #available(iOS 11.0, *) { | ||
if let mockedDirectionalLayoutMargins = mockParameters?.directionalLayoutMargins { | ||
directionalLayoutMargins = mockedDirectionalLayoutMargins | ||
} | ||
} | ||
|
||
if let mockedLayoutMargins = mockParameters?.layoutMargins { | ||
layoutMargins = mockedLayoutMargins | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// MockingView.swift | ||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 22/02/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
/// View stubbing layout defining properties. | ||
/// Some of layout properties (e.g. `safeAreaInsets`, or `traitCollection`) are read-only, | ||
/// so this view can be used as container which helps to force custom layout. | ||
@objc(AGMockingView) | ||
public class MockingView: UIView { | ||
|
||
// MARK: - Public methods | ||
|
||
/// Object storing properties to stub. | ||
public var mockParameters: ViewMockable? { | ||
didSet { | ||
guard let mockParameters = mockParameters else { | ||
return | ||
} | ||
|
||
frame.size = mockParameters.size | ||
setUpLayoutMargins() | ||
} | ||
} | ||
|
||
/// Initializes and returns a newly allocated view object with rectangle frame equal to 0,0 origin and `mockParameters.size` size. | ||
/// | ||
/// - Parameter mockParameters: Object with properties to stub. | ||
public init(mockParameters: ViewMockable) { | ||
let newFrame = CGRect(origin: .zero, size: mockParameters.size) | ||
super.init(frame: newFrame) | ||
} | ||
|
||
// MARK: - Overriden methods | ||
|
||
override public init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
setUpLayoutMargins() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
|
||
setUpLayoutMargins() | ||
} | ||
|
||
@available(iOS 11.0, *) | ||
override public var safeAreaInsets: UIEdgeInsets { | ||
return mockParameters?.safeAreaInsets ?? super.safeAreaInsets | ||
} | ||
|
||
override public var traitCollection: UITraitCollection { | ||
guard let mockParameters = mockParameters else { | ||
return super.traitCollection | ||
} | ||
|
||
return UITraitCollection(traitsFrom: [ | ||
super.traitCollection, | ||
.init(mockParameters: mockParameters), | ||
]) | ||
|
||
} | ||
|
||
// MARK: - Private methods | ||
|
||
private func setUpLayoutMargins() { | ||
|
||
if let mockedLayoutMargins = mockParameters?.layoutMargins { | ||
layoutMargins = mockedLayoutMargins | ||
} | ||
|
||
if #available(iOS 11.0, *) { | ||
if let mockedDirectionalLayoutMargins = mockParameters?.directionalLayoutMargins { | ||
directionalLayoutMargins = mockedDirectionalLayoutMargins | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// ViewMockable.swift | ||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 22/02/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
/// A set of properties that provide information about layout of view. | ||
@objc(AGViewMockable) | ||
public protocol ViewMockable { | ||
|
||
/// Size to stub | ||
var size: CGSize { get set } | ||
|
||
/// Safe area insets to stub. | ||
var safeAreaInsets: UIEdgeInsets { get set } | ||
|
||
/// Layout margins insets to stub. | ||
/// In iOS 11 and later use the `directionalLayoutMargins`. | ||
var layoutMargins: UIEdgeInsets { get set } | ||
|
||
/// Directional layout margins insets to stub. | ||
@available(iOS 11.0, *) | ||
var directionalLayoutMargins: NSDirectionalEdgeInsets { get set } | ||
|
||
/// Horizontal size class to stub. Part of trait collection. | ||
var horizontalSizeClass: UIUserInterfaceSizeClass { get set } | ||
|
||
/// Vertical size class to stub. Part of trait collection. | ||
var verticalSizeClass: UIUserInterfaceSizeClass { get set } | ||
|
||
/// Preferred content size category to stub. Part of trait collection. | ||
@available(iOS 10.0, *) | ||
var preferredContentSizeCategory: UIContentSizeCategory? { get set } | ||
|
||
/// Layout direction to stub. Part of trait collection. | ||
@available(iOS 10.0, *) | ||
var layoutDirection: UITraitEnvironmentLayoutDirection { get set } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DeviceMockable
>MockableDevice
?