Having written a UIPrintInteractionControllerDelegate
, I wish to unit test its paper selection functionality in printInteractionController:choosePaper:
Its declaration is:
optional func printInteractionController(_ printInteractionController: UIPrintInteractionController, choosePaper paperList: [UIPrintPaper]) -> UIPrintPaper
It is a simple matter of calling it with predefined UIPrintPaper values and checking the output. However I am unable to create UIPrintPaper instances. Here is how UIPrintPaper is declared:
NS_CLASS_AVAILABLE_IOS(4_2)__TVOS_PROHIBITED @interface UIPrintPaper : NSObject
+ (UIPrintPaper *)bestPaperForPageSize:(CGSize)contentSize withPapersFromArray:(NSArray<UIPrintPaper *> *)paperList; // for use by delegate. pass in list
@property(readonly) CGSize paperSize;
@property(readonly) CGRect printableRect;
@end
The paperSize and printableRect properties are readonly and there is no initializer to define them. How can I create UIPrintPaper to represent different paper sizes for my tests? (A4, US Letter, 4x6...)
I don't think you are supposed to create UIPrintPaper. The Apple API calls:
... on your UIPrintInteractionControllerDelegate with an array of all UIPaper supported by your printer. If you don't get the one you want, then the printer doesn't support it.
So instead of creating one, implement this delegate call, and return the correct UIPrintPaper from the params that the printer supports.
Use the
UIPrintPaper
class methodbestPaperForPageSize
:I imagine you would want to use it like this:
Where CGSize is your paper size.
Can't control UIPrintPaper, but subclassing it to override its readonly properties is straighforward: