Skip to content

Commit 1febfb2

Browse files
Added the helper method
1 parent ec16fc8 commit 1febfb2

File tree

9 files changed

+19
-72
lines changed

9 files changed

+19
-72
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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,7 @@ public async Task BoxViewTranslationConsistent()
4040
var nativeView = GetNativeBoxView(handler);
4141
await InvokeOnMainThreadAsync(() =>
4242
{
43-
var translation = nativeView.TranslationX;
44-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
45-
var expectedInPixels = density * boxView.TranslationX;
46-
47-
Assert.Equal(expectedInPixels, translation, 1.0);
48-
49-
var translationY = nativeView.TranslationY;
50-
var expectedYInPixels = density * boxView.TranslationY;
51-
Assert.Equal(expectedYInPixels, translationY, 1.0);
43+
AssertTranslationMatches(nativeView, boxView.TranslationX, boxView.TranslationY);
5244
});
5345
}
5446
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,7 @@ public async Task ButtonTranslationConsistent()
133133
var nativeView = GetPlatformButton(handler);
134134
await InvokeOnMainThreadAsync(() =>
135135
{
136-
var translation = nativeView.TranslationX;
137-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
138-
var expectedInPixels = density * button.TranslationX;
139-
140-
Assert.Equal(expectedInPixels, translation, 1.0);
141-
142-
var translationY = nativeView.TranslationY;
143-
var expectedYInPixels = density * button.TranslationY;
144-
Assert.Equal(expectedYInPixels, translationY, 1.0);
136+
AssertTranslationMatches(nativeView, button.TranslationX, button.TranslationY);
145137
});
146138
}
147139
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,7 @@ public async Task CheckBoxTranslationConsistent()
3939
var nativeView = GetNativeCheckBox(handler);
4040
await InvokeOnMainThreadAsync(() =>
4141
{
42-
var translation = nativeView.TranslationX;
43-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
44-
var expectedInPixels = density * checkBox.TranslationX;
45-
46-
Assert.Equal(expectedInPixels, translation, 1.0);
47-
48-
var translationY = nativeView.TranslationY;
49-
var expectedYInPixels = density * checkBox.TranslationY;
50-
Assert.Equal(expectedYInPixels, translationY, 1.0);
42+
AssertTranslationMatches(nativeView, checkBox.TranslationX, checkBox.TranslationY);
5143
});
5244
}
5345
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,7 @@ public async Task EditorTranslationConsistent()
9393
var nativeView = GetPlatformControl(handler);
9494
await InvokeOnMainThreadAsync(() =>
9595
{
96-
var translation = nativeView.TranslationX;
97-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
98-
var expectedInPixels = density * editor.TranslationX;
99-
100-
Assert.Equal(expectedInPixels, translation, 1.0);
101-
102-
var translationY = nativeView.TranslationY;
103-
var expectedYInPixels = density * editor.TranslationY;
104-
Assert.Equal(expectedYInPixels, translationY, 1.0);
96+
AssertTranslationMatches(nativeView, editor.TranslationX, editor.TranslationY);
10597
});
10698
}
10799
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,7 @@ public async Task EntryTranslationConsistent()
107107
var nativeView = GetPlatformControl(handler);
108108
await InvokeOnMainThreadAsync(() =>
109109
{
110-
var translation = nativeView.TranslationX;
111-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
112-
var expectedInPixels = density * entry.TranslationX;
113-
114-
Assert.Equal(expectedInPixels, translation, 1.0);
115-
116-
var translationY = nativeView.TranslationY;
117-
var expectedYInPixels = density * entry.TranslationY;
118-
Assert.Equal(expectedYInPixels, translationY, 1.0);
110+
AssertTranslationMatches(nativeView, entry.TranslationX, entry.TranslationY);
119111
});
120112
}
121113
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,7 @@ public async Task LabelTranslationConsistent()
105105
var nativeView = GetPlatformLabel(handler);
106106
await InvokeOnMainThreadAsync(() =>
107107
{
108-
var translation = nativeView.TranslationX;
109-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
110-
var expectedInPixels = density * label.TranslationX;
111-
112-
Assert.Equal(expectedInPixels, translation, 1.0);
113-
114-
var translationY = nativeView.TranslationY;
115-
var expectedYInPixels = density * label.TranslationY;
116-
Assert.Equal(expectedYInPixels, translationY, 1.0);
108+
AssertTranslationMatches(nativeView, label.TranslationX, label.TranslationY);
117109
});
118110
}
119111

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ public async Task SearchBarTranslationConsistent()
5959
var nativeView = GetPlatformControl(handler);
6060
await InvokeOnMainThreadAsync(() =>
6161
{
62-
var translation = nativeView.TranslationX;
63-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
64-
var expectedInPixels = density * searchBar.TranslationX;
65-
66-
Assert.Equal(expectedInPixels, translation, 1.0);
67-
68-
var translationY = nativeView.TranslationY;
69-
var expectedYInPixels = density * searchBar.TranslationY;
70-
Assert.Equal(expectedYInPixels, translationY, 1.0);
62+
AssertTranslationMatches(nativeView, searchBar.TranslationX, searchBar.TranslationY);
7163
});
7264
}
7365
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,7 @@ public async Task SwipeViewTranslationConsistent()
114114
var nativeView = GetPlatformControl(handler);
115115
await InvokeOnMainThreadAsync(() =>
116116
{
117-
var translation = nativeView.TranslationX;
118-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
119-
var expectedInPixels = density * swipeView.TranslationX;
120-
121-
Assert.Equal(expectedInPixels, translation, 1.0);
122-
123-
var translationY = nativeView.TranslationY;
124-
var expectedYInPixels = density * swipeView.TranslationY;
125-
Assert.Equal(expectedYInPixels, translationY, 1.0);
117+
AssertTranslationMatches(nativeView, swipeView.TranslationX, swipeView.TranslationY);
126118
});
127119
}
128120

0 commit comments

Comments
 (0)