Skip to content

Commit 7888b14

Browse files
authored
DevTools Client - Serialize lists of DevToolsDomainEntityBase (#3487)
1 parent 1723288 commit 7888b14

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

CefSharp.Test/DevTools/DevToolsClientFacts.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Threading.Tasks;
78
using CefSharp.DevTools.Browser;
9+
using CefSharp.DevTools.Emulation;
810
using CefSharp.DevTools.Network;
911
using CefSharp.Example;
1012
using CefSharp.OffScreen;
@@ -156,5 +158,68 @@ public async Task CanUseMultipleDevToolsClientInstancesPerBrowser()
156158
}
157159
}
158160
}
161+
162+
[Fact]
163+
public async Task CanSetUserAgentOverride()
164+
{
165+
using (var browser = new ChromiumWebBrowser("www.google.com"))
166+
{
167+
await browser.LoadPageAsync();
168+
169+
using (var devToolsClient = browser.GetDevToolsClient())
170+
{
171+
var brandsList = new List<UserAgentBrandVersion>();
172+
var uab = new UserAgentBrandVersion();
173+
uab.Brand = "Google Chrome";
174+
uab.Version = "89";
175+
brandsList.Add(uab);
176+
177+
var uab2 = new UserAgentBrandVersion();
178+
uab2.Brand = "Chromium";
179+
uab2.Version = "89";
180+
brandsList.Add(uab2);
181+
182+
var ua = new UserAgentMetadata();
183+
ua.Brands = brandsList;
184+
ua.Architecture = "arm";
185+
ua.Model = "Nexus 7";
186+
ua.Platform = "Android";
187+
ua.PlatformVersion = "6.0.1";
188+
ua.FullVersion = "89.0.4389.114";
189+
ua.Mobile = true;
190+
191+
await devToolsClient.Emulation.SetUserAgentOverrideAsync("Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/5(KHTML,likeGeckoChrome/89.0.4389.114Safari/537.36", null, null, ua);
192+
}
193+
194+
var userAgent = await browser.EvaluateScriptAsync("navigator.userAgent");
195+
Assert.True(userAgent.Success);
196+
Assert.Contains("Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/5(KHTML,likeGeckoChrome/89.0.4389.114Safari/537.36", Assert.IsType<string>(userAgent.Result));
197+
198+
var brands = await browser.EvaluateScriptAsync("navigator.userAgentData.brands");
199+
Assert.True(brands.Success);
200+
dynamic brandsResult = brands.Result;
201+
Assert.Collection((IEnumerable<dynamic>)brandsResult,
202+
(dynamic d) =>
203+
{
204+
Assert.Equal("Google Chrome", d.brand);
205+
Assert.Equal("89", d.version);
206+
},
207+
(dynamic d) =>
208+
{
209+
Assert.Equal("Chromium", d.brand);
210+
Assert.Equal("89", d.version);
211+
}
212+
);
213+
214+
var highEntropyValues = await browser.EvaluateScriptAsPromiseAsync("return navigator.userAgentData.getHighEntropyValues(['architecture','model','platform','platformVersion','uaFullVersion'])");
215+
Assert.True(highEntropyValues.Success);
216+
dynamic highEntropyValuesResult = highEntropyValues.Result;
217+
Assert.Equal("arm", highEntropyValuesResult.architecture);
218+
Assert.Equal("Nexus 7", highEntropyValuesResult.model);
219+
Assert.Equal("Android", highEntropyValuesResult.platform);
220+
Assert.Equal("6.0.1", highEntropyValuesResult.platformVersion);
221+
Assert.Equal("89.0.4389.114", highEntropyValuesResult.uaFullVersion);
222+
}
223+
}
159224
}
160225
}

CefSharp/DevTools/DevToolsDomainEntityBase.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections;
67
using System.Collections.Generic;
78
using System.Linq;
89
using System.Reflection;
@@ -166,19 +167,28 @@ public IDictionary<string, object> ToDictionary()
166167
{
167168
propertyValue = ((DevToolsDomainEntityBase)(propertyValue)).ToDictionary();
168169
}
170+
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(IList<>) && typeof(DevToolsDomainEntityBase).IsAssignableFrom(prop.PropertyType.GetGenericArguments()[0]))
171+
{
172+
var values = new List<IDictionary<string, object>>();
173+
foreach (var value in (IEnumerable)propertyValue)
174+
{
175+
values.Add(((DevToolsDomainEntityBase)value).ToDictionary());
176+
}
177+
propertyValue = values;
178+
}
169179
else if (propertyValueType.IsEnum)
170180
{
171181
propertyValue = EnumToString((Enum)propertyValue);
172182
}
173-
else if(propertyValueType.IsGenericType)
183+
else if (propertyValueType.IsGenericType)
174184
{
175185
var nullableType = Nullable.GetUnderlyingType(propertyValueType);
176186
if(nullableType != null && nullableType.IsEnum)
177187
{
178188
propertyValue = EnumToString((Enum)propertyValue);
179189
}
180190
}
181-
else if(propertyValueType.IsArray && propertyValueType.GetElementType().IsEnum)
191+
else if (propertyValueType.IsArray && propertyValueType.GetElementType().IsEnum)
182192
{
183193
propertyValue = EnumToString((Array)propertyValue);
184194
}
@@ -226,6 +236,15 @@ public IDictionary<string, object> ToDictionary()
226236
{
227237
propertyValue = ((DevToolsDomainEntityBase)(propertyValue)).ToDictionary();
228238
}
239+
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(IList<>) && typeof(DevToolsDomainEntityBase).IsAssignableFrom(prop.PropertyType.GetGenericArguments()[0]))
240+
{
241+
var values = new List<IDictionary<string, object>>();
242+
foreach (var value in (IEnumerable)propertyValue)
243+
{
244+
values.Add(((DevToolsDomainEntityBase)value).ToDictionary());
245+
}
246+
propertyValue = values;
247+
}
229248

230249
dict.Add(propertyName, propertyValue);
231250
}

0 commit comments

Comments
 (0)