Skip to content

Commit 37d8dec

Browse files
committed
try to fix brand
1 parent c42ed80 commit 37d8dec

File tree

6 files changed

+87
-26
lines changed

6 files changed

+87
-26
lines changed

DeviceDetector.NET.Tests/DeviceDetectorTest.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,36 @@ public void TestParse(string fileNme)
225225

226226
var dd = DeviceDetector.GetInfoFromUserAgent(expected.user_agent, clientHints);
227227
dd.Success.Should().BeTrue();
228+
228229
dd.Match.OsFamily.Should().BeEquivalentTo(expected.os_family);
229230
dd.Match.BrowserFamily.Should().BeEquivalentTo(expected.browser_family);
231+
230232
if (expected.os != null)
231233
{
232234
switch (expected.os)
233235
{
234236
case Dictionary<object, object> { Count: > 0 } dicOs:
235237
{
236-
var osName = dicOs["name"];
237-
dd.Match.Os.Name.Should().BeEquivalentTo(osName.ToString());
238+
if (dicOs.TryGetValue("name", out var osName))
239+
{
240+
dd.Match.Os.Name.Should().BeEquivalentTo(osName.ToString());
241+
}
242+
if (dicOs.TryGetValue("version", out var osVersion))
243+
{
244+
dd.Match.Os.Version.Should().BeEquivalentTo(osVersion.ToString());
245+
}
246+
if (dicOs.TryGetValue("platform", out var osPlatform))
247+
{
248+
dd.Match.Os.Platform.Should().BeEquivalentTo(osPlatform.ToString());
249+
}
250+
if (dicOs.TryGetValue("short_name", out var osShortName))
251+
{
252+
dd.Match.Os.ShortName.Should().BeEquivalentTo(osShortName.ToString());
253+
}
254+
if (dicOs.TryGetValue("family", out var osfamily))
255+
{
256+
dd.Match.Os.Family.Should().BeEquivalentTo(osfamily.ToString());
257+
}
238258
break;
239259
}
240260
case List<object> { Count: > 0 }:
@@ -248,12 +268,15 @@ public void TestParse(string fileNme)
248268
{
249269
dd.Match.Client.Type.Should().BeEquivalentTo(expected.client.type);
250270
dd.Match.Client.Name.Should().BeEquivalentTo(expected.client.name);
271+
dd.Match.Client.Version.Should().BeEquivalentTo(expected.client.version);
272+
//dd.Match.Client.Engine.Should().BeEquivalentTo(expected.client.engine);
273+
//dd.Match.Client.EngineVersion.Should().BeEquivalentTo(expected.client.engine_version);
251274
}
252275

253276
if (expected.device == null) return;
254277

255278
dd.Match.DeviceType?.Should().BeEquivalentTo(expected.device.type);
256-
//dd.Match.DeviceBrand.Should().BeEquivalentTo((expected.device.brand ?? ""));
279+
dd.Match.DeviceBrand.Should().BeEquivalentTo((expected.device.brand ?? ""), expected.user_agent);
257280
dd.Match.DeviceModel?.Should().BeEquivalentTo((expected.device.model ?? ""));
258281
});
259282
}
@@ -296,7 +319,6 @@ public void TestInstanceReusage()
296319

297320
// quick sanity check of Reset()
298321
deviceDetector.IsParsed().Should().BeFalse();
299-
deviceDetector.GetBrand().Should().BeNullOrEmpty();
300322
deviceDetector.GetModel().Should().BeNullOrEmpty();
301323

302324
deviceDetector.Parse();
@@ -340,8 +362,8 @@ public void TestParseBots()
340362
var path = $"{Utils.CurrentDirectory()}\\{@"fixtures\bots.yml"}";
341363

342364
var parser = new YamlParser<List<BotFixture>>();
343-
var _fixtureData = parser.ParseFile(path);
344-
foreach (var fixture in _fixtureData)
365+
var fixtureData = parser.ParseFile(path);
366+
foreach (var fixture in fixtureData)
345367
{
346368
var dd = new DeviceDetector(fixture.user_agent);
347369
dd.Parse();
@@ -367,7 +389,7 @@ public void TestParseBots()
367389
}
368390

369391
[Fact]
370-
public void TestGetInfoFromUABot()
392+
public void TestGetInfoFromUaBot()
371393
{
372394
var expected = new DeviceDetectorResult {
373395
UserAgent = "Googlebot/2.1 (http://www.googlebot.com/bot.html)",
@@ -392,8 +414,8 @@ public void TestGetInfoFromUABot()
392414
[Fact]
393415
public void TestParseNoDetails()
394416
{
395-
var user_agent = "Googlebot/2.1 (http://www.googlebot.com/bot.html)";
396-
var dd = new DeviceDetector(user_agent);
417+
const string userAgent = "Googlebot/2.1 (http://www.googlebot.com/bot.html)";
418+
var dd = new DeviceDetector(userAgent);
397419
dd.DiscardBotInformation();
398420
dd.Parse();
399421
dd.IsBot().Should().BeTrue();
@@ -446,7 +468,7 @@ public void TestTypeMethods()
446468
}
447469
}
448470
[Fact]
449-
public void TestLRUCache()
471+
public void TestLruCache()
450472
{
451473
var dd = LRUCachedDeviceDetector.GetDeviceDetector("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
452474
dd.IsParsed().Should().BeTrue();

DeviceDetector.NET/DeviceDetector.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class DeviceDetector
2121
/// <summary>
2222
/// Current version number of DeviceDetector
2323
/// </summary>
24-
public const string VERSION = "6.0.2";
24+
public const string VERSION = "6.1.0";
2525

2626
/// <summary>
2727
/// Constant used as value for unknown browser / os
@@ -375,12 +375,21 @@ public string GetDeviceName()
375375
{
376376
return device.HasValue
377377
? Devices.GetDeviceName(device.Value).Key
378-
: null;
379-
}
380-
378+
: null; //todo: string.Empty?
379+
}
380+
381+
/**
382+
* Returns the device brand extracted from the parsed UA
383+
*
384+
* @see self::$deviceBrand for available device brands
385+
*
386+
* @return string
387+
*
388+
* @deprecated since 4.0 - short codes might be removed in next major release
389+
*/
381390
public string GetBrand()
382391
{
383-
return brand;
392+
return Devices.GetShortCode(brand);
384393
}
385394

386395
/// <summary>
@@ -390,7 +399,7 @@ public string GetBrand()
390399
/// <returns></returns>
391400
public string GetBrandName()
392401
{
393-
return Devices.GetFullName(brand);
402+
return brand;
394403
}
395404

396405
public string GetModel()
@@ -488,7 +497,7 @@ private bool LoadCacheData(string key)
488497

489498
private void SaveCacheData(string key)
490499
{
491-
var ent = new DeviceDetectorCachedData()
500+
var ent = new DeviceDetectorCachedData
492501
{
493502
Device = device,
494503
Brand = brand,
@@ -565,8 +574,10 @@ protected void ParseClient()
565574
var result = clientParser.Parse();
566575
if (!result.Success) continue;
567576

568-
client = new ParseResult<ClientMatchResult>();
569-
client.ParserName = clientParser.ParserName;
577+
client = new ParseResult<ClientMatchResult>
578+
{
579+
ParserName = clientParser.ParserName
580+
};
570581
client.AddRange(result.Matches);
571582
return;
572583
}
@@ -645,7 +656,8 @@ protected void ParseDevice()
645656
//See https://developer.chrome.com/multidevice/user-agent#chrome_for_android_user_agent
646657
//Note: We do not check for browser (family) here, as there might be mobile apps using Chrome, that won't have
647658
//a detected browser, but can still be detected. So we check the useragent for Chrome instead.
648-
if (!device.HasValue && osFamily == "Android" && IsMatchUserAgent(@"Chrome/[\.0-9]*"))
659+
if (!device.HasValue && osFamily == "Android"
660+
&& IsMatchUserAgent(@"Chrome/[\.0-9]*"))
649661
{
650662
if (IsMatchUserAgent(@"(?:Mobile|eliboM) Safari/"))
651663
{
@@ -657,6 +669,12 @@ protected void ParseDevice()
657669
}
658670
}
659671

672+
//Some UA contain the fragment 'Pad/APad', so we assume those devices as tablets
673+
if (DeviceType.DEVICE_TYPE_SMARTPHONE == device && IsMatchUserAgent("Pad/APad"))
674+
{
675+
device = DeviceType.DEVICE_TYPE_TABLET;
676+
}
677+
660678
//Some user agents simply contain the fragment 'Android; Tablet;' or 'Opera Tablet', so we assume those devices as tablets
661679
if (!device.HasValue && (HasAndroidTableFragment() || IsMatchUserAgent("Opera Tablet")))
662680
{
@@ -725,7 +743,7 @@ protected void ParseDevice()
725743
}
726744

727745
//All devices that contain Andr0id in string are assumed to be a tv
728-
if (IsMatchUserAgent("Andr0id|Android TV"))
746+
if (IsMatchUserAgent("Andr0id|Android TV|\\(lite\\) TV"))
729747
{
730748
device = DeviceType.DEVICE_TYPE_TV;
731749
}
@@ -742,6 +760,12 @@ protected void ParseDevice()
742760
device = DeviceType.DEVICE_TYPE_TV;
743761
}
744762

763+
//All devices containing TV fragment are assumed to be a tv
764+
if (!device.HasValue && IsMatchUserAgent("\\(TV;"))
765+
{
766+
device = DeviceType.DEVICE_TYPE_TV;
767+
}
768+
745769
// Set device type desktop if string ua contains desktop
746770
if (device != DeviceType.DEVICE_TYPE_DESKTOP && !userAgent.Contains("Desktop") && HasDesktopFragment())
747771
{
@@ -813,9 +837,10 @@ public static ParseResult<DeviceDetectorResult> GetInfoFromUserAgent(string ua,
813837

814838
match.Device = new DeviceMatchResult
815839
{
816-
Name = deviceDetector.GetDeviceName(),
817-
//Type = deviceDetector.GetDeviceName(),
840+
//Name = deviceDetector.GetDeviceName(),
841+
Type = deviceDetector.device,
818842
Brand = deviceDetector.GetBrandName(),
843+
Name = deviceDetector.GetModel(),
819844
};
820845
match.DeviceType = deviceDetector.GetDeviceName();
821846
match.DeviceBrand = deviceDetector.GetBrandName();

DeviceDetector.NET/Parser/Client/BrowserParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class BrowserParser : AbstractClientParser<List<Class.Client.Browser>>
1818
/// <summary>
1919
///
2020
/// </summary>
21-
private BrowserHints browserHints;
21+
private readonly BrowserHints browserHints;
2222

2323
/// <summary>
2424
/// Known browsers mapped to their internal short codes

DeviceDetector.NET/Parser/Device/AbstractDeviceParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static List<string> GetAvailableDeviceTypeNames()
4343
[Obsolete("Use Devices.GetDeviceName")]
4444
public static KeyValuePair<string, int> GetDeviceName(int deviceType)
4545
{
46-
return Devices.DeviceTypes.ContainsValue(deviceType) ? DeviceTypes.FirstOrDefault(t=>t.Value.Equals(deviceType)): new KeyValuePair<string, int>();
46+
return Devices.GetDeviceName(deviceType);
4747
}
4848

4949
/// <summary>

DeviceDetector.NET/Parser/Device/Devices.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,5 +1654,20 @@ public static string GetFullName(string brandId)
16541654
return string.Empty;
16551655
return DeviceBrands.ContainsKey(brandId) ? DeviceBrands[brandId] : string.Empty;
16561656
}
1657+
1658+
1659+
/**
1660+
* Returns the brand short code for the given name
1661+
*
1662+
* @param string $brand brand name
1663+
*
1664+
* @return string
1665+
*
1666+
* @deprecated since 4.0 - short codes might be removed in next major release
1667+
*/
1668+
public static string GetShortCode(string brand)
1669+
{
1670+
return DeviceBrands.ContainsValue(brand) ? DeviceBrands.FirstOrDefault(x => x.Value == brand).Key : string.Empty;
1671+
}
16571672
}
16581673
}

DeviceDetector.NET/Parser/OperatingSystemParser.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ public class OperatingSystemParser : AbstractParser<List<Os>, OsMatchResult>
159159
{"Android" , new [] {"AND", "CYN", "FIR", "REM", "RZD", "MLD", "MCD", "YNS", "GRI", "HAR",
160160
"ADR", "CLR", "BOS", "REV", "LEN", "SIR", "RRS" }},
161161
{"AmigaOS" , new [] {"AMG", "MOR"}},
162-
//{"Apple TV" , new [] {"ATV"}},
163162
{"BlackBerry" , new [] {"BLB", "QNX"}},
164163
{"Brew" , new [] {"BMP"}},
165164
{"BeOS" , new [] {"BEO", "HAI"}},

0 commit comments

Comments
 (0)