Anonymous avatar Anonymous committed db81a99

added classes

Comments (0)

Files changed (8)

src/com/mobilebytes/andcalculon/FunctionalTest.java

+package com.mobilebytes.andcalculon;
+
+import com.mobilebytes.andcalculon.assertion.ActivityAssertion;
+import com.mobilebytes.andcalculon.assertion.ViewAssertion;
+
+import android.app.Activity;
+import android.test.ActivityInstrumentationTestCase2;
+import android.view.View;
+
+public class FunctionalTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
+
+    public FunctionalTest(String pkg, Class<T> activityClass) {
+        super(pkg, activityClass);
+    }
+
+    protected ActivityAssertion assertThat() {
+        return new ActivityAssertion(getActivity(), getInstrumentation());
+    }
+
+    protected ActivityAssertion assertThat(Activity activity) {
+        return new ActivityAssertion(activity, getInstrumentation());
+    }
+
+    protected ViewAssertion assertThat(View view) {
+        return new ViewAssertion(getActivity(), getInstrumentation(), view);
+    }
+
+    protected ViewAssertion assertThat(int viewId) {
+        View view = getActivity().findViewById(viewId);
+        return assertThat(view);
+    }
+
+    // protected void flipScreen(Activity activity) {
+    // DisplayMetrics dm = activity.getResources().getDisplayMetrics();
+    // Configuration config = activity.getResources().getConfiguration();
+    // int orientation = activity.getResources().getConfiguration().orientation;
+    // if (orientation == Configuration.ORIENTATION_PORTRAIT) {
+    // activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+    // config.orientation = Configuration.ORIENTATION_LANDSCAPE;
+    // } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
+    // activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+    // config.orientation = Configuration.ORIENTATION_PORTRAIT;
+    // }
+    // activity.getResources().updateConfiguration(config, dm);
+    // getInstrumentation().waitForIdleSync();
+    // }
+    //
+    // protected void flipScreen() {
+    // flipScreen(getActivity());
+    // }
+}

src/com/mobilebytes/andcalculon/action/Click.java

+package com.mobilebytes.andcalculon.action;
+
+public class Click {
+
+}

src/com/mobilebytes/andcalculon/assertion/ActionAssertion.java

+package com.mobilebytes.andcalculon.assertion;
+
+import static junit.framework.Assert.assertTrue;
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.app.Instrumentation.ActivityMonitor;
+import android.content.Context;
+
+public class ActionAssertion extends AssertionBase {
+
+    private Runnable action;
+
+    private boolean runOnMainThread;
+
+    public ActionAssertion(Activity activity, Instrumentation instrumentation, Runnable action,
+            boolean runOnMainThread) {
+        super(activity, instrumentation);
+        this.action = action;
+        this.runOnMainThread = runOnMainThread;
+    }
+
+    public ViewAssertion implies(int otherViewId) {
+        performPendingAction();
+        return new ViewAssertion(activity, instrumentation, activity
+            .findViewById(otherViewId));
+    }
+
+    public <C extends Context> Activity starts(Class<C> contextClass) {
+        ActivityMonitor monitor = instrumentation.addMonitor(contextClass.getCanonicalName(), null,
+            false);
+
+        requirePendingAction();
+        performPendingAction();
+
+        assertTrue(instrumentation.checkMonitorHit(monitor, 1));
+
+        return monitor.getLastActivity();
+    }
+
+    public void finishesActivity() {
+        performPendingAction();
+        assertTrue(activity.isFinishing());
+    }
+
+    protected void requirePendingAction() {
+        if (action == null) {
+            throw new IllegalStateException(
+                "This assertion relies on an action being set which triggers it, such as a click()");
+        }
+    }
+
+    protected void performPendingAction() {
+        if (action != null) {
+            if (runOnMainThread) {
+                instrumentation.runOnMainSync(action);
+            } else {
+                action.run();
+            }
+            instrumentation.waitForIdleSync();
+        }
+    }
+
+}

src/com/mobilebytes/andcalculon/assertion/ActivityAssertion.java

+package com.mobilebytes.andcalculon.assertion;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import android.app.Activity;
+import android.app.Instrumentation;
+
+public class ActivityAssertion extends UserInputAssertionBase<Activity> {
+
+    public ActivityAssertion(Activity activity, Instrumentation instrumentation) {
+        super(activity, instrumentation);
+        this.target = activity;
+    }
+
+    public void viewExists(int... viewIds) {
+        for (int id : viewIds) {
+            assertNotNull("View " + id + " is null, but expected to exist", activity
+                .findViewById(id));
+        }
+    }
+
+    public void inPortraitMode() {
+        assertEquals("Expected portrait mode, but was landscape", 0, activity.getWindowManager()
+            .getDefaultDisplay().getOrientation());
+    }
+
+    public void inLandscapeMode() {
+        assertEquals("Expected landscape mode, but was portrait", 1, activity.getWindowManager()
+            .getDefaultDisplay().getOrientation());
+    }
+}

src/com/mobilebytes/andcalculon/assertion/AssertionBase.java

+package com.mobilebytes.andcalculon.assertion;
+
+import android.app.Activity;
+import android.app.Instrumentation;
+
+public abstract class AssertionBase {
+
+    protected Activity activity;
+
+    protected Instrumentation instrumentation;
+
+    public AssertionBase(Activity activity, Instrumentation instrumentation) {
+        this.activity = activity;
+        this.instrumentation = instrumentation;
+    }
+}

src/com/mobilebytes/andcalculon/assertion/UserInputAssertionBase.java

+package com.mobilebytes.andcalculon.assertion;
+
+import com.mobilebytes.andcalculon.predicate.Predicate;
+
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.view.KeyEvent;
+
+public abstract class UserInputAssertionBase<T> extends AssertionBase {
+
+    protected T target;
+
+    public UserInputAssertionBase(Activity activity, Instrumentation instrumentation) {
+        super(activity, instrumentation);
+    }
+
+    public ActionAssertion keyDown(int keyCode) {
+        final int finalKeyCode = keyCode;
+        return new ActionAssertion(activity, instrumentation, new Runnable() {
+            public void run() {
+                UserInputAssertionBase.this.instrumentation.sendKeySync(new KeyEvent(
+                    KeyEvent.ACTION_DOWN, finalKeyCode));
+            }
+        }, false);
+    }
+
+    public ActionAssertion keyUp(int keyCode) {
+        final int finalKeyCode = keyCode;
+        return new ActionAssertion(activity, instrumentation, new Runnable() {
+            public void run() {
+                UserInputAssertionBase.this.instrumentation.sendKeySync(new KeyEvent(
+                    KeyEvent.ACTION_UP, finalKeyCode));
+            }
+        }, false);
+    }
+
+    public void satisfies(Predicate<T> predicate) {
+        try {
+            assertTrue(predicate.check(target));
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Terminated unexpectedly by exception");
+        }
+    }
+}

src/com/mobilebytes/andcalculon/assertion/ViewAssertion.java

+package com.mobilebytes.andcalculon.assertion;
+
+import static junit.framework.Assert.assertEquals;
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.view.View;
+
+public class ViewAssertion extends UserInputAssertionBase<View> {
+
+    public ViewAssertion(Activity activity, Instrumentation instrumentation, View view) {
+        super(activity, instrumentation);
+        this.target = view;
+    }
+
+    public ActionAssertion click() {
+        return new ActionAssertion(activity, instrumentation, new Runnable() {
+            public void run() {
+                target.performClick();
+            }
+        }, true);
+    }
+
+    public ActionAssertion longClick() {
+        return new ActionAssertion(activity, instrumentation, new Runnable() {
+            public void run() {
+                target.performLongClick();
+            }
+        }, true);
+    }
+
+    public void isVisible() {
+        assertEquals("view expected to be VISIBLE, but wasn't", View.VISIBLE, target
+            .getVisibility());
+    }
+
+    public void isInvisible() {
+        assertEquals("view expected to be INVISIBLE, but wasn't", View.INVISIBLE, target
+            .getVisibility());
+    }
+
+    public void isGone() {
+        assertEquals("view expected to be GONE, but wasn't", View.GONE, target.getVisibility());
+    }
+}

src/com/mobilebytes/andcalculon/predicate/Predicate.java

+package com.mobilebytes.andcalculon.predicate;
+
+public interface Predicate<T> {
+
+    public boolean check(T target);
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.