diff --git a/source/ProtocolGenerator.Tests/CommandTests.cs b/source/ProtocolGenerator.Tests/CommandTests.cs index c3c2fd66917cd199e8c502a21f35d3f17f66fe65..e4835cf75db2f9c041a724f7cb26822ee5c8a95f 100644 --- a/source/ProtocolGenerator.Tests/CommandTests.cs +++ b/source/ProtocolGenerator.Tests/CommandTests.cs @@ -18,8 +18,8 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator.Tests var inspector11 = ProtocolProcessor.LoadProtocol(DeploymentItems.Inspector11, "inspector-1.1"); var protocol = ProtocolProcessor.LoadProtocol(DeploymentItems.Protocol, "protocol"); - ProtocolProcessor.ResolveTypeReferences(inspector11); - ProtocolProcessor.ResolveTypeReferences(protocol); + ProtocolProcessor.ResolveTypeReferences(inspector11, new Dictionary<string, string>()); + ProtocolProcessor.ResolveTypeReferences(protocol, new Dictionary<string, string>()); var stopScreencast10 = inspector11.GetDomain("Page").GetCommand("stopScreencast"); var stopScreencastTip = protocol.GetDomain("Page").GetCommand("stopScreencast"); diff --git a/source/ProtocolGenerator.Tests/DeploymentItems.cs b/source/ProtocolGenerator.Tests/DeploymentItems.cs index 7b97f4ca5465539bf4c75158a9b097fbe0b355c9..dec0e235aab89c6757e46a6f6e525dfaf32766c2 100644 --- a/source/ProtocolGenerator.Tests/DeploymentItems.cs +++ b/source/ProtocolGenerator.Tests/DeploymentItems.cs @@ -11,5 +11,6 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator.Tests public const string Inspector10 = "Inspector-1.0.json"; public const string Inspector11 = "Inspector-1.1.json"; public const string Protocol = "protocol.json"; + public const string InspectoriOS8 = "Inspector-ios-8.0.json"; } } diff --git a/source/ProtocolGenerator.Tests/MasterDevs.ChromeDevTools.ProtocolGenerator.Tests.csproj b/source/ProtocolGenerator.Tests/MasterDevs.ChromeDevTools.ProtocolGenerator.Tests.csproj index 338b3d797ca5a1b5a3a2d4db2f71f1ef573daf32..8e37f6572a6f57b277fbd82e2e651b5ae863513c 100644 --- a/source/ProtocolGenerator.Tests/MasterDevs.ChromeDevTools.ProtocolGenerator.Tests.csproj +++ b/source/ProtocolGenerator.Tests/MasterDevs.ChromeDevTools.ProtocolGenerator.Tests.csproj @@ -67,6 +67,9 @@ <Link>Inspector-1.0.json</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> + <None Include="..\ProtocolGenerator\Inspector-iOS-8.0.json"> + <Link>Inspector-iOS-8.0.json</Link> + </None> <None Include="..\ProtocolGenerator\protocol.json"> <Link>protocol.json</Link> </None> diff --git a/source/ProtocolGenerator.Tests/ProtocolProcessorTests.cs b/source/ProtocolGenerator.Tests/ProtocolProcessorTests.cs index 5745864a443623256e28ec2e90718c9137ee1ee7..371d1adbd30d98380de0a837e17c090d22a54c72 100644 --- a/source/ProtocolGenerator.Tests/ProtocolProcessorTests.cs +++ b/source/ProtocolGenerator.Tests/ProtocolProcessorTests.cs @@ -1,5 +1,7 @@ using System; +using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; namespace MasterDevs.ChromeDevTools.ProtocolGenerator.Tests { @@ -12,15 +14,60 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator.Tests /// </summary> [TestMethod] [DeploymentItem(DeploymentItems.Inspector10)] - public void ResolveTypeReferencesTest() + public void ResolveTypeReferencesCommandParameterTest() { Protocol p = ProtocolProcessor.LoadProtocol(DeploymentItems.Inspector10, "Chrome-1.0"); - ProtocolProcessor.ResolveTypeReferences(p); + ProtocolProcessor.ResolveTypeReferences(p, new Dictionary<string, string>()); var evaluateCommand = p.GetDomain("Runtime").GetCommand("evaluate"); var frameIdParameter = evaluateCommand.GetParameter("frameId"); Assert.AreEqual("string", frameIdParameter.TypeName); } + + [TestMethod] + [DeploymentItem(DeploymentItems.Inspector10)] + public void ResolveTypeReferencesCommandParameterTest2() + { + Protocol p = ProtocolProcessor.LoadProtocol(DeploymentItems.Inspector10, "Chrome-1.0"); + ProtocolProcessor.ResolveTypeReferences(p, new Dictionary<string, string>()); + + var addInspectedNodeCommand = p.GetDomain("Console").GetCommand("addInspectedNode"); + var nodeId = addInspectedNodeCommand.GetParameter("nodeId"); + + Assert.AreEqual("integer", nodeId.TypeName); + } + + [TestMethod] + [DeploymentItem(DeploymentItems.InspectoriOS8)] + public void ResolveTypeReferencesCommandReturnValueTest() + { + Dictionary<string, string> explicitMappings = new Dictionary<string, string>(); + explicitMappings.Add("Page.Cookie", "Network.Cookie"); + + Protocol p = ProtocolProcessor.LoadProtocol(DeploymentItems.InspectoriOS8, "iOS-8.0"); + ProtocolProcessor.ResolveTypeReferences(p, explicitMappings); + + var getCookiesCommand = p.GetDomain("Page").GetCommand("getCookies"); + var cookieArray = getCookiesCommand.Returns.Single(); + + Assert.AreEqual("Network.Cookie[]", cookieArray.TypeName); + } + + [TestMethod] + [DeploymentItem(DeploymentItems.InspectoriOS8)] + public void ResolveTypeReferencesCommandReturnValueTest2() + { + Dictionary<string, string> explicitMappings = new Dictionary<string, string>(); + explicitMappings.Add("GenericTypes.SearchMatch", "Debugger.SearchMatch"); + + Protocol p = ProtocolProcessor.LoadProtocol(DeploymentItems.InspectoriOS8, "iOS-8.0"); + ProtocolProcessor.ResolveTypeReferences(p, explicitMappings); + + var searchInResourceCommand = p.GetDomain("Page").GetCommand("searchInResource"); + var searchMatchArray = searchInResourceCommand.Returns.Single(); + + Assert.AreEqual("Debugger.SearchMatch[]", searchMatchArray.TypeName); + } } } diff --git a/source/ProtocolGenerator/Program.cs b/source/ProtocolGenerator/Program.cs index 043194bcc360b45b0d19c9530b4b05a3c51691ce..a375f348ed37b2d110695d0c72129ea79f820b1b 100644 --- a/source/ProtocolGenerator/Program.cs +++ b/source/ProtocolGenerator/Program.cs @@ -27,22 +27,33 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator private static void Main(string[] args) { + // At this point in time, we only process the most recent Chrome + // and iOS (Safari) protocols. Dictionary<string, string> protocolFiles = new Dictionary<string, string>(); - protocolFiles.Add("Chrome-0.1", "Inspector-0.1.json"); - protocolFiles.Add("Chrome-1.0", "Inspector-1.0.json"); - protocolFiles.Add("Chrome-1.1", "Inspector-1.1.json"); + //protocolFiles.Add("Chrome-0.1", "Inspector-0.1.json"); + //protocolFiles.Add("Chrome-1.0", "Inspector-1.0.json"); + //protocolFiles.Add("Chrome-1.1", "Inspector-1.1.json"); protocolFiles.Add("Chrome-Tip", "protocol.json"); - protocolFiles.Add("iOS-7.0", "Inspector-iOS-7.0.json"); - protocolFiles.Add("iOS-8.0", "Inspector-iOS-8.0.json"); - protocolFiles.Add("iOS-9.0", "Inspector-iOS-9.0.json"); + //protocolFiles.Add("iOS-7.0", "Inspector-iOS-7.0.json"); + //protocolFiles.Add("iOS-8.0", "Inspector-iOS-8.0.json"); + //protocolFiles.Add("iOS-9.0", "Inspector-iOS-9.0.json"); protocolFiles.Add("iOS-9.3", "Inspector-iOS-9.3.json"); Collection<Protocol> protocols = new Collection<Protocol>(); - - foreach(var protocolFile in protocolFiles) + + // "Explicit mappings" allow us to map one type reference to another. This is a + // rather hard-coded way of doing things, and is only used when the same type + // has different names accross different versions of the dev tools - e.g. the RGBA + // type which is named RGBAColor for Safari. + Dictionary<string, string> explicitMappings = new Dictionary<string, string>(); + explicitMappings.Add("DOM.RGBAColor", "RGBA"); + explicitMappings.Add("Page.Cookie", "Network.Cookie"); + explicitMappings.Add("GenericTypes.SearchMatch", "Debugger.SearchMatch"); + + foreach (var protocolFile in protocolFiles) { Protocol p = ProtocolProcessor.LoadProtocol(protocolFile.Value, protocolFile.Key); - ProtocolProcessor.ResolveTypeReferences(p); + ProtocolProcessor.ResolveTypeReferences(p, explicitMappings); protocols.Add(p); } diff --git a/source/ProtocolGenerator/ProtocolMerger.cs b/source/ProtocolGenerator/ProtocolMerger.cs index e823b6b552ef2ac1f2c11e654d99dc2e6c06d8d2..cbdd168240d1c77187ff915a637d9ad0665c0391 100644 --- a/source/ProtocolGenerator/ProtocolMerger.cs +++ b/source/ProtocolGenerator/ProtocolMerger.cs @@ -18,12 +18,12 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator } else { - Merge(domain, target.Domains.Single(t => NameEqualityComparer.Instance.Equals(domain, t))); + Merge(source, domain, target.Domains.Single(t => NameEqualityComparer.Instance.Equals(domain, t))); } } } - static void Merge(Domain source, Domain target) + static void Merge(Protocol protocol, Domain source, Domain target) { foreach (var command in source.Commands) { @@ -37,7 +37,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator if(!targetCommand.Equals(command)) { - Console.WriteLine($"{source.Name}:{command},{targetCommand}"); + Console.WriteLine($"{protocol.Alias};{source.Name};{command.Name};{command};{targetCommand}"); } else { diff --git a/source/ProtocolGenerator/ProtocolProcessor.cs b/source/ProtocolGenerator/ProtocolProcessor.cs index fa448dd64882cc9fb52926a630047d0572d332e4..2286424ad44295645d84ee2932fa53e860bae2e2 100644 --- a/source/ProtocolGenerator/ProtocolProcessor.cs +++ b/source/ProtocolGenerator/ProtocolProcessor.cs @@ -10,36 +10,36 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator { public class ProtocolProcessor { - public static void ResolveTypeReferences(Protocol protocol) + public static void ResolveTypeReferences(Protocol protocol, Dictionary<string, string> explicitMappings) { foreach (var domain in protocol.Domains) { - ResolveTypeReferences(protocol, domain); + ResolveTypeReferences(protocol, domain, explicitMappings); } } - public static void ResolveTypeReferences(Protocol protocol, Domain domain) + public static void ResolveTypeReferences(Protocol protocol, Domain domain, Dictionary<string, string> explicitMappings) { foreach (var command in domain.Commands) { - ResolveTypeReferences(protocol, domain, command); + ResolveTypeReferences(protocol, domain, command, explicitMappings); } } - public static void ResolveTypeReferences(Protocol protocol, Domain domain, Command command) + public static void ResolveTypeReferences(Protocol protocol, Domain domain, Command command, Dictionary<string, string> explicitMappings) { foreach (var parameter in command.Parameters) { - ResolveTypeReferences(protocol, domain, parameter); + ResolveTypeReferences(protocol, domain, parameter, explicitMappings); } foreach (var returnValue in command.Returns) { - ResolveTypeReferences(protocol, domain, returnValue); + ResolveTypeReferences(protocol, domain, returnValue, explicitMappings); } } - public static void ResolveTypeReferences(Protocol protocol, Domain domain, Property property) + public static void ResolveTypeReferences(Protocol protocol, Domain domain, Type property, Dictionary<string, string> explicitMappings) { if (property.TypeReference != null) { @@ -64,12 +64,27 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator throw new ArgumentOutOfRangeException(); } + string fullReferenceName = $"{referencedDomain.Name}.{referencedType.Name}"; + // If it is a string, it can be resolved easily - if(referencedType.IsString()) + if (referencedType.IsString()) { property.Kind = "string"; property.TypeReference = null; } + else if (referencedType.IsInteger()) + { + property.Kind = "integer"; + property.TypeReference = null; + } + else if (explicitMappings.ContainsKey(fullReferenceName)) + { + property.TypeReference = explicitMappings[fullReferenceName]; + } + } + else if(property.Items != null) + { + ResolveTypeReferences(protocol, domain, property.Items, explicitMappings); } } diff --git a/source/ProtocolGenerator/Type.cs b/source/ProtocolGenerator/Type.cs index f383acfb1dfbf47d0c8dfba2a17d5f00498d1c9a..7d8f8cfc9850d3fb9b9691e0d5a92b3a14e67bee 100644 --- a/source/ProtocolGenerator/Type.cs +++ b/source/ProtocolGenerator/Type.cs @@ -144,6 +144,11 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator return string.Equals(this.Kind, "string", StringComparison.OrdinalIgnoreCase); } + public bool IsInteger() + { + return string.Equals(this.Kind, "integer", StringComparison.OrdinalIgnoreCase); + } + public bool IsEnum() { return this.Enum.Any();