Skip to content

Commit

Permalink
Add InformationLabel & InformationButton Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCodeTraveler committed Aug 5, 2020
1 parent c254053 commit 2e144c1
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 58 deletions.
2 changes: 1 addition & 1 deletion GitTrends.Android/GitTrends.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<PackageReference Include="Xamarin.Firebase.Messaging" Version="119.0.1-preview02" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="3.3.0" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.2.1-preview02" />
<PackageReference Include="Xamarin.Forms.PancakeView" Version="2.0.1.698" />
<PackageReference Include="Xamarin.Forms.PancakeView" Version="2.0.1.707" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.0.0.1" />
<PackageReference Include="Xamarin.Essentials.Interfaces" Version="1.5.3.2" />
<PackageReference Include="Xamarin.Build.Download" Version="0.10.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,21 @@ public static class RepositoryPageAutomationIds
public const string EmptyDataView = nameof(RepositoryPageAutomationIds) + nameof(EmptyDataView);
public const string LargeScreenTrendingImage = nameof(RepositoryPageAutomationIds) + nameof(LargeScreenTrendingImage);
public const string SmallScreenTrendingImage = nameof(RepositoryPageAutomationIds) + nameof(SmallScreenTrendingImage);
public const string InformationButton = nameof(RepositoryPageAutomationIds) + nameof(InformationButton);
public const string InformationLabel = nameof(RepositoryPageAutomationIds) + nameof(InformationLabel);

public static string GetFloatingActionTextButtonLabelAutomationId(in FloatingActionButtonType floatingActionButtonType)
{
const string label = "Label";

return floatingActionButtonType switch
{
FloatingActionButtonType.Information => nameof(RepositoryPageAutomationIds) + nameof(FloatingActionButtonType.Information) + label,
FloatingActionButtonType.Statistic1 => nameof(RepositoryPageAutomationIds) + nameof(FloatingActionButtonType.Statistic1) + label,
FloatingActionButtonType.Statistic2 => nameof(RepositoryPageAutomationIds) + nameof(FloatingActionButtonType.Statistic2) + label,
FloatingActionButtonType.Statistic3 => nameof(RepositoryPageAutomationIds) + nameof(FloatingActionButtonType.Statistic3) + label,
_ => throw new System.NotImplementedException(),
};
}
}
}
4 changes: 4 additions & 0 deletions GitTrends.Mobile.Common/Enums/FloatingActionButtonType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace GitTrends.Mobile.Common
{
public enum FloatingActionButtonType { Information, Statistic1, Statistic2, Statistic3 }
}
41 changes: 40 additions & 1 deletion GitTrends.Mobile.Common/Services/StatisticsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
namespace GitTrends.Mobile.Common
using System;
using System.Collections.Generic;
using System.Linq;
using GitTrends.Mobile.Common.Constants;
using GitTrends.Shared;

namespace GitTrends.Mobile.Common
{
public static class StatisticsService
{
Expand All @@ -17,5 +23,38 @@ public static string ToAbbreviatedText(this long number)

return "0";
}

public static string GetInformationLabelText<TRepository>(in IReadOnlyList<TRepository> repositories, in MobileSortingService mobileSortingService) where TRepository : IRepository =>
GetInformationLabelText(repositories, MobileSortingService.GetSortingCategory(mobileSortingService.CurrentOption));

public static string GetInformationLabelText<TRepository>(in IReadOnlyList<TRepository> repositories, in SortingCategory sortingCategory) where TRepository : IRepository => (repositories.Any(), sortingCategory) switch
{
(false, _) => string.Empty,
(true, SortingCategory.Views) => $"{SortingConstants.Views} {repositories.Sum(x => x.TotalViews).ToAbbreviatedText()}, {SortingConstants.UniqueViews} {repositories.Sum(x => x.TotalUniqueViews).ToAbbreviatedText()}, {SortingConstants.Stars} {repositories.Sum(x => x.StarCount).ToAbbreviatedText()}",
(true, SortingCategory.Clones) => $"{SortingConstants.Clones} {repositories.Sum(x => x.TotalClones).ToAbbreviatedText()}, {SortingConstants.UniqueClones} {repositories.Sum(x => x.TotalUniqueClones).ToAbbreviatedText()}, {SortingConstants.Stars} {repositories.Sum(x => x.StarCount).ToAbbreviatedText()}",
(true, SortingCategory.IssuesForks) => $"{SortingConstants.Stars} {repositories.Sum(x => x.StarCount).ToAbbreviatedText()}, {SortingConstants.Forks} {repositories.Sum(x => x.ForkCount).ToAbbreviatedText()}, {SortingConstants.Issues} {repositories.Sum(x => x.IssuesCount).ToAbbreviatedText()}",
(_, _) => throw new NotSupportedException()
};

public static string GetFloatingActionTextButtonText<TRepository>(in MobileSortingService mobileSortingService, in IReadOnlyList<TRepository> repositories, in FloatingActionButtonType floatingActionButtonType) where TRepository : IRepository =>
GetFloatingActionTextButtonText(MobileSortingService.GetSortingCategory(mobileSortingService.CurrentOption), repositories.ToList(), floatingActionButtonType);

public static string GetFloatingActionTextButtonText<TRepository>(in SortingCategory sortingCategory, in IReadOnlyList<TRepository> repositories, in FloatingActionButtonType floatingActionButtonType) where TRepository : IRepository
{
return (sortingCategory, floatingActionButtonType) switch
{
(SortingCategory.Clones, FloatingActionButtonType.Statistic1) => repositories.Sum(x => x.TotalClones).ToAbbreviatedText(),
(SortingCategory.Clones, FloatingActionButtonType.Statistic2) => repositories.Sum(x => x.TotalUniqueClones).ToAbbreviatedText(),
(SortingCategory.Clones, FloatingActionButtonType.Statistic3) => repositories.Sum(x => x.StarCount).ToAbbreviatedText(),
(SortingCategory.Views, FloatingActionButtonType.Statistic1) => repositories.Sum(x => x.TotalViews).ToAbbreviatedText(),
(SortingCategory.Views, FloatingActionButtonType.Statistic2) => repositories.Sum(x => x.TotalUniqueViews).ToAbbreviatedText(),
(SortingCategory.Views, FloatingActionButtonType.Statistic3) => repositories.Sum(x => x.StarCount).ToAbbreviatedText(),
(SortingCategory.IssuesForks, FloatingActionButtonType.Statistic1) => repositories.Sum(x => x.StarCount).ToAbbreviatedText(),
(SortingCategory.IssuesForks, FloatingActionButtonType.Statistic2) => repositories.Sum(x => x.ForkCount).ToAbbreviatedText(),
(SortingCategory.IssuesForks, FloatingActionButtonType.Statistic3) => repositories.Sum(x => x.IssuesCount).ToAbbreviatedText(),
(_, FloatingActionButtonType.Information) => throw new NotSupportedException(),
(_, _) => throw new NotImplementedException()
};
}
}
}
2 changes: 1 addition & 1 deletion GitTrends.UITests/Pages/BaseCollectionPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected BaseCollectionPage(IApp app, Func<string>? getPageTitle) : base(app, g

}

public List<TCollection> VisibleCollection => App.InvokeBackdoorMethod<List<TCollection>>(BackdoorMethodConstants.GetVisibleCollection);
public IReadOnlyList<TCollection> VisibleCollection => App.InvokeBackdoorMethod<List<TCollection>>(BackdoorMethodConstants.GetVisibleCollection);

bool IsRefreshViewRefreshIndicatorDisplayed => App switch
{
Expand Down
27 changes: 26 additions & 1 deletion GitTrends.UITests/Pages/RepositoryPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class RepositoryPage : BaseCollectionPage<Repository>
{
readonly Query _searchBar, _settingsButton, _collectionView, _refreshView,
_androidContextMenuOverflowButton, _androidSearchBarButton, _sortButton, _emptyDataView,
_smallScreenTrendingImage, _largeScreenTrendingImage;
_smallScreenTrendingImage, _largeScreenTrendingImage, _informationLabel, _informationButton,
_statistic1FloatingActionButton, _statistic2FloatingActionButton, _statistic3FloatingActionButton;

public RepositoryPage(IApp app) : base(app, () => PageTitles.RepositoryPage)
{
Expand All @@ -28,13 +29,37 @@ public RepositoryPage(IApp app) : base(app, () => PageTitles.RepositoryPage)
_emptyDataView = GenerateMarkedQuery(RepositoryPageAutomationIds.EmptyDataView);
_smallScreenTrendingImage = GenerateMarkedQuery(RepositoryPageAutomationIds.SmallScreenTrendingImage);
_largeScreenTrendingImage = GenerateMarkedQuery(RepositoryPageAutomationIds.LargeScreenTrendingImage);
_informationButton = GenerateMarkedQuery(RepositoryPageAutomationIds.InformationButton);
_informationLabel = GenerateMarkedQuery(RepositoryPageAutomationIds.InformationLabel);
_statistic1FloatingActionButton = GenerateMarkedQuery(RepositoryPageAutomationIds.GetFloatingActionTextButtonLabelAutomationId(FloatingActionButtonType.Statistic1));
_statistic2FloatingActionButton = GenerateMarkedQuery(RepositoryPageAutomationIds.GetFloatingActionTextButtonLabelAutomationId(FloatingActionButtonType.Statistic2));
_statistic3FloatingActionButton = GenerateMarkedQuery(RepositoryPageAutomationIds.GetFloatingActionTextButtonLabelAutomationId(FloatingActionButtonType.Statistic3));
}

public bool IsEmptyDataViewVisible => App.Query(_emptyDataView).Any();

public int SmallScreenTrendingImageCount => App.Query(_smallScreenTrendingImage).Count();
public int LargeScreenTrendingImageCount => App.Query(_largeScreenTrendingImage).Count();

public string InformationLabelText => App is iOSApp ? GetText(_informationLabel) : throw new NotSupportedException("Information Label Only Available on iOS");

public string InformationButtonStatistic1Text => App is AndroidApp ? GetText(_statistic1FloatingActionButton) : throw new NotSupportedException("Information Button Only Available on Android");
public string InformationButtonStatistic2Text => App is AndroidApp ? GetText(_statistic2FloatingActionButton) : throw new NotSupportedException("Information Button Only Available on Android");
public string InformationButtonStatistic3Text => App is AndroidApp ? GetText(_statistic3FloatingActionButton) : throw new NotSupportedException("Information Button Only Available on Android");

public void TapInformationButton()
{
if (App is AndroidApp)
{
App.Tap(_informationButton);
App.Screenshot("Information Button Tapped");
}
else
{
throw new NotSupportedException("Information Button Only Available on Android");
}
}

public void WaitForEmptyDataView()
{
App.WaitForElement(_emptyDataView);
Expand Down
2 changes: 1 addition & 1 deletion GitTrends.UITests/Tests/ReferringSitesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override async Task BeforeEachTest()
{
await base.BeforeEachTest().ConfigureAwait(false);

var referringSites = new List<ReferringSiteModel>();
IReadOnlyList<ReferringSiteModel> referringSites = Enumerable.Empty<ReferringSiteModel>().ToList();

var repositories = RepositoryPage.VisibleCollection;
var repositoriesEnumerator = repositories.GetEnumerator();
Expand Down
39 changes: 36 additions & 3 deletions GitTrends.UITests/Tests/RepositoriesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using GitTrends.Mobile.Common.Constants;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.iOS;

namespace GitTrends.UITests
{
Expand Down Expand Up @@ -105,16 +107,27 @@ public async Task VerifySortingOptions(SortingOption sortingOption)
Assert.AreEqual(MobileSortingService.DefaultSortingOption, SortingOption.Views);

//Arrange
Repository finalFirstRepository;
Repository finalSecondTopRepository;
Repository finalLastRepository;
Repository finalFirstRepository, finalSecondTopRepository, finalLastRepository;
Repository initialFirstRepository = RepositoryPage.VisibleCollection.First();
Repository initialSecondTopRepository = RepositoryPage.VisibleCollection.Skip(1).First();
Repository initialLastRepository = RepositoryPage.VisibleCollection.Last();

string floatingActionTextButtonStatistic1Text = string.Empty,
floatingActionTextButtonStatistic2Text = string.Empty,
floatingActionTextButtonStatistic3Text = string.Empty;

//Act
await RepositoryPage.SetSortingOption(sortingOption).ConfigureAwait(false);

if (App is AndroidApp)
{
floatingActionTextButtonStatistic1Text = RepositoryPage.InformationButtonStatistic1Text;
floatingActionTextButtonStatistic2Text = RepositoryPage.InformationButtonStatistic2Text;
floatingActionTextButtonStatistic3Text = RepositoryPage.InformationButtonStatistic3Text;

RepositoryPage.TapInformationButton();
}

//Assert
finalFirstRepository = RepositoryPage.VisibleCollection.First();
finalSecondTopRepository = RepositoryPage.VisibleCollection.Skip(1).First();
Expand All @@ -125,6 +138,26 @@ public async Task VerifySortingOptions(SortingOption sortingOption)

Assert.GreaterOrEqual(initialFirstRepository.TotalViews, initialLastRepository.TotalViews);

if (App is AndroidApp)
{
var floatingActionTextButtonStatistic1Text_Expected = StatisticsService.GetFloatingActionTextButtonText(MobileSortingService.GetSortingCategory(sortingOption), RepositoryPage.VisibleCollection, FloatingActionButtonType.Statistic1);
var floatingActionTextButtonStatistic2Text_Expected = StatisticsService.GetFloatingActionTextButtonText(MobileSortingService.GetSortingCategory(sortingOption), RepositoryPage.VisibleCollection, FloatingActionButtonType.Statistic2);
var floatingActionTextButtonStatistic3Text_Expected = StatisticsService.GetFloatingActionTextButtonText(MobileSortingService.GetSortingCategory(sortingOption), RepositoryPage.VisibleCollection, FloatingActionButtonType.Statistic3);

Assert.AreEqual(floatingActionTextButtonStatistic1Text_Expected, floatingActionTextButtonStatistic1Text);
Assert.AreEqual(floatingActionTextButtonStatistic2Text_Expected, floatingActionTextButtonStatistic2Text);
Assert.AreEqual(floatingActionTextButtonStatistic3Text_Expected, floatingActionTextButtonStatistic3Text);
}
else if (App is iOSApp)
{
var informationLabelText_Expected = StatisticsService.GetInformationLabelText(RepositoryPage.VisibleCollection, MobileSortingService.GetSortingCategory(sortingOption));
Assert.AreEqual(informationLabelText_Expected, RepositoryPage.InformationLabelText);
}
else
{
throw new NotSupportedException();
}

switch (sortingOption)
{
case SortingOption.Views when finalFirstRepository.IsTrending == finalSecondTopRepository.IsTrending:
Expand Down
2 changes: 1 addition & 1 deletion GitTrends.iOS/GitTrends.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="3.4.0" />
<PackageReference Include="Shiny.Notifications" Version="1.2.0.1755" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="3.3.0" />
<PackageReference Include="Xamarin.Forms.PancakeView" Version="2.0.1.698" />
<PackageReference Include="Xamarin.Forms.PancakeView" Version="2.0.1.707" />
<PackageReference Include="Xamarin.Essentials.Interfaces" Version="1.5.3.2" />
<PackageReference Include="Sharpnado.MaterialFrame" Version="1.1.2" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion GitTrends/GitTrends.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="3.4.0" />
<PackageReference Include="Xamarin.Forms" Version="4.7.0.1239" />
<PackageReference Include="Shiny.Notifications" Version="1.2.0.1755" />
<PackageReference Include="Xamarin.Forms.PancakeView" Version="2.0.1.698" />
<PackageReference Include="Xamarin.Forms.PancakeView" Version="2.0.1.707" />
<PackageReference Include="Xamarin.Essentials.Interfaces" Version="1.5.3.2" />
<PackageReference Include="Sharpnado.MaterialFrame" Version="1.1.2" />
</ItemGroup>
Expand Down
28 changes: 9 additions & 19 deletions GitTrends/Pages/RepositoryPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public RepositoryPage(IMainThread mainThread,

Children =
{
new TotalsLabel().Row(Row.Totals).ColumnSpan(All<Column>())
new InformationLabel().Row(Row.Totals).ColumnSpan(All<Column>())
.Bind<Label, bool, bool>(IsVisibleProperty,nameof(RepositoryViewModel.IsRefreshing), convert: isRefreshing => !isRefreshing)
.Bind<Label, IReadOnlyList<Repository>, string>(Label.TextProperty, nameof(RepositoryViewModel.VisibleRepositoryList), convert: repositories => totalsLabelConverter(repositories, mobileSortingService)),
.Bind<Label, IReadOnlyList<Repository>, string>(Label.TextProperty, nameof(RepositoryViewModel.VisibleRepositoryList), convert: repositories => StatisticsService.GetInformationLabelText(repositories, mobileSortingService)),

new RefreshView
{
Expand Down Expand Up @@ -109,21 +109,7 @@ public RepositoryPage(IMainThread mainThread,

if (Device.RuntimePlatform is Device.Android)
{
grid.Children.Add(new InformationButton(mobileSortingService, mainThread).Row(Row.Information).Column(Column.Information));
}

static string totalsLabelConverter(in IReadOnlyList<Repository> repositories, in MobileSortingService mobileSortingService)
{
if (!repositories.Any())
return string.Empty;

return MobileSortingService.GetSortingCategory(mobileSortingService.CurrentOption) switch
{
SortingCategory.Views => $"{SortingConstants.Views} {repositories.Sum(x => x.TotalViews).ToAbbreviatedText()}, {SortingConstants.UniqueViews} {repositories.Sum(x => x.TotalUniqueViews).ToAbbreviatedText()}, {SortingConstants.Stars} {repositories.Sum(x => x.StarCount).ToAbbreviatedText()}",
SortingCategory.Clones => $"{SortingConstants.Clones} {repositories.Sum(x => x.TotalClones).ToAbbreviatedText()}, {SortingConstants.UniqueClones} {repositories.Sum(x => x.TotalUniqueClones).ToAbbreviatedText()}, {SortingConstants.Stars} {repositories.Sum(x => x.StarCount).ToAbbreviatedText()}",
SortingCategory.IssuesForks => $"{SortingConstants.Stars} {repositories.Sum(x => x.StarCount).ToAbbreviatedText()}, {SortingConstants.Forks} {repositories.Sum(x => x.ForkCount).ToAbbreviatedText()}, {SortingConstants.Issues} {repositories.Sum(x => x.IssuesCount).ToAbbreviatedText()}",
_ => throw new NotSupportedException()
};
grid.Children.Add(new InformationButton(mobileSortingService, mainThread, analyticsService).Row(Row.Information).Column(Column.Information));
}
}

Expand Down Expand Up @@ -248,9 +234,13 @@ void HandlePreferredLanguageChanged(object sender, string? e)

void ISearchPage.OnSearchBarTextChanged(in string text) => _searchTextChangedEventManager.RaiseEvent(this, text, nameof(SearchBarTextChanged));

class TotalsLabel : StatisticsLabel
class InformationLabel : StatisticsLabel
{
public TotalsLabel() : base(string.Empty, true, nameof(BaseTheme.PrimaryTextColor)) => this.FillExpand().TextCenter();
public InformationLabel() : base(string.Empty, true, nameof(BaseTheme.PrimaryTextColor))
{
AutomationId = RepositoryPageAutomationIds.InformationLabel;
this.FillExpand().TextCenter();
}
}
}
}
Loading

0 comments on commit 2e144c1

Please sign in to comment.