Skip to content

Commit

Permalink
more detailed tests and some fixes for HtmlArea processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jul 25, 2021
1 parent 8794ce9 commit d543de2
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="2.52.0" date="August xx, 2021" description="Bugfixes, Firefox 90">
<action type="add" dev="rbri">
More detailed tests and some fixes for HtmlArea processing.
</action>
<action type="update" dev="rbri">
core-js: Two minor code optimizations regarding ConsString handling.
</action>
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/com/gargoylesoftware/htmlunit/html/HtmlArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,27 @@ private Rectangle2D parseRect() {
private Circle2D parseCircle() {
// browsers seem to support comma and blank
final String[] coords = StringUtils.split(getCoordsAttribute(), ", ");
final String radiusString = coords[2].trim();

final int radius;
double centerX = 0;
double centerY = 0;
double radius = 0;

try {
radius = Integer.parseInt(radiusString);
if (coords.length > 0) {
centerX = Double.parseDouble(coords[0].trim());
}
if (coords.length > 1) {
centerY = Double.parseDouble(coords[1].trim());
}
if (coords.length > 2) {
radius = Double.parseDouble(coords[2].trim());
}

}
catch (final NumberFormatException nfe) {
throw new NumberFormatException("Circle radius of " + radiusString + " is not yet implemented.");
catch (final NumberFormatException e) {
LOG.warn("Invalid circle coords '" + Arrays.toString(coords) + "'", e);
}

final double centerX = Double.parseDouble(coords[0].trim());
final double centerY = Double.parseDouble(coords[1].trim());
return new Circle2D(centerX, centerY, radius);
}

Expand All @@ -325,7 +334,7 @@ private Shape2D parsePoly() {
final String[] coords = StringUtils.split(getCoordsAttribute(), ", ");

try {
if (coords.length < 1) {
if (coords.length > 1) {
final Polygon2D path = new Polygon2D(Double.parseDouble(coords[0]), Double.parseDouble(coords[1]));

for (int i = 2; i + 1 < coords.length; i += 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public boolean contains(final double x, final double y) {
*/
@Override
public boolean isEmpty() {
// sufficient for now
return points_.size() > 1;
return points_.size() < 2;
}

@Override
Expand Down
132 changes: 130 additions & 2 deletions src/test/java/com/gargoylesoftware/htmlunit/html/HtmlAreaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.gargoylesoftware.htmlunit.BrowserRunner;
import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
import com.gargoylesoftware.htmlunit.BrowserRunner.BuggyWebDriver;
import com.gargoylesoftware.htmlunit.BrowserRunner.HtmlUnitNYI;
import com.gargoylesoftware.htmlunit.HttpHeader;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebDriverTestCase;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void referer() throws Exception {
* @throws Exception if an error occurs
*/
@Test
public void isDisplayed() throws Exception {
public void isDisplayedRect() throws Exception {
final String html = "<html><head><title>Page A</title></head>\n"
+ "<body>\n"
+ " <img id='myImg' usemap='#imgmap'"
Expand All @@ -124,6 +125,60 @@ public void isDisplayed() throws Exception {
assertTrue(displayed);
}

/**
* @throws Exception if an error occurs
*/
@Test
public void isDisplayedCircle() throws Exception {
final String html = "<html><head><title>Page A</title></head>\n"
+ "<body>\n"
+ " <img id='myImg' usemap='#imgmap'"
+ " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
+ "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
+ " <map id='myMap' name='imgmap'>\n"
+ " <area id='myArea' shape='circle' coords='0,0,1'>\n"
+ " </map>\n"
+ "</body></html>";

final WebDriver driver = loadPageWithAlerts2(html);

boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
assertTrue(displayed);

displayed = driver.findElement(By.id("myMap")).isDisplayed();
assertTrue(displayed);

displayed = driver.findElement(By.id("myArea")).isDisplayed();
assertTrue(displayed);
}

/**
* @throws Exception if an error occurs
*/
@Test
public void isDisplayedPolygon() throws Exception {
final String html = "<html><head><title>Page A</title></head>\n"
+ "<body>\n"
+ " <img id='myImg' usemap='#imgmap'"
+ " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
+ "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
+ " <map id='myMap' name='imgmap'>\n"
+ " <area id='myArea' shape='poly' coords='7,9,5,1,2,2'>\n"
+ " </map>\n"
+ "</body></html>";

final WebDriver driver = loadPageWithAlerts2(html);

boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
assertTrue(displayed);

displayed = driver.findElement(By.id("myMap")).isDisplayed();
assertTrue(displayed);

displayed = driver.findElement(By.id("myArea")).isDisplayed();
assertTrue(displayed);
}

/**
* @throws Exception if an error occurs
*/
Expand Down Expand Up @@ -183,7 +238,7 @@ public void isDisplayedHiddenMap() throws Exception {
*/
@Test
@Alerts({"false", "false", "false", "false", "false", "true"})
public void isDisplayedEmptyArea() throws Exception {
public void isDisplayedEmptyRect() throws Exception {
final String html = "<html><head><title>Page A</title></head>\n"
+ "<body>\n"
+ " <img id='myImg' usemap='#imgmap'"
Expand Down Expand Up @@ -223,6 +278,79 @@ public void isDisplayedEmptyArea() throws Exception {
assertEquals(Boolean.parseBoolean(expected[5]), displayed);
}

/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = {"false", "false", "true"},
IE = {"false", "false", "false"})
@HtmlUnitNYI(IE = {"false", "false", "true"})
public void isDisplayedEmptyCircle() throws Exception {
final String html = "<html><head><title>Page A</title></head>\n"
+ "<body>\n"
+ " <img id='myImg' usemap='#imgmap'"
+ " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
+ "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
+ " <map id='myMap' name='imgmap'>\n"
+ " <area id='myArea1' shape='circle' coords='0,0,0'>\n"
+ " <area id='myArea2' shape='circle' >\n"
+ " <area id='myArea3' shape='circle' coords='0,0,0.8'>\n"
+ " </map>\n"
+ "</body></html>";

final String[] expected = getExpectedAlerts();

setExpectedAlerts(new String[] {});
final WebDriver driver = loadPageWithAlerts2(html);

boolean displayed = driver.findElement(By.id("myArea1")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[0]), displayed);

displayed = driver.findElement(By.id("myArea2")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[1]), displayed);

displayed = driver.findElement(By.id("myArea3")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[2]), displayed);
}


/**
* @throws Exception if an error occurs
*/
@Test
@Alerts({"false", "true", "false", "true"})
public void isDisplayedEmptyPolygon() throws Exception {
final String html = "<html><head><title>Page A</title></head>\n"
+ "<body>\n"
+ " <img id='myImg' usemap='#imgmap'"
+ " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
+ "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
+ " <map id='myMap' name='imgmap'>\n"
+ " <area id='myArea1' shape='poly' coords='0,0'>\n"
+ " <area id='myArea2' shape='poly' coords='0,0,1,1'>\n"
+ " <area id='myArea3' shape='poly' >\n"
+ " <area id='myArea4' shape='poly' coords='0,0,1,0,0,1'>\n"
+ " </map>\n"
+ "</body></html>";

final String[] expected = getExpectedAlerts();

setExpectedAlerts(new String[] {});
final WebDriver driver = loadPageWithAlerts2(html);

boolean displayed = driver.findElement(By.id("myArea1")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[0]), displayed);

displayed = driver.findElement(By.id("myArea2")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[1]), displayed);

displayed = driver.findElement(By.id("myArea3")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[2]), displayed);

displayed = driver.findElement(By.id("myArea4")).isDisplayed();
assertEquals(Boolean.parseBoolean(expected[3]), displayed);
}

/**
* @throws Exception if an error occurs
*/
Expand Down

0 comments on commit d543de2

Please sign in to comment.