using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;
+using System.Threading;
#if UITEST
using Xamarin.UITest;
Image _image;
ListView _listView;
- string _disappearText = "You should see 4 images. Clicking this should cause the images to all disappear";
- string _appearText = "Clicking this should cause the images to all appear";
+ string _disappearText = "You should see an Image. Clicking this should cause the image to disappear";
+ string _appearText = "Clicking this should cause the image to reappear";
string _theListView = "theListViewAutomationId";
- string _fileName = "coffee.png";
- string _uriImage = "https://raw.githubusercontent.com/xamarin/Xamarin.Forms/master/Xamarin.Forms.Controls/coffee.png";
+ string _fileName = "xamarinlogo.png";
+ string _fileNameAutomationId = "CoffeeAutomationId";
+ string _uriImage = "https://github.com/xamarin/Xamarin.Forms/blob/3216ce4ccd096f8b9f909bbeea572dcf2a8c4466/Xamarin.Forms.ControlGallery.iOS/Resources/xamarinlogo.png?raw=true";
bool _isUri = false;
+ string _nextTestId = "NextTest";
+ string _activeTestId = "activeTestId";
+ string _switchUriId = "SwitchUri";
+ string _imageFromUri = "Image From Uri";
+ string _imageFromFile = "Image From File";
protected override void Init()
{
- _image = new Image() { Source = _fileName, AutomationId = _fileName, ClassId = "Something" };
- _button = new Button() { ImageSource = _fileName, AutomationId = _fileName };
- _imageButton = new ImageButton() { Source = _fileName, AutomationId = _fileName };
+ Label labelActiveTest = new Label()
+ {
+ AutomationId = _activeTestId
+ };
+
+ _image = new Image() { Source = _fileName, AutomationId = _fileNameAutomationId };
+ _button = new Button() { ImageSource = _fileName, AutomationId = _fileNameAutomationId };
+ _imageButton = new ImageButton() { Source = _fileName, AutomationId = _fileNameAutomationId };
_listView = new ListView()
{
ItemTemplate = new DataTemplate(() =>
{
var cell = new ImageCell();
cell.SetBinding(ImageCell.ImageSourceProperty, ".");
+ cell.AutomationId = _fileNameAutomationId;
return cell;
}),
AutomationId = _theListView,
BackgroundColor = Color.Purple
};
+ View[] imageControls = new View[] { _image, _button, _imageButton, _listView };
+
Button button = null;
button = new Button()
{
var switchToUri = new Switch
{
- AutomationId = "SwitchUri",
- IsToggled = false
+ AutomationId = _switchUriId,
+ IsToggled = false,
+ HeightRequest = 60
};
- switchToUri.Toggled += (_, e) => _isUri = e.Value;
- var switchWithCaption = new Grid() { HeightRequest = 60 };
- switchWithCaption.AddChild(new Label { Text = "Image From Url" }, 0, 0);
- switchWithCaption.AddChild(switchToUri, 1, 0);
+ var sourceLabel = new Label { Text = _imageFromFile };
+
+ switchToUri.Toggled += (_, e) =>
+ {
+ _isUri = e.Value;
+
+ // reset the images to visible
+ button.Text = _appearText;
+ button.SendClicked();
+
+ if (_isUri)
+ sourceLabel.Text = _imageFromUri;
+ else
+ sourceLabel.Text = _imageFromFile;
+ };
+
+
+ foreach(var view in imageControls)
+ {
+ view.BackgroundColor = Color.Red;
+ }
- var layout = new StackLayout()
+ StackLayout layout = null;
+ layout = new StackLayout()
{
+ AutomationId = "layoutContainer",
Children =
- {
+ {
+ new StackLayout()
+ {
+ Orientation = StackOrientation.Horizontal,
+ Children =
+ {
+ labelActiveTest,
+ switchToUri,
+ sourceLabel
+ }
+ },
button,
- switchWithCaption,
- _image,
- _button,
- _imageButton,
- _listView,
+ new Button()
+ {
+ Text = "Load Next Image Control to Test",
+ Command = new Command(() =>
+ {
+ var activeImage = layout.Children.Last();
+ int nextIndex = imageControls.IndexOf(activeImage) + 1;
+
+ if(nextIndex >= imageControls.Length)
+ nextIndex = 0;
+
+ layout.Children.Remove(activeImage);
+ layout.Children.Add(imageControls[nextIndex]);
+ labelActiveTest.Text = imageControls[nextIndex].GetType().Name;
+
+ // reset the images to visible
+ button.Text = _appearText;
+ button.SendClicked();
+ }),
+ AutomationId = _nextTestId
+ },
+ imageControls[0]
}
};
Content = layout;
+ labelActiveTest.Text = imageControls[0].GetType().Name;
}
#if UITEST
+
+#if !__WINDOWS__
[Test]
- public void TestImagesDisappearCorrectly()
+ public void ImageFromFileSourceAppearsAndDisappearsCorrectly()
{
- RunningApp.WaitForElement(_fileName);
- var elementsBefore = RunningApp.WaitForElement(_fileName);
-#if !__WINDOWS__
- var imageCell = RunningApp.Query(app => app.Marked(_theListView).Descendant()).Where(x => x.Class.Contains("Image")).FirstOrDefault();
-#endif
+ RunTest(nameof(Image), false);
+ }
-#if __IOS__
- Assert.AreEqual(4, elementsBefore.Where(x => x.Class.Contains("Image")).Count());
-#elif __ANDROID__
- Assert.AreEqual(3, elementsBefore.Length);
-#else
- Assert.AreEqual(4, elementsBefore.Count());
-#endif
+ [Test]
+ public void ImageFromUriSourceAppearsAndDisappearsCorrectly()
+ {
+ RunTest(nameof(Image), true);
+ }
-#if !__WINDOWS__
- Assert.IsNotNull(imageCell);
-#endif
+ [Test]
+ public void ButtonFromFileSourceAppearsAndDisappearsCorrectly()
+ {
+ RunTest(nameof(Button), false);
+ }
- RunningApp.Tap("ClickMe");
- RunningApp.WaitForElement(_appearText);
- var elementsAfter = RunningApp.WaitForElement(_fileName);
+ [Test]
+ public void ButtonFromUriSourceAppearsAndDisappearsCorrectly()
+ {
+ RunTest(nameof(Button), true);
+ }
-#if !__WINDOWS__
- var imageCellAfter = RunningApp.Query(app => app.Marked(_theListView).Descendant()).Where(x => x.Class.Contains("Image")).FirstOrDefault();
- Assert.IsNull(imageCellAfter);
-#endif
-#if __IOS__
- Assert.AreEqual(0, elementsAfter.Where(x => x.Class.Contains("Image")).Count());
-#elif __ANDROID__
- foreach (var newElement in elementsAfter)
+ [Test]
+ public void ImageButtonFromFileSourceAppearsAndDisappearsCorrectly()
+ {
+ RunTest(nameof(ImageButton), false);
+ }
+
+ [Test]
+ public void ImageButtonFromUriSourceAppearsAndDisappearsCorrectly()
+ {
+ RunTest(nameof(ImageButton), true);
+ }
+
+ [Test]
+ public void ImageCellFromFileSourceAppearsAndDisappearsCorrectly()
+ {
+ ImageCellTest(true);
+ }
+
+ [Test]
+ public void ImageCellFromUriSourceAppearsAndDisappearsCorrectly()
+ {
+ ImageCellTest(false);
+ }
+
+ void ImageCellTest(bool fileSource)
+ {
+ string className = "ImageView";
+ SetupTest(nameof(ListView), fileSource);
+
+ var imageVisible =
+ RunningApp.RetryUntilPresent(GetImage, 10, 2000);
+
+ Assert.AreEqual(1, imageVisible.Length);
+ SetImageSourceToNull();
+
+ imageVisible = GetImage();
+
+ UITest.Queries.AppResult[] GetImage()
{
- foreach(var oldElement in elementsBefore)
- {
- if(newElement.Class == oldElement.Class)
- {
- Assert.IsTrue(newElement.Rect.Height < oldElement.Rect.Height);
- continue;
- }
- }
+ return RunningApp
+ .Query(app => app.Marked(_theListView).Descendant())
+ .Where(x => x.Class != null && x.Class.Contains(className)).ToArray();
}
-#endif
- RunningApp.Tap("SwitchUri");
- RunningApp.Tap("ClickMe");
- RunningApp.WaitForElement(_disappearText);
-#if !__WINDOWS__
- imageCell = RunningApp.Query(app => app.Marked(_theListView).Descendant()).Where(x => x.Class.Contains("Image")).FirstOrDefault();
+ }
#endif
-#if __IOS__
- Assert.AreEqual(4, elementsBefore.Where(x => x.Class.Contains("Image")).Count());
-#elif __ANDROID__
- Assert.AreEqual(3, elementsBefore.Length);
-#else
- Assert.AreEqual(4, elementsBefore.Count());
-#endif
+ void RunTest(string testName, bool fileSource)
+ {
+ SetupTest(testName, fileSource);
+ var foundImage = TestForImageVisible();
+ SetImageSourceToNull();
+ TestForImageNotVisible(foundImage);
+ }
-#if !__WINDOWS__
- Assert.IsNotNull(imageCell);
-#endif
+ void SetImageSourceToNull()
+ {
RunningApp.Tap("ClickMe");
RunningApp.WaitForElement(_appearText);
- elementsAfter = RunningApp.WaitForElement(_fileName);
-#if !__WINDOWS__
- imageCellAfter = RunningApp.Query(app => app.Marked(_theListView).Descendant()).Where(x => x.Class.Contains("Image")).FirstOrDefault();
- Assert.IsNull(imageCellAfter);
-#endif
+ }
-#if __IOS__
- Assert.AreEqual(0, elementsAfter.Where(x => x.Class.Contains("Image")).Count());
-#elif __ANDROID__
- foreach (var newElement in elementsAfter)
+ UITest.Queries.AppResult TestForImageVisible()
+ {
+ var images = RunningApp.RetryUntilPresent(() =>
{
- foreach (var oldElement in elementsBefore)
- {
- if (newElement.Class == oldElement.Class)
- {
- Assert.IsTrue(newElement.Rect.Height < oldElement.Rect.Height);
- continue;
- }
- }
+ var result = RunningApp.WaitForElement(_fileNameAutomationId);
+
+ if (result[0].Rect.Height > 1)
+ return result;
+
+ return new UITest.Queries.AppResult[0];
+ }, 10, 4000);
+
+ Assert.AreEqual(1, images.Length);
+ var imageVisible = images[0];
+
+ Assert.Greater(imageVisible.Rect.Height, 1);
+ Assert.Greater(imageVisible.Rect.Width, 1);
+ return imageVisible;
+ }
+
+ void TestForImageNotVisible(UITest.Queries.AppResult previousFinding)
+ {
+ var imageVisible = RunningApp.Query(_fileNameAutomationId);
+
+ if (imageVisible.Length > 0)
+ {
+ Assert.Less(imageVisible[0].Rect.Height, previousFinding.Rect.Height);
}
-#else
- //can't validate if images have vanished until this is resolved
- Assert.Inconclusive(@"https://github.com/xamarin/Xamarin.Forms/issues/4731");
-#endif
+ }
+
+ void SetupTest(string controlType, bool fileSource)
+ {
+ RunningApp.WaitForElement(_nextTestId);
+ string activeTest = null;
+ while (RunningApp.Query(controlType).Length == 0)
+ {
+ activeTest = RunningApp.WaitForElement(_activeTestId)[0].ReadText();
+ RunningApp.Tap(_nextTestId);
+ RunningApp.WaitForNoElement(activeTest);
+ }
+
+ var currentSetting = RunningApp.WaitForElement(_switchUriId)[0].ReadText();
+
+ if (fileSource && RunningApp.Query(_imageFromUri).Length == 0)
+ RunningApp.Tap(_switchUriId);
+ else if (!fileSource && RunningApp.Query(_imageFromFile).Length == 0)
+ RunningApp.Tap(_switchUriId);
}
#endif
}
-}
+}
\ No newline at end of file