swiftairprintuiprintinteractioncntrler

How to create a UIPrintPaper to test UIPrintInteractionControllerDelegate


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...)


Solution

  • Can't control UIPrintPaper, but subclassing it to override its readonly properties is straighforward:

    class FakePrintPaper: UIPrintPaper {
    
        private let size: CGSize
        override var paperSize: CGSize { return size }
        override var printableRect: CGRect  { return CGRect(origin: CGPointZero, size: size) }
    
        init(size: CGSize) {
            self.size = size
        }
    }