Skip to content

Commit 6d06ce7

Browse files
committed
Explicitly add CollectingAlertHandler so it is generated. Fixes #17.
1 parent e2735a6 commit 6d06ce7

7 files changed

Lines changed: 90 additions & 16 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Generated class v2.19.0.0, don't modify
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.Specialized;
6+
using System.Linq;
7+
using System.Text;
8+
9+
namespace NHtmlUnit
10+
{
11+
public partial class CollectingAlertHandler : ObjectWrapper, NHtmlUnit.IAlertHandler
12+
{
13+
static CollectingAlertHandler()
14+
{
15+
ObjectWrapper.RegisterWrapperCreator((com.gargoylesoftware.htmlunit.CollectingAlertHandler o) =>
16+
new CollectingAlertHandler(o));
17+
}
18+
19+
public CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler wrappedObject) : base(wrappedObject) {}
20+
21+
public com.gargoylesoftware.htmlunit.CollectingAlertHandler WObj
22+
{
23+
get { return (com.gargoylesoftware.htmlunit.CollectingAlertHandler)WrappedObject; }
24+
}
25+
26+
public CollectingAlertHandler(java.util.List list)
27+
: this(new com.gargoylesoftware.htmlunit.CollectingAlertHandler(list)) {}
28+
29+
public CollectingAlertHandler()
30+
: this(new com.gargoylesoftware.htmlunit.CollectingAlertHandler()) {}
31+
32+
33+
public IList<System.String> CollectedAlerts
34+
{
35+
get
36+
{
37+
return new ShallowListWrapper<System.String>(
38+
WObj.getCollectedAlerts());
39+
}
40+
}
41+
// Generating method code for handleAlert
42+
public virtual void HandleAlert(NHtmlUnit.IPage page, string message)
43+
{
44+
WObj.handleAlert((com.gargoylesoftware.htmlunit.Page)page.WrappedObject, message);
45+
}
46+
47+
}
48+
49+
50+
}

app/NHtmlUnit/NHtmlUnit.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Compile Include="Generated\BrowserVersion.cs" />
174174
<Compile Include="Generated\BrowserVersionFeatures.cs" />
175175
<Compile Include="Generated\Cache.cs" />
176+
<Compile Include="Generated\CollectingAlertHandler.cs" />
176177
<Compile Include="Generated\CookieManager.cs" />
177178
<Compile Include="Generated\DialogWindow.cs" />
178179
<Compile Include="Generated\FormEncodingType.cs" />
@@ -1151,6 +1152,7 @@
11511152
<Compile Include="NonGenerated\BrowserVersion.cs" />
11521153
<Compile Include="NonGenerated\BrowserVersionFeatures.cs" />
11531154
<Compile Include="NonGenerated\Cache.cs" />
1155+
<Compile Include="NonGenerated\CollectingAlertHandler.cs" />
11541156
<Compile Include="NonGenerated\CookieManager.cs" />
11551157
<Compile Include="NonGenerated\DialogWindow.cs" />
11561158
<Compile Include="NonGenerated\FormEncodingType.cs" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Generated class v2.19.0.0, can be modified
2+
3+
namespace NHtmlUnit
4+
{
5+
public partial class CollectingAlertHandler
6+
{
7+
}
8+
9+
10+
}

app/NHtmlUnit/NonGenerated/ObjectWrapper.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace NHtmlUnit
1818
{
1919
public class ObjectWrapper : IObjectWrapper
2020
{
21-
private static readonly Dictionary<Type, Func<object, object>> creatorDict =
21+
private static readonly Dictionary<Type, Func<object, object>> WrapperCreators =
2222
new Dictionary<Type, Func<object, object>>();
2323

2424
private readonly object wrappedObject;
@@ -45,25 +45,25 @@ public object WrappedObject
4545
public static T CreateWrapper<T>(object wrappedObject)
4646
{
4747
if (wrappedObject == null)
48-
return (T)wrappedObject;
48+
return default(T);
4949

50-
Func<object, object> creator = null;
50+
Func<object, object> creator;
5151
var fromType = wrappedObject.GetType();
5252

53-
// Search up type hierche until we find a matching creator
53+
// Search up type hierchy until we find a matching creator
5454
do
5555
{
56-
if (!creatorDict.TryGetValue(fromType, out creator))
56+
if (!WrapperCreators.TryGetValue(fromType, out creator))
5757
fromType = fromType.BaseType;
5858
} while (creator == null && fromType != null);
5959

6060
if (creator == null)
6161
{
62-
Console.WriteLine("No creator found for " + wrappedObject.GetType().FullName);
63-
throw new InvalidOperationException();
62+
throw new InvalidOperationException("No creator found for " + wrappedObject.GetType().FullName);
6463
}
6564

66-
return (T)creator(wrappedObject);
65+
var wrapper = creator(wrappedObject);
66+
return (T)wrapper;
6767
}
6868

6969

@@ -72,8 +72,7 @@ public override bool Equals(object obj)
7272
var otherObjectWrapper = obj as ObjectWrapper;
7373

7474
return otherObjectWrapper != null
75-
? WrappedObject.Equals(otherObjectWrapper.WrappedObject)
76-
: false;
75+
&& WrappedObject.Equals(otherObjectWrapper.WrappedObject);
7776
}
7877

7978

@@ -91,7 +90,7 @@ public override string ToString()
9190

9291
internal static void RegisterWrapperCreator<T>(Func<T, ObjectWrapper> wrapperCreator)
9392
{
94-
creatorDict.Add(typeof(T), o => wrapperCreator((T)o));
93+
WrapperCreators.Add(typeof(T), o => wrapperCreator((T)o));
9594
}
9695

9796

app/NHtmlUnitGenerator/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static void Main(string[] args)
5252
//GetJavaPropsFromType(typeof(HtmlPage));
5353
//GetJavaPropsFromType(typeof(HtmlInput));
5454

55-
wrapperRep.GenerateUntilDone(typeof(DomNode), typeof(WebClient), typeof(HtmlPage));
55+
wrapperRep.GenerateUntilDone(typeof(DomNode), typeof(WebClient), typeof(HtmlPage), typeof(CollectingAlertHandler));
5656

5757
wrapperRep.GenerateAssemblyInfo();
5858
wrapperRep.GenerateNuspec();

app/NHtmlUnitGenerator/WrapperMethodInfo.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public MethodInfo TargetMethodInfo
4747

4848
internal void GenerateMethodCode(StringBuilder sb)
4949
{
50-
sb.AppendLine("// Generating method code for " + TargetMethodInfo.Name);
50+
var methodName = TargetMethodInfo.Name;
51+
sb.AppendLine("// Generating method code for " + methodName);
5152

5253
var validListMapping = WrapperRepository.GetValidMapping(TargetMethodInfo.ReturnType);
5354
var returnValueIsWrappedList = validListMapping != null;
@@ -95,7 +96,7 @@ internal void GenerateMethodCode(StringBuilder sb)
9596

9697
// Change from camelCase to UpperCamelCase
9798

98-
string origName = TargetMethodInfo.Name;
99+
string origName = methodName;
99100
string transformedName = origName.Substring(0, 1).ToUpper() + origName.Substring(1);
100101

101102
// Check for condition where a method name has same name as property
@@ -143,7 +144,7 @@ internal void GenerateMethodCode(StringBuilder sb)
143144
// Generate function call
144145
var functionCallSb = new StringBuilder();
145146

146-
functionCallSb.AppendFormat("WObj.{0}(", TargetMethodInfo.Name);
147+
functionCallSb.AppendFormat("WObj.{0}(", methodName);
147148

148149
firstParameter = true;
149150
foreach (var mp in parameters)

tests/IntegrationTests/WebClientTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@
1111

1212
using System;
1313
using System.Linq;
14+
using java.util;
1415
using NHtmlUnit;
1516
using NHtmlUnit.Html;
1617
using NUnit.Framework;
18+
using List = java.util.List;
1719

1820
namespace IntegrationTests
1921
{
2022
[TestFixture]
2123
public class WebClientTests
2224
{
25+
[Test]
26+
public void CanSetAlertHandler()
27+
{
28+
var webClient = new WebClient(BrowserVersion.FIREFOX_38);
29+
30+
List collectedAlerts = new ArrayList();
31+
var alertHandler = new CollectingAlertHandler(collectedAlerts);
32+
webClient.AlertHandler = alertHandler;
33+
}
34+
2335
[Test]
2436
public void DoWikipediaSearch()
2537
{
@@ -31,7 +43,7 @@ public void DoWikipediaSearch()
3143
webClient.Options.CssEnabled = true;
3244

3345
var page = webClient.GetHtmlPage("http://wikipedia.org");
34-
46+
3547
page.GetElementById<HtmlInput>("searchInput").Type("network");
3648

3749
var searchButton = page.HtmlElementDescendants

0 commit comments

Comments
 (0)