Home | History | Annotate | Download | only in calculator2
      1 /**
      2  * Copyright (c) 2008, Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.calculator2;
     18 
     19 import android.app.Activity;
     20 import android.app.Instrumentation;
     21 import android.app.Instrumentation.ActivityMonitor;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.test.ActivityInstrumentationTestCase;
     25 import android.test.suitebuilder.annotation.LargeTest;
     26 import android.util.Log;
     27 import android.view.KeyEvent;
     28 import android.view.View;
     29 import android.widget.EditText;
     30 import android.widget.Button;
     31 import android.widget.LinearLayout;
     32 import android.graphics.Rect;
     33 import android.test.TouchUtils;
     34 
     35 import com.android.calculator2.Calculator;
     36 import com.android.calculator2.R;
     37 import com.android.calculator2.CalculatorDisplay;
     38 
     39 /**
     40  * Instrumentation tests for poking some buttons
     41  *
     42  */
     43 
     44 public class CalculatorHitSomeButtons extends ActivityInstrumentationTestCase <Calculator>{
     45     public boolean setup = false;
     46     private static final String TAG = "CalculatorTests";
     47     Calculator mActivity = null;
     48     Instrumentation mInst = null;
     49 
     50     public CalculatorHitSomeButtons() {
     51         super("com.android.calculator2", Calculator.class);
     52     }
     53 
     54     @Override
     55     protected void setUp() throws Exception {
     56         super.setUp();
     57 
     58         mActivity = getActivity();
     59         mInst = getInstrumentation();
     60     }
     61 
     62     @Override
     63     protected void tearDown() throws Exception {
     64         super.tearDown();
     65     }
     66 
     67     @LargeTest
     68     public void testPressSomeKeys() {
     69         Log.v(TAG, "Pressing some keys!");
     70 
     71         // Make sure that we clear the output
     72         press(KeyEvent.KEYCODE_ENTER);
     73         press(KeyEvent.KEYCODE_CLEAR);
     74 
     75         // 3 + 4 * 5 => 23
     76         press(KeyEvent.KEYCODE_3);
     77         press(KeyEvent.KEYCODE_PLUS);
     78         press(KeyEvent.KEYCODE_4);
     79         press(KeyEvent.KEYCODE_9 | KeyEvent.META_SHIFT_ON);
     80         press(KeyEvent.KEYCODE_5);
     81         press(KeyEvent.KEYCODE_ENTER);
     82 
     83         assertEquals(displayVal(), "23");
     84     }
     85 
     86     @LargeTest
     87     public void testTapSomeButtons() {
     88         Log.v(TAG, "Tapping some buttons!");
     89 
     90         // Make sure that we clear the output
     91         tap(R.id.equal);
     92         tap(R.id.del);
     93 
     94         // 567 / 3 => 189
     95         tap(R.id.digit5);
     96         tap(R.id.digit6);
     97         tap(R.id.digit7);
     98         tap(R.id.div);
     99         tap(R.id.digit3);
    100         tap(R.id.equal);
    101 
    102         assertEquals(displayVal(), "189");
    103 
    104         // make sure we can continue calculations also
    105         // 189 - 789 => -600
    106         tap(R.id.minus);
    107         tap(R.id.digit7);
    108         tap(R.id.digit8);
    109         tap(R.id.digit9);
    110         tap(R.id.equal);
    111 
    112         // Careful: the first digit in the expected value is \u2212, not "-" (a hyphen)
    113         assertEquals(displayVal(), mActivity.getString(R.string.minus) + "600");
    114     }
    115 
    116     // helper functions
    117     private void press(int keycode) {
    118         mInst.sendKeyDownUpSync(keycode);
    119     }
    120 
    121     private boolean tap(int id) {
    122         View view = mActivity.findViewById(id);
    123         if(view != null) {
    124             TouchUtils.clickView(this, view);
    125             return true;
    126         }
    127         return false;
    128     }
    129 
    130     private String displayVal() {
    131         CalculatorDisplay display = (CalculatorDisplay) mActivity.findViewById(R.id.display);
    132         assertNotNull(display);
    133 
    134         EditText box = (EditText) display.getCurrentView();
    135         assertNotNull(box);
    136 
    137         return box.getText().toString();
    138     }
    139 }
    140 
    141