package io.appium.java_client.pagefactory_tests;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.SelendroidFindAll;
import io.appium.java_client.pagefactory.SelendroidFindBy;
import io.appium.java_client.pagefactory.SelendroidFindBys;
import io.appium.java_client.remote.MobileCapabilityType;

import org.openqa.selenium.WebElement;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class SelendroidModeTest {
	private static int SELENDROID_PORT = 9999;

    private WebDriver driver;

    @SelendroidFindBy(id = "text1")
    private WebElement textId;

    @AndroidFindBy(id = "Invalid Identifier")
    @SelendroidFindBy(id = "text1")
    private WebElement textSelendroidId;

    @SelendroidFindBy(name = "Accessibility")
    private WebElement textName;

    @AndroidFindBy(name = "Accessibility")
    private WebElement textNameAndroid;

    @FindBy(name = "Accessibility")
    private WebElement textNameDefault;

    @SelendroidFindBy(xpath = "//TextView[@value='Accessibility']")
    private WebElement textXpath;

    @SelendroidFindBys({
        @SelendroidFindBy(id = "text1")})
    private WebElement textIds;

    @SelendroidFindAll({
        @SelendroidFindBy(id = "text1")})
    private WebElement textAll;

    @SelendroidFindAll({
        @SelendroidFindBy(id = "text1")})
    private List<WebElement> textsAll;

    @SelendroidFindBy(className = "android.widget.TextView")
    private WebElement textClass;

    @SelendroidFindBy(tagName = "TextView")
    private WebElement textTag;

	@Before
    public void setUp() throws Exception {
        File appDir = new File("src/test/java/io/appium/java_client");
        File app = new File(appDir, "ApiDemos-debug.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Selendroid");
        capabilities.setCapability(MobileCapabilityType.SELENDROID_PORT, SELENDROID_PORT);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

        //This time out is set because test can be run on slow Android SDK emulator
        PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public void findByIdElementTest() {
        Assert.assertNotEquals(null, textId.getAttribute("text"));
    }
    
        @Test
    public void findBySelendroidSelectorTest() {
        Assert.assertNotEquals(null, textSelendroidId.getAttribute("text"));
    }

    @Test
    public void findByElementByNameTest() {
        Assert.assertEquals("Accessibility", textName.getText());
    }

    @Test
    public void findByElementByNameAndroidTest() {
        Assert.assertEquals("Accessibility", textNameAndroid.getText());
    }

    @Test
    public void findByElementByNameDefaultTest() {
        Assert.assertEquals("Accessibility", textNameDefault.getText());
    }

    @Test
    public void findByElementByXpathTest() {
        Assert.assertEquals("Accessibility", textXpath.getText());
    }

    @Test
    public void findByElementByIdsTest() {
        Assert.assertNotNull(textIds.getText());
    }

    @Test
    public void findByElementByTestAllTest() {
        Assert.assertNotNull(textAll.getText());
    }

    @Test
    public void findByElementByTextsAllTest() {
        Assert.assertTrue(textsAll.size() > 1);
    }

    @Test
    public void findByElementByCalssTest() {
        Assert.assertNotEquals(null, textClass.getAttribute("text"));
    }

    @Test
    public void findByElementByTagTest() {
        Assert.assertNotEquals(null, textTag.getAttribute("text"));
    }
    @Test
    public void findBySelendroidAnnotationOnlyTest() {
        Assert.assertNotEquals(null, textSelendroidId.getAttribute("text"));
    }

}

@@ -2002,16 +2002,197 @@
 textTag;
+%0A    %0A    @SelendroidFindBy(linkText = %22Accessibility%22)%0A    private WebElement textLink;%0A    %0A    @SelendroidFindBy(linkText = %22ccessibilit%22)%0A    private WebElement textPartialLink;
 %0A%0A%09@Befo
@@ -4559,24 +4559,29 @@
 t%22));%0A    %7D%0A
+    %0A
     @Test%0A  
@@ -4716,11 +4716,298 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void findBySelendroidLinkTextTest() %7B%0A        Assert.assertEquals(%22Accessibility%22, textLink.getText());%0A%0A    %7D%0A    %0A    @Test%0A    public void findBySelendroidPartialLinkTextTest() %7B%0A        Assert.assertEquals(%22Accessibility%22, textPartialLink.getText());%0A%0A    %7D
 %0A%7D%0A

Add support for linkText and partialLinkText serch locators to @SelendroidFindBy annotation