/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package org.openqa.selenium;

import org.junit.Test;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Ignore.Driver.IE;
import static org.openqa.selenium.testing.Ignore.Driver.OPERA;
import static org.openqa.selenium.testing.Ignore.Driver.REMOTE;
import static org.openqa.selenium.testing.Ignore.Driver.SELENESE;
import static org.openqa.selenium.testing.TestUtilities.isFirefox30;
import static org.openqa.selenium.testing.TestUtilities.isNativeEventsEnabled;
import static org.openqa.selenium.TestWaiter.waitFor;
import static org.openqa.selenium.WaitingConditions.elementTextToEqual;

import java.util.List;

@Ignore(
    value = {HTMLUNIT, IE, CHROME, REMOTE, SELENESE, OPERA},
    reason = "HtmlUnit: SVG interaction is only implemented in rendered browsers")
public class SvgElementTest extends JUnit4TestBase {

  @Test
  public void testShouldClickOnGraphVisualElements() {
    driver.get(pages.svgPage);
    WebElement svg = driver.findElement(By.tagName("svg:svg"));

    if (isFirefox30(driver) && isNativeEventsEnabled(driver)) {
      System.out.println("Not testing SVG elements with Firefox 3.0 and native events as" +
          " this functionality is not working.");
      return;
    }

    List<WebElement> groupElements = svg.findElements(By.tagName("svg:g"));
    assertEquals(5, groupElements.size());

    groupElements.get(1).click();
    WebElement resultElement = driver.findElement(By.id("result"));

    waitFor(elementTextToEqual(resultElement, "slice_red"));
    assertEquals("slice_red", resultElement.getText());

    groupElements.get(2).click();
    resultElement = driver.findElement(By.id("result"));

    waitFor(elementTextToEqual(resultElement, "slice_green"));
    assertEquals("slice_green", resultElement.getText());
  }

  private static WebElement findAppleElement(List<WebElement> textElements) {
    for (WebElement currentElement : textElements) {
      if (currentElement.getText().contains("Apple")) {
        return currentElement;
      }
    }

    return null;
  }

  @Test
  public void testShouldClickOnGraphTextElements() {
    driver.get(pages.svgPage);
    WebElement svg = driver.findElement(By.tagName("svg:svg"));
    List<WebElement> textElements = svg.findElements(By.tagName("svg:text"));

    if (isFirefox30(driver) && isNativeEventsEnabled(driver)) {
      System.out.println("Not testing SVG elements with Firefox 3.0 and native events as" +
          " this functionality is not working.");
      return;
    }

    WebElement appleElement = findAppleElement(textElements);
    assertNotNull(appleElement);

    appleElement.click();
    WebElement resultElement = driver.findElement(By.id("result"));
    waitFor(elementTextToEqual(resultElement, "text_apple"));
    assertEquals("text_apple", resultElement.getText());
  }
}

@@ -652,24 +652,31 @@
 ;%0D%0A%0D%0Aimport 
+static 
 org.junit.Te
@@ -677,111 +677,75 @@
 nit.
-Test;%0D%0Aimport org.openqa.selenium.testing.Ignore;%0D%0Aimport org.openqa.selenium.testing.JUnit4TestBase;%0D%0A
+Assert.assertEquals;%0D%0Aimport static org.junit.Assert.assertNotNull;
 %0D%0Aim
@@ -769,32 +769,30 @@
 unit.Ass
-ert
+ume
 .ass
-ertEquals
+umeTrue
 ;%0D%0Aimpor
@@ -808,34 +808,42 @@
 org.
-junit.Assert.assertNotNull
+openqa.selenium.TestWaiter.waitFor
 ;%0D%0Ai
@@ -879,36 +879,44 @@
 ium.
-tes
+Wai
 ting
-.Ignore.Driver.CHROME
+Conditions.elementTextToEqual
 ;%0D%0Ai
@@ -1110,73 +1110,8 @@
 A;%0D%0A
-import static org.openqa.selenium.testing.Ignore.Driver.REMOTE;%0D%0A
 impo
@@ -1323,30 +1323,25 @@
 abled;%0D%0A
+%0D%0A
 import 
-static 
 org.open
@@ -1356,42 +1356,88 @@
 ium.
-TestWaiter.waitFor;%0D%0Aimport static
+internal.FindsByCssSelector;%0D%0Aimport org.openqa.selenium.testing.Ignore;%0D%0Aimport
  org
@@ -1457,44 +1457,82 @@
 ium.
-Wai
+tes
 ting
-Conditions.elementTextToEqual
+.JUnit4TestBase;%0D%0A%0D%0Aimport org.junit.Before;%0D%0Aimport org.junit.Test
 ;%0D%0A%0D
@@ -1599,22 +1599,13 @@
 IE, 
-CHROME, REMOTE
+OPERA
 , SE
@@ -1610,23 +1610,16 @@
 SELENESE
-, OPERA
 %7D,%0D%0A    
@@ -1750,24 +1750,142 @@
 stBase %7B%0D%0A%0D%0A
+  @Before%0D%0A  public void checkSupportsCssSelectors() %7B%0D%0A    assumeTrue(driver instanceof FindsByCssSelector);%0D%0A  %7D%0D%0A%0D%0A
   @Test%0D%0A  p
@@ -2008,37 +2008,37 @@
 dElement(By.
-tagName(%22svg:
+cssSelector(%22
 svg%22));%0D%0A%0D%0A 
@@ -2326,29 +2326,29 @@
 ents(By.
-tagName(%22svg:
+cssSelector(%22
 g%22));%0D%0A 
@@ -3247,29 +3247,29 @@
 ment(By.
-tagName(%22svg:
+cssSelector(%22
 svg%22));%0D
@@ -3329,21 +3329,21 @@
 (By.
-tagName(%22svg:
+cssSelector(%22
 text

Change SvgElementTest to use CSS locators instead of tag names. The test now passes in Firefox, Chrome, and Safari. Opera is able to find the svg elements, but fails when attempting to interact with them.