package com.marakana.test;

import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.ViewAsserts;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.EditText;

import com.marakana.TestDemo;

/*
 * Test code to test com.marakana.TestDemo
 * 
 * To run on command line:
 * adb -e shell am instrument -w -e class com.marakana.test.TestDemoTests 
 *              com.marakana.test/android.test.InstrumentationTestRunner
 */
public class TestDemoTests extends ActivityInstrumentationTestCase2<TestDemo> {
  EditText editKilos, editPounds;
  TestDemo activity;

  public TestDemoTests(String name) {
    super("com.marakana", TestDemo.class);
    setName(name);
  }

  protected void setUp() throws Exception {
    super.setUp();

    // Find views
    activity = getActivity();
    editKilos = (EditText) activity.findViewById(com.marakana.R.id.editKilos);
    editPounds = (EditText) activity.findViewById(com.marakana.R.id.editPounds);
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }

  @SmallTest
  public void testViewsCreated() {
    assertNotNull(getActivity());
    assertNotNull(editKilos);
    assertNotNull(editPounds);
  }

  @SmallTest
  public void testViewsVisible() {
    ViewAsserts.assertOnScreen(editKilos.getRootView(), editPounds);
    ViewAsserts.assertOnScreen(editPounds.getRootView(), editKilos);
  }

  @SmallTest
  public void testStartingEmpty() {
    assertTrue("Kilos field is empty",
        "".equals(editKilos.getText().toString()));
    assertTrue("Pounds field is empty",
        "".equals(editPounds.getText().toString()));
  }

  @SmallTest
  public void testKilosToPounds() {
    editKilos.clearComposingText();
    editPounds.clearComposingText();

    TouchUtils.tapView(this, editKilos);
    sendKeys("1");

    double pounds;
    try {
      pounds = Double.parseDouble(editPounds.getText().toString());
    } catch (NumberFormatException e) {
      pounds = -1;
    }
    assertTrue("1 kilo is 2.20462262 pounds", pounds > 2.2 && pounds < 2.3);
  }

}
