Home | History | Annotate | Download | only in helpers
      1 /*
      2  * Copyright (C) 2013 DroidDriver committers
      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 io.appium.droiddriver.helpers;
     18 
     19 import android.app.Activity;
     20 import android.test.ActivityInstrumentationTestCase2;
     21 import android.test.ActivityTestCase;
     22 
     23 import java.lang.reflect.Field;
     24 import java.lang.reflect.Modifier;
     25 
     26 /**
     27  * Fixes bugs in {@link ActivityInstrumentationTestCase2}.
     28  */
     29 public abstract class D2ActivityInstrumentationTestCase2<T extends Activity> extends
     30     ActivityInstrumentationTestCase2<T> {
     31   protected D2ActivityInstrumentationTestCase2(Class<T> activityClass) {
     32     super(activityClass);
     33   }
     34 
     35   /**
     36    * Fixes a bug in {@link ActivityTestCase#scrubClass} that causes
     37    * NullPointerException if your leaf-level test class declares static fields.
     38    * This is <a href="https://code.google.com/p/android/issues/detail?id=4244">a
     39    * known bug</a> that has been fixed in ICS Android release. But it still
     40    * exists on devices older than ICS. If your test class extends this class, it
     41    * can work on older devices.
     42    * <p>
     43    * In addition to the official fix in ICS and beyond, which skips
     44    * {@code final} fields, the fix below also skips {@code static} fields, which
     45    * should be the expectation of Java programmers.
     46    * </p>
     47    */
     48   @Override
     49   protected void scrubClass(final Class<?> testCaseClass) throws IllegalAccessException {
     50     final Field[] fields = getClass().getDeclaredFields();
     51     for (Field field : fields) {
     52       final Class<?> fieldClass = field.getDeclaringClass();
     53       if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
     54           && !Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
     55         try {
     56           field.setAccessible(true);
     57           field.set(this, null);
     58         } catch (Exception e) {
     59           android.util.Log.d("TestCase", "Error: Could not nullify field!");
     60         }
     61 
     62         if (field.get(this) != null) {
     63           android.util.Log.d("TestCase", "Error: Could not nullify field!");
     64         }
     65       }
     66     }
     67   }
     68 }
     69