locked
How to interact with camera roll in Xamarin.UITesting? RRS feed

  • Question

  • User392394 posted

    I have been trying to tap on Camera Roll in Xamarin.iOS, using Xamarin.UITesting when it opens in my application but as it is part of the OS, I am unable to use Tap function to select the camera roll and then an image. Using the app.Repl() , it just appears as this:

    [CalabashRootView]                                                              
      [UIWindow > ... > UILayoutContainerView]
        [UINavigationTransitionView > ... > _UISizeTrackingView]
          [_UIRemoteView] id: "RemoteViewBridge"
        [UINavigationBar] id: "Photos"
          [_UIBarBackground]
            [UIImageView]
            [UIVisualEffectView]
              [_UIVisualEffectBackdropView]
              [_UIVisualEffectSubview]
          [_UINavigationBarContentView]
            [_UIButtonBarStackView]
              [_UIButtonBarButton] label: "Cancel"
                [_UIModernBarButton] label: "Cancel"
                  [UIButtonLabel] label: "Cancel",  text: "Cancel"
            [UILabel] label: "Photos",  text: "Photos"
          [UILabel] label: "Photos",  text: "Photos"
          [_UIButtonBarButton] label: "Cancel"
            [_UIModernBarButton] label: "Cancel"
              [UIButtonLabel] label: "Cancel",  text: "Cancel"
      [UIWindow > UILayoutContainerView]
        [UINavigationTransitionView > ... > UITableView]
          [WelcomeScreenCustomCell]
            [UITableViewCellContentView]
              [UIView]
    

    I have tried to just push an image into the photo selection process through a backdoor but I can't seem to get it working properly (this is my AppDelegate):

        [Export("selectImageFromCameraRoll:")] // notice the colon at the end of the method name
        public NSString SelectImage(NSString value)
        {
            UIImage photo = UIImage.FromFile("testImage.jpg");
            NSData imgData = photo.AsJPEG(0.6f);
            UIImage img = new UIImage(imgData);
            var vc = window.RootViewController;
    
            //not working
            ((UIDataViewController)vc.Handle).TakenPhoto(img);
            return new NSString("true");
        }
    

    Has anyone had any experience of this and is there a way to just select an image?

    Thursday, January 23, 2020 11:32 AM

All replies

  • User392394 posted

    I have now changed it to this but the image is still not pushing in:

    public static UIViewController GetTopViewController()
            {
                var window1 = UIApplication.SharedApplication.KeyWindow;
                var vc = window1.RootViewController;
                while (vc.PresentedViewController != null)
                    vc = vc.PresentedViewController;
    
                if (vc is UINavigationController navController)
                    vc = navController.ViewControllers.LastOrDefault();
    
                return vc;
            }
    
            [Preserve, Export("selectImageFromCameraRoll:")] // notice the colon at the end of the method name
            public void SelectImage(NSString value)
            {
                UIImage photo = UIImage.FromFile("compass.png");
                NSData imgData = photo.AsJPEG(0.6f);
                UIImage img = new UIImage(imgData);
    
                var vc = GetTopViewController();
    
                var test = ((UIDataViewController)vc);
                test.addPhoto = true;
                test.TakenPhoto(img);
                test.ForceReloadData();
                test.RefreshView();
            }
    
    Thursday, January 23, 2020 3:46 PM