package com.test;
 
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
 
import junit.framework.TestCase;
 
import com.google.android.testing.nativedriver.client.AndroidNativeDriver;
import com.google.android.testing.nativedriver.client.AndroidNativeDriverBuilder;
import com.google.android.testing.nativedriver.client.AndroidNativeElement;
import com.google.android.testing.nativedriver.common.AndroidKeys;
import com.google.android.testing.nativedriver.common.AndroidNativeBy;
 
public class K9NativeTesting {
 
    private AndroidNativeDriver driver; //Defining the driver object of AndroidNativeDriver 
 
    @Before
    public void beforeTest() { // Before method that will get the driver object from getDriver()
        driver = getDriver();
    }
 
    @After
    public void afterTest() { //Runs after the test is run and shutdown the driver.
        driver.quit();
    }
 
     // Build/start the Android driver and returns it.
protected AndroidNativeDriver getDriver() {
        return new AndroidNativeDriverBuilder().withDefaultServer().build(); 
    }
     
      private void startK9Activity() {
            driver.startActivity("com.fsck.k9." +
                "activity.Accounts");
          }
     // starts the activity (application) mentioned. In this case it is com.fsck.k9 
    //    also you can see the activity.Accounts this you can from the logs of adb logcat while starting the application
       
      @Test //Test method to configure a new mail account in K9
      public void tesK9(){
          startK9Activity();
          driver.findElement(AndroidNativeBy.text("Next")).click();
          driver.findElement(AndroidNativeBy.id("account_email")).sendKeys("test@test.com");
 
// Here I am using the By.Id("account_email") you can get the Ids of the UI elements from the R.java
// file genrated by the android api while building an application. 
 
          WebElement ele=this.findAndClickElementById(driver, "account_password");
          ele.sendKeys("password");
          driver.findElement(AndroidNativeBy.text("Next")).click();
      }
       
      @Test /*Test method to compose a new mail in K9*/
      public void composeMail(){
          WebElement ele;
          startK9Activity();
          driver.getKeyboard().sendKeys(AndroidKeys.MENU);
          driver.findElement(AndroidNativeBy.text("Compose")).click();
          ele=this.findAndClickElementById(driver, "to");
          ele.sendKeys("send@test.com");
          ele=this.findAndClickElementById(driver, "subject");
          ele.sendKeys("Test Message");
          ele=this.findAndClickElementById(driver, "message_content");
          ele.sendKeys("This is a test message");
           
          driver.getKeyboard().sendKeys(AndroidKeys.MENU);
          driver.findElement(AndroidNativeBy.text("Send")).click();
           
      }
       
      public WebElement findAndClickElementById(AndroidNativeDriver tempDriver, String id){
          WebElement ele = tempDriver.findElementById(id);
          ele.click();
          return ele;
      }
 
}