-
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 2 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,24 @@ | ||
// | ||
// DeviceMockable.swift | ||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 06/03/2018. | ||
// | ||
|
||
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,102 @@ | ||
// | ||
// 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 stubing layout defining properties and information about device. | ||
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.
|
||
/// 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 { | ||
var mockedTraitCollection: [UITraitCollection] = [super.traitCollection] | ||
|
||
if let mockedVerticalSizeClass = mockParameters?.verticalSizeClass { | ||
mockedTraitCollection.append(UITraitCollection(verticalSizeClass: mockedVerticalSizeClass)) | ||
} | ||
if let mockedHorizontalSizeClass = mockParameters?.horizontalSizeClass { | ||
mockedTraitCollection.append(UITraitCollection(horizontalSizeClass: mockedHorizontalSizeClass)) | ||
} | ||
if #available(iOS 10.0, *) { | ||
if let mockedPreferredContentSizeCategory = mockParameters?.preferredContentSizeCategory { | ||
mockedTraitCollection.append(UITraitCollection(preferredContentSizeCategory: mockedPreferredContentSizeCategory)) | ||
} | ||
|
||
if let mockedLayoutDirection = mockParameters?.layoutDirection { | ||
mockedTraitCollection.append(UITraitCollection(layoutDirection: mockedLayoutDirection)) | ||
} | ||
} | ||
if let userInterfaceIdiom = mockParameters?.userInterfaceIdiom { | ||
mockedTraitCollection.append(UITraitCollection(userInterfaceIdiom: userInterfaceIdiom)) | ||
} | ||
|
||
return UITraitCollection(traitsFrom: mockedTraitCollection) | ||
} | ||
|
||
// 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,95 @@ | ||
// | ||
// MockingView.swift | ||
// AGSnapshotHelper | ||
// | ||
// Created by Adam Grzegorowski on 22/02/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
/// View stubing 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 { | ||
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. When one could use 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.
Subclass is impossible because of 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.
What about making just one subclass of |
||
|
||
// 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 { | ||
var mockedTraitCollection: [UITraitCollection] = [super.traitCollection] | ||
|
||
if let mockedVerticalSizeClass = mockParameters?.verticalSizeClass { | ||
mockedTraitCollection.append(UITraitCollection(verticalSizeClass: mockedVerticalSizeClass)) | ||
} | ||
if let mockedHorizontalSizeClass = mockParameters?.horizontalSizeClass { | ||
mockedTraitCollection.append(UITraitCollection(horizontalSizeClass: mockedHorizontalSizeClass)) | ||
} | ||
if #available(iOS 10.0, *) { | ||
if let mockedPreferredContentSizeCategory = mockParameters?.preferredContentSizeCategory { | ||
mockedTraitCollection.append(UITraitCollection(preferredContentSizeCategory: mockedPreferredContentSizeCategory)) | ||
} | ||
|
||
if let mockedLayoutDirection = mockParameters?.layoutDirection { | ||
mockedTraitCollection.append(UITraitCollection(layoutDirection: mockedLayoutDirection)) | ||
} | ||
} | ||
|
||
return UITraitCollection(traitsFrom: mockedTraitCollection) | ||
} | ||
|
||
// 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 } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// AGDeviceViewMockParameters.h | ||
// AGSnapshotHelperTests | ||
// | ||
// Created by Adam Grzegorowski on 10/03/2018. | ||
// Copyright © 2018 Allegro Group. All rights reserved. | ||
// | ||
|
||
@import AGSnapshotHelper; | ||
@import Foundation; | ||
@import UIKit; | ||
|
||
// Swift does not allow to mark stored properties with @available() atribute, | ||
// so use Objective-C which works fine. | ||
NS_SWIFT_NAME(DeviceViewMockParameters) | ||
@interface AGDeviceViewMockParameters : NSObject<AGDeviceMockable, AGViewMockable> | ||
|
||
// AGDeviceMockable | ||
@property (nonatomic, copy) NSString *name; | ||
@property (nonatomic, assign) UIInterfaceOrientation orientation; | ||
@property (nonatomic, assign) UIUserInterfaceIdiom userInterfaceIdiom; | ||
|
||
// AGViewMockable | ||
@property (nonatomic, assign) CGSize size; | ||
@property (nonatomic, assign) UIEdgeInsets safeAreaInsets; | ||
@property (nonatomic, assign) UIEdgeInsets layoutMargins; | ||
@property (nonatomic, assign) UIUserInterfaceSizeClass horizontalSizeClass; | ||
@property (nonatomic, assign) UIUserInterfaceSizeClass verticalSizeClass; | ||
@property (nonatomic, strong) UIContentSizeCategory preferredContentSizeCategory NS_AVAILABLE_IOS(10.0); | ||
@property (nonatomic, assign) UITraitEnvironmentLayoutDirection layoutDirection NS_AVAILABLE_IOS(10.0); | ||
@property (nonatomic, assign) NSDirectionalEdgeInsets directionalLayoutMargins NS_AVAILABLE_IOS(11.0); | ||
|
||
@end |
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
?