Skip to content

Commit

Permalink
Cherry pick Issue4597 fixes back to branch 4.0.0 (xamarin#6724)
Browse files Browse the repository at this point in the history
* Split up Issue4597 into separate testable chunks (xamarin#6654)

* split up tests

* fix tests for ios

* comment out tests for UWP for now

* - add delay for loading from uri
- rearrage components so switch is always visible

* add retry logic to url based tests so images can load

* fix automationid binding

* remove extra file

* fix tabs
  • Loading branch information
PureWeen authored Jul 1, 2019
1 parent e224948 commit e15ae8c
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 96 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<AndroidResource Include="Resources\drawable\newspaperflyout.png" />
<AndroidResource Include="Resources\drawable\booksflyout.png" />
<AndroidResource Include="Resources\drawable\homeflyout.png" />
<AndroidResource Include="Resources\drawable\xamarinlogo.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable\Icon.png" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="xamarinlogo.png" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;
using System.Threading;

#if UITEST
using Xamarin.UITest;
Expand All @@ -29,24 +30,36 @@ public class Issue4597 : TestContentPage
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,
Expand All @@ -55,6 +68,8 @@ protected override void Init()
BackgroundColor = Color.Purple
};

View[] imageControls = new View[] { _image, _button, _imageButton, _listView };

Button button = null;
button = new Button()
{
Expand Down Expand Up @@ -83,122 +98,215 @@ protected override void Init()

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
}
}
}
Loading

0 comments on commit e15ae8c

Please sign in to comment.