2016-05-26 16:06:52 +03:00
// Copyright 2011 Xamarin Inc. All rights reserved
2017-10-31 19:09:21 +03:00
#if ! __WATCHOS__ & & ! MONOMAC
2016-05-26 16:06:52 +03:00
using System ;
using System.Drawing ;
using Foundation ;
using UIKit ;
using ObjCRuntime ;
using NUnit.Framework ;
using RectangleF = CoreGraphics . CGRect ;
using SizeF = CoreGraphics . CGSize ;
using PointF = CoreGraphics . CGPoint ;
namespace MonoTouchFixtures.UIKit {
[TestFixture]
[Preserve (AllMembers = true)]
public class PopoverControllerTest {
[Test]
public void Defaults ( )
{
if ( UIDevice . CurrentDevice . UserInterfaceIdiom ! = UIUserInterfaceIdiom . Pad )
return ;
2018-07-04 12:44:14 +03:00
bool ios8 = TestRuntime . CheckSystemVersion ( PlatformName . iOS , 8 , 0 , throwIfOtherPlatform : false ) ;
2016-05-26 16:06:52 +03:00
using ( var vc = new UIViewController ( ) )
using ( var pc = new UIPopoverController ( vc ) ) {
Assert . That ( pc . ContentViewController , Is . SameAs ( vc ) , "ContentViewController" ) ;
Assert . Null ( pc . PassthroughViews , "PassthroughViews" ) ;
Assert . That ( pc . PopoverArrowDirection , Is . EqualTo ( UIPopoverArrowDirection . Unknown ) , "PopoverArrowDirection" ) ;
Assert . That ( pc . PopoverContentSize . IsEmpty , Is . EqualTo ( ios8 ) , "PopoverContentSize" ) ;
Assert . That ( pc . PopoverLayoutMargins . ToString ( ) , Is . EqualTo ( ios8 ? "{0, 0, 0, 0}" : "{30, 10, 10, 10}" ) , "PopoverLayoutMargins" ) ;
Assert . False ( pc . PopoverVisible , "PopoverVisible" ) ;
Assert . Null ( pc . ShouldDismiss , "ShouldDismiss" ) ;
}
}
[Test]
[ExpectedException (typeof (MonoTouchException))]
public void PresentFromBarButtonItem_BadButton ( )
{
if ( UIDevice . CurrentDevice . UserInterfaceIdiom ! = UIUserInterfaceIdiom . Pad )
Assert . Inconclusive ( "Requires iPad" ) ;
if ( Runtime . Arch = = Arch . DEVICE )
Assert . Ignore ( "ObjectiveC exception crash on devices - bug #3980" ) ;
using ( var vc = new UIViewController ( ) )
using ( var bbi = new UIBarButtonItem ( UIBarButtonSystemItem . Action ) )
using ( var pc = new UIPopoverController ( vc ) ) {
// UIBarButtonItem is itself 'ok' but it's not assigned to a view
pc . PresentFromBarButtonItem ( bbi , UIPopoverArrowDirection . Down , true ) ;
// fails with:
// Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[UIPopoverController presentPopoverFromBarButtonItem:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.
}
}
[Test]
public void PresentFromRect ( )
{
if ( UIDevice . CurrentDevice . UserInterfaceIdiom ! = UIUserInterfaceIdiom . Pad )
return ;
using ( var vc = new UIViewController ( ) )
using ( var bbi = new UIBarButtonItem ( UIBarButtonSystemItem . Action ) )
using ( var pc = new UIPopoverController ( vc ) ) {
var view = UIApplication . SharedApplication . KeyWindow ;
pc . PresentFromRect ( new RectangleF ( 10 , 10 , 100 , 100 ) , view , UIPopoverArrowDirection . Down , true ) ;
pc . Dismiss ( true ) ;
// works (as long as we dismiss the popover before disposing)
}
}
[Test]
[ExpectedException (typeof (MonoTouchException))]
public void PresentFromRect_BadView ( )
{
if ( UIDevice . CurrentDevice . UserInterfaceIdiom ! = UIUserInterfaceIdiom . Pad )
Assert . Inconclusive ( "Requires iPad" ) ;
if ( Runtime . Arch = = Arch . DEVICE )
Assert . Ignore ( "ObjectiveC exception crash on devices - bug #3980" ) ;
using ( var vc = new UIViewController ( ) )
using ( var bbi = new UIBarButtonItem ( UIBarButtonSystemItem . Action ) )
using ( var pc = new UIPopoverController ( vc ) ) {
// 'vc' has never been shown
pc . PresentFromRect ( new RectangleF ( 10 , 10 , 100 , 100 ) , vc . View , UIPopoverArrowDirection . Down , true ) ;
// fails with:
// Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.
}
}
// note: not complete (won't work) - but enough for testing the property
// full sample at http://stackoverflow.com/a/9312939/220643
class MyPopoverBackgroundView : UIPopoverBackgroundView {
}
[Test]
public void PopoverBackgroundViewType ( )
{
if ( UIDevice . CurrentDevice . UserInterfaceIdiom ! = UIUserInterfaceIdiom . Pad )
return ;
using ( var vc = new UIViewController ( ) )
using ( var pc = new UIPopoverController ( vc ) ) {
Assert . Null ( pc . PopoverBackgroundViewType , "PopoverBackgroundViewType" ) ;
Type my = typeof ( MyPopoverBackgroundView ) ;
pc . PopoverBackgroundViewType = my ;
Assert . That ( pc . PopoverBackgroundViewType , Is . SameAs ( my ) , "MyPopoverBackgroundView" ) ;
}
}
}
}
#endif // !__WATCHOS__