Skip to content

Commit 344b604

Browse files
Added the helper method
1 parent ee137cf commit 344b604

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
@@ -124,15 +124,7 @@ public async Task ButtonTranslationConsistent()
124124
var nativeView = GetPlatformButton(handler);
125125
await InvokeOnMainThreadAsync(() =>
126126
{
127-
var translation = nativeView.TranslationX;
128-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
129-
var expectedInPixels = density * button.TranslationX;
130-
131-
Assert.Equal(expectedInPixels, translation, 1.0);
132-
133-
var translationY = nativeView.TranslationY;
134-
var expectedYInPixels = density * button.TranslationY;
135-
Assert.Equal(expectedYInPixels, translationY, 1.0);
127+
AssertTranslationMatches(nativeView, button.TranslationX, button.TranslationY);
136128
});
137129
}
138130
}

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
@@ -84,15 +84,7 @@ public async Task EditorTranslationConsistent()
8484
var nativeView = GetPlatformControl(handler);
8585
await InvokeOnMainThreadAsync(() =>
8686
{
87-
var translation = nativeView.TranslationX;
88-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
89-
var expectedInPixels = density * editor.TranslationX;
90-
91-
Assert.Equal(expectedInPixels, translation, 1.0);
92-
93-
var translationY = nativeView.TranslationY;
94-
var expectedYInPixels = density * editor.TranslationY;
95-
Assert.Equal(expectedYInPixels, translationY, 1.0);
87+
AssertTranslationMatches(nativeView, editor.TranslationX, editor.TranslationY);
9688
});
9789
}
9890
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,7 @@ public async Task EntryTranslationConsistent()
9898
var nativeView = GetPlatformControl(handler);
9999
await InvokeOnMainThreadAsync(() =>
100100
{
101-
var translation = nativeView.TranslationX;
102-
var density = Microsoft.Maui.Devices.DeviceDisplay.Current.MainDisplayInfo.Density;
103-
var expectedInPixels = density * entry.TranslationX;
104-
105-
Assert.Equal(expectedInPixels, translation, 1.0);
106-
107-
var translationY = nativeView.TranslationY;
108-
var expectedYInPixels = density * entry.TranslationY;
109-
Assert.Equal(expectedYInPixels, translationY, 1.0);
101+
AssertTranslationMatches(nativeView, entry.TranslationX, entry.TranslationY);
110102
});
111103
}
112104
}

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)