-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
102 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using WeddingShare.Enums; | ||
using WeddingShare.Helpers; | ||
|
||
namespace WeddingShare.UnitTests.Tests.Helpers | ||
{ | ||
public class DeviceDetectorTests | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
} | ||
|
||
[TestCase("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", DeviceType.Desktop)] | ||
[TestCase("Mozilla/5.0 (Linux; Android 7.0; SM-T827R4 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.116 Safari/537.36", DeviceType.Tablet)] | ||
[TestCase("Mozilla/5.0 (Linux; Android 13; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36", DeviceType.Mobile)] | ||
[TestCase("Googlebot-Image/1.0", DeviceType.Desktop)] | ||
public async Task DeviceDetector_ParseDeviceType(string userAgent, DeviceType expected) | ||
{ | ||
var actual = await new DeviceDetector().ParseDeviceType(userAgent); | ||
Assert.That(actual, Is.EqualTo(expected)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace WeddingShare.Enums | ||
{ | ||
public enum DeviceType | ||
{ | ||
Unknown, | ||
Desktop, | ||
Tablet, | ||
Mobile | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Text.RegularExpressions; | ||
using WeddingShare.Enums; | ||
|
||
namespace WeddingShare.Helpers | ||
{ | ||
public interface IDeviceDetector | ||
{ | ||
Task<DeviceType> ParseDeviceType(string userAgent); | ||
} | ||
|
||
public class DeviceDetector : IDeviceDetector | ||
{ | ||
public async Task<DeviceType> ParseDeviceType(string userAgent) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
if (string.IsNullOrEmpty(userAgent)) | ||
{ | ||
return DeviceType.Unknown; | ||
} | ||
|
||
if (Regex.IsMatch(userAgent, "(tablet|ipad|playbook|silk)|(android(?!.*mobile))", RegexOptions.IgnoreCase)) | ||
{ | ||
return DeviceType.Tablet; | ||
} | ||
|
||
if (Regex.IsMatch(userAgent, "blackberry|iphone|mobile|windows ce|opera mini|htc|sony|palm|symbianos|ipad|ipod|blackberry|bada|kindle|symbian|sonyericsson|android|samsung|nokia|wap|motor", RegexOptions.IgnoreCase)) | ||
{ | ||
return DeviceType.Mobile; | ||
} | ||
|
||
return DeviceType.Desktop; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters