michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.tests.helpers; michael@0: michael@0: import static org.mozilla.gecko.tests.helpers.AssertionHelper.fFail; michael@0: michael@0: import java.lang.reflect.Field; michael@0: michael@0: import android.content.Context; michael@0: import android.view.View; michael@0: michael@0: /** michael@0: * Provides helper functions for accessing Android framework features michael@0: * michael@0: * This class uses reflection to access framework functionalities that are michael@0: * unavailable through the regular Android API. Using reflection in this michael@0: * case is okay because it does not touch Gecko classes that go through michael@0: * ProGuard. michael@0: */ michael@0: public final class FrameworkHelper { michael@0: michael@0: private FrameworkHelper() { /* To disallow instantiation. */ } michael@0: michael@0: private static Field getClassField(final Class clazz, final String fieldName) michael@0: throws NoSuchFieldException { michael@0: Class cls = clazz; michael@0: do { michael@0: try { michael@0: return cls.getDeclaredField(fieldName); michael@0: } catch (final NoSuchFieldException e) { michael@0: cls = cls.getSuperclass(); michael@0: } michael@0: } while (cls != null); michael@0: // We tried getDeclaredField before; now try getField instead. michael@0: // getField behaves differently in that getField traverses the inheritance michael@0: // list, but it only works on public fields. While getField won't get us michael@0: // anything new, it makes code cleaner by throwing an exception for us. michael@0: return clazz.getField(fieldName); michael@0: } michael@0: michael@0: private static Object getField(final Object obj, final String fieldName) { michael@0: try { michael@0: final Field field = getClassField(obj.getClass(), fieldName); michael@0: final boolean accessible = field.isAccessible(); michael@0: field.setAccessible(true); michael@0: final Object ret = field.get(obj); michael@0: field.setAccessible(accessible); michael@0: return ret; michael@0: } catch (final NoSuchFieldException e) { michael@0: // We expect a valid field name; if it's not valid, michael@0: // the caller is doing something wrong and should be fixed. michael@0: fFail("Argument field should be a valid field name: " + e.toString()); michael@0: } catch (final IllegalAccessException e) { michael@0: // This should not happen. If it does, setAccessible above is not working. michael@0: fFail("Field should be accessible: " + e.toString()); michael@0: } michael@0: throw new IllegalStateException("Should not continue from previous failures"); michael@0: } michael@0: michael@0: private static void setField(final Object obj, final String fieldName, final Object value) { michael@0: try { michael@0: final Field field = getClassField(obj.getClass(), fieldName); michael@0: final boolean accessible = field.isAccessible(); michael@0: field.setAccessible(true); michael@0: field.set(obj, value); michael@0: field.setAccessible(accessible); michael@0: return; michael@0: } catch (final NoSuchFieldException e) { michael@0: // We expect a valid field name; if it's not valid, michael@0: // the caller is doing something wrong and should be fixed. michael@0: fFail("Argument field should be a valid field name: " + e.toString()); michael@0: } catch (final IllegalAccessException e) { michael@0: // This should not happen. If it does, setAccessible above is not working. michael@0: fFail("Field should be accessible: " + e.toString()); michael@0: } michael@0: throw new IllegalStateException("Cannot continue from previous failures"); michael@0: } michael@0: michael@0: public static Context getViewContext(final View v) { michael@0: return (Context) getField(v, "mContext"); michael@0: } michael@0: michael@0: public static void setViewContext(final View v, final Context c) { michael@0: setField(v, "mContext", c); michael@0: } michael@0: }