Skip to content

Commit

Permalink
Rewrite recent dropdown tests to read better and add failing test case
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Jun 19, 2023
1 parent 7510e3d commit 73a2f02
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneDropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,19 @@ public void TestItemSource()
AddStep("close dropdown", () => InputManager.Key(Key.Escape));
}

/// <summary>
/// Adds an item before a dropdown is loaded, and ensures item labels are assigned correctly.
/// </summary>
/// <remarks>
/// Ensures item labels are assigned after the dropdown finishes loading (reaches <see cref="LoadState.Ready"/> state),
/// so any dependency from BDL can be retrieved first before calling <see cref="Dropdown{T}.GenerateItemText"/>.
/// </remarks>
[Test]
public void TestAccessBdlInGenerateItemText()
public void TestAddItemBeforeDropdownLoad()
{
BdlDropdown dropdown = null!;

AddStep("add dropdown that uses BDL", () => Add(dropdown = new BdlDropdown
AddStep("setup dropdown", () => Add(dropdown = new BdlDropdown
{
Width = 150,
Position = new Vector2(250, 350),
Expand All @@ -385,27 +392,59 @@ public void TestAccessBdlInGenerateItemText()
}

/// <summary>
/// Checks that <see cref="Dropdown{T}.GenerateItemText"/> is not called before load when initialising with <see cref="Dropdown{T}.Current"/>.
/// Adds an item after the dropdown is in <see cref="LoadState.Ready"/> state, and ensures item labels are assigned correctly and not ignored by <see cref="Dropdown{T}"/>.
/// </summary>
[Test]
public void TestBdlWithCurrent()
public void TestAddItemWhileDropdownIsInReadyState()
{
BdlDropdown dropdown = null!;

Bindable<TestModel> bindable = null!;
AddStep("setup dropdown", () =>
{
Child = dropdown = new BdlDropdown
{
Width = 150,
Position = new Vector2(250, 350),
};
dropdown.Items = new TestModel("test").Yield();
});

AddAssert("text is expected", () => dropdown.Menu.DrawableMenuItems.First(d => d.IsSelected).ChildrenOfType<SpriteText>().First().Text.ToString(), () => Is.EqualTo("loaded: test"));
}

/// <summary>
/// Sets a non-existent item dropdown and ensures its label is assigned correctly.
/// </summary>
/// <param name="afterBdl">Whether the non-existent item should be defined after dropdown finishes from BDL.</param>
[Test]
public void TestSetNonExistentItemBeforeDropdownIsLoaded([Values] bool afterBdl)
{
BdlDropdown dropdown = null!;
Bindable<TestModel> bindable;

AddStep("add items to bindable", () => bindableList.AddRange(new[] { "one", "two", "three" }.Select(s => new TestModel(s))));
AddStep("create current", () => bindable = new Bindable<TestModel>(bindableList[1]));

AddStep("add dropdown that uses BDL", () => Add(dropdown = new BdlDropdown
AddStep("add dropdown that uses BDL", () =>
{
Width = 150,
Position = new Vector2(250, 350),
ItemSource = bindableList,
Current = bindable,
}));
bindable = new Bindable<TestModel>();
if (!afterBdl)
bindable.Value = new TestModel("non-existent item");
Add(dropdown = new BdlDropdown
{
Width = 150,
Position = new Vector2(250, 350),
ItemSource = bindableList,
Current = bindable,
});
if (afterBdl)
bindable.Value = new TestModel("non-existent item");
});

AddAssert("text is expected", () => dropdown.Menu.DrawableMenuItems.First(d => d.IsSelected).ChildrenOfType<SpriteText>().First().Text.ToString(), () => Is.EqualTo("loaded: two"));
AddAssert("text is expected", () => dropdown.SelectedItem.Text.Value.ToString(), () => Is.EqualTo("loaded: non-existent item"));
}

private void toggleDropdownViaClick(TestDropdown dropdown, string dropdownName = null) => AddStep($"click {dropdownName ?? "dropdown"}", () =>
Expand Down

0 comments on commit 73a2f02

Please sign in to comment.