Skip to content

Commit 27810b5

Browse files
nivetha-nagalingamPureWeen
authored andcommitted
[Testing] Migration of Compatibility.Core platform-specific unit tests into device tests - 6 (#28399)
* Enabled the translation property for device tests * Added the helper method * Reverted the changes * Updated EditorTests.Android.cs * Fix formating
1 parent ca3ceec commit 27810b5

File tree

9 files changed

+170
-0
lines changed

9 files changed

+170
-0
lines changed

src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Android.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ protected bool IsBackButtonVisible(IElementHandler handler)
219219
return false;
220220
}
221221

222+
protected void AssertTranslationMatches(Android.Views.View nativeView, double expectedTranslationX, double expectedTranslationY)
223+
{
224+
var context = nativeView?.Context ?? throw new InvalidOperationException("Context cannot be null.");
225+
226+
var expectedXInPixels = context.ToPixels(expectedTranslationX);
227+
Assert.Equal(expectedXInPixels, nativeView.TranslationX, precision: 1);
228+
229+
var expectedYInPixels = context.ToPixels(expectedTranslationY);
230+
Assert.Equal(expectedYInPixels, nativeView.TranslationY, precision: 1);
231+
}
232+
222233
class WindowTestFragment : Fragment
223234
{
224235
TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();

src/Controls/tests/DeviceTests/Elements/BoxView/BoxViewTests.Android.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,26 @@ Task<bool> GetPlatformIsVisible(ShapeViewHandler boxViewViewHandler)
126126
return nativeView.Visibility == Android.Views.ViewStates.Visible;
127127
});
128128
}
129+
130+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
131+
[Fact]
132+
[Description("The Translation property of a BoxView should match with native Translation")]
133+
public async Task BoxViewTranslationConsistent()
134+
{
135+
var boxView = new BoxView()
136+
{
137+
HeightRequest = 100,
138+
WidthRequest = 200,
139+
TranslationX = 50,
140+
TranslationY = -20
141+
};
142+
143+
var handler = await CreateHandlerAsync<ShapeViewHandler>(boxView);
144+
var nativeView = GetNativeBoxView(handler);
145+
await InvokeOnMainThreadAsync(() =>
146+
{
147+
AssertTranslationMatches(nativeView, boxView.TranslationX, boxView.TranslationY);
148+
});
149+
}
129150
}
130151
}

src/Controls/tests/DeviceTests/Elements/Button/ButtonTests.Android.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,25 @@ public async Task RotationConsistent()
190190
var platformRotation = await InvokeOnMainThreadAsync(() => platformButton.Rotation);
191191
Assert.Equal(expected, platformRotation);
192192
}
193+
194+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
195+
[Fact]
196+
[Description("The Translation property of a Button should match with native Translation")]
197+
public async Task ButtonTranslationConsistent()
198+
{
199+
var button = new Button()
200+
{
201+
Text = "Button Test",
202+
TranslationX = 50,
203+
TranslationY = -20
204+
};
205+
206+
var handler = await CreateHandlerAsync<ButtonHandler>(button);
207+
var nativeView = GetPlatformButton(handler);
208+
await InvokeOnMainThreadAsync(() =>
209+
{
210+
AssertTranslationMatches(nativeView, button.TranslationX, button.TranslationY);
211+
});
212+
}
193213
}
194214
}

src/Controls/tests/DeviceTests/Elements/CheckBox/CheckBoxTests.Android.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,24 @@ Task<bool> GetPlatformIsVisible(CheckBoxHandler checkBoxHandler)
122122
return nativeView.Visibility == Android.Views.ViewStates.Visible;
123123
});
124124
}
125+
126+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
127+
[Fact]
128+
[Description("The Translation property of a CheckBox should match with native Translation")]
129+
public async Task CheckBoxTranslationConsistent()
130+
{
131+
var checkBox = new CheckBox()
132+
{
133+
TranslationX = 50,
134+
TranslationY = -20
135+
};
136+
137+
var handler = await CreateHandlerAsync<CheckBoxHandler>(checkBox);
138+
var nativeView = GetNativeCheckBox(handler);
139+
await InvokeOnMainThreadAsync(() =>
140+
{
141+
AssertTranslationMatches(nativeView, checkBox.TranslationX, checkBox.TranslationY);
142+
});
143+
}
125144
}
126145
}

src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Android.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,25 @@ await InvokeOnMainThreadAsync(() =>
170170
Assert.Equal(expectedValue, isEnabled);
171171
});
172172
}
173+
174+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
175+
[Fact]
176+
[Description("The Translation property of a Editor should match with native Translation")]
177+
public async Task EditorTranslationConsistent()
178+
{
179+
var editor = new Editor()
180+
{
181+
Text = "Editor Test",
182+
TranslationX = 50,
183+
TranslationY = -20
184+
};
185+
186+
var handler = await CreateHandlerAsync<EditorHandler>(editor);
187+
var nativeView = GetPlatformControl(handler);
188+
await InvokeOnMainThreadAsync(() =>
189+
{
190+
AssertTranslationMatches(nativeView, editor.TranslationX, editor.TranslationY);
191+
});
192+
}
173193
}
174194
}

src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Android.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,25 @@ public async Task RotationConsistent()
164164
var platformRotation = await InvokeOnMainThreadAsync(() => platformEntry.Rotation);
165165
Assert.Equal(expected, platformRotation);
166166
}
167+
168+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
169+
[Fact]
170+
[Description("The Translation property of a Entry should match with native Translation")]
171+
public async Task EntryTranslationConsistent()
172+
{
173+
var entry = new Entry()
174+
{
175+
Text = "Entry Test",
176+
TranslationX = 50,
177+
TranslationY = -20
178+
};
179+
180+
var handler = await CreateHandlerAsync<EntryHandler>(entry);
181+
var nativeView = GetPlatformControl(handler);
182+
await InvokeOnMainThreadAsync(() =>
183+
{
184+
AssertTranslationMatches(nativeView, entry.TranslationX, entry.TranslationY);
185+
});
186+
}
167187
}
168188
}

src/Controls/tests/DeviceTests/Elements/Label/LabelTests.Android.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ await InvokeOnMainThreadAsync(() =>
182182
});
183183
}
184184

185+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
186+
[Fact]
187+
[Description("The Translation property of a Label should match with native Translation")]
188+
public async Task LabelTranslationConsistent()
189+
{
190+
var label = new Label()
191+
{
192+
Text = "Label Test",
193+
TranslationX = 50,
194+
TranslationY = -20
195+
};
196+
197+
var handler = await CreateHandlerAsync<LabelHandler>(label);
198+
var nativeView = GetPlatformLabel(handler);
199+
await InvokeOnMainThreadAsync(() =>
200+
{
201+
AssertTranslationMatches(nativeView, label.TranslationX, label.TranslationY);
202+
});
203+
}
204+
185205
TextView GetPlatformLabel(LabelHandler labelHandler) =>
186206
labelHandler.PlatformView;
187207

src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Android.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,25 @@ Task<bool> GetPlatformIsVisible(SearchBarHandler searchBarHandler)
145145
return nativeView.Visibility == Android.Views.ViewStates.Visible;
146146
});
147147
}
148+
149+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
150+
[Fact]
151+
[Description("The Translation property of a SearchBar should match with native Translation")]
152+
public async Task SearchBarTranslationConsistent()
153+
{
154+
var searchBar = new SearchBar()
155+
{
156+
Text = "SearchBar Test",
157+
TranslationX = 50,
158+
TranslationY = -20
159+
};
160+
161+
var handler = await CreateHandlerAsync<SearchBarHandler>(searchBar);
162+
var nativeView = GetPlatformControl(handler);
163+
await InvokeOnMainThreadAsync(() =>
164+
{
165+
AssertTranslationMatches(nativeView, searchBar.TranslationX, searchBar.TranslationY);
166+
});
167+
}
148168
}
149169
}

src/Controls/tests/DeviceTests/Elements/SwipeView/SwipeViewTests.Android.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,25 @@ await InvokeOnMainThreadAsync(() =>
190190
});
191191
}
192192

193+
//src/Compatibility/Core/tests/Android/TranslationTests.cs
194+
[Fact]
195+
[Description("The Translation property of a SwipeView should match with native Translation")]
196+
public async Task SwipeViewTranslationConsistent()
197+
{
198+
var swipeView = new SwipeView()
199+
{
200+
TranslationX = 50,
201+
TranslationY = -20
202+
};
203+
204+
var handler = await CreateHandlerAsync<SwipeViewHandler>(swipeView);
205+
var nativeView = GetPlatformControl(handler);
206+
await InvokeOnMainThreadAsync(() =>
207+
{
208+
AssertTranslationMatches(nativeView, swipeView.TranslationX, swipeView.TranslationY);
209+
});
210+
}
211+
193212
[Fact]
194213
[Description("The IsEnabled of a SwipeView should match with native IsEnabled")]
195214
public async Task VerifySwipeViewIsEnabledProperty()

0 commit comments

Comments
 (0)