Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.tests.helpers; |
michael@0 | 6 | |
michael@0 | 7 | import static org.mozilla.gecko.tests.helpers.AssertionHelper.fFail; |
michael@0 | 8 | |
michael@0 | 9 | import java.lang.reflect.Field; |
michael@0 | 10 | |
michael@0 | 11 | import android.content.Context; |
michael@0 | 12 | import android.view.View; |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * Provides helper functions for accessing Android framework features |
michael@0 | 16 | * |
michael@0 | 17 | * This class uses reflection to access framework functionalities that are |
michael@0 | 18 | * unavailable through the regular Android API. Using reflection in this |
michael@0 | 19 | * case is okay because it does not touch Gecko classes that go through |
michael@0 | 20 | * ProGuard. |
michael@0 | 21 | */ |
michael@0 | 22 | public final class FrameworkHelper { |
michael@0 | 23 | |
michael@0 | 24 | private FrameworkHelper() { /* To disallow instantiation. */ } |
michael@0 | 25 | |
michael@0 | 26 | private static Field getClassField(final Class<?> clazz, final String fieldName) |
michael@0 | 27 | throws NoSuchFieldException { |
michael@0 | 28 | Class<?> cls = clazz; |
michael@0 | 29 | do { |
michael@0 | 30 | try { |
michael@0 | 31 | return cls.getDeclaredField(fieldName); |
michael@0 | 32 | } catch (final NoSuchFieldException e) { |
michael@0 | 33 | cls = cls.getSuperclass(); |
michael@0 | 34 | } |
michael@0 | 35 | } while (cls != null); |
michael@0 | 36 | // We tried getDeclaredField before; now try getField instead. |
michael@0 | 37 | // getField behaves differently in that getField traverses the inheritance |
michael@0 | 38 | // list, but it only works on public fields. While getField won't get us |
michael@0 | 39 | // anything new, it makes code cleaner by throwing an exception for us. |
michael@0 | 40 | return clazz.getField(fieldName); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | private static Object getField(final Object obj, final String fieldName) { |
michael@0 | 44 | try { |
michael@0 | 45 | final Field field = getClassField(obj.getClass(), fieldName); |
michael@0 | 46 | final boolean accessible = field.isAccessible(); |
michael@0 | 47 | field.setAccessible(true); |
michael@0 | 48 | final Object ret = field.get(obj); |
michael@0 | 49 | field.setAccessible(accessible); |
michael@0 | 50 | return ret; |
michael@0 | 51 | } catch (final NoSuchFieldException e) { |
michael@0 | 52 | // We expect a valid field name; if it's not valid, |
michael@0 | 53 | // the caller is doing something wrong and should be fixed. |
michael@0 | 54 | fFail("Argument field should be a valid field name: " + e.toString()); |
michael@0 | 55 | } catch (final IllegalAccessException e) { |
michael@0 | 56 | // This should not happen. If it does, setAccessible above is not working. |
michael@0 | 57 | fFail("Field should be accessible: " + e.toString()); |
michael@0 | 58 | } |
michael@0 | 59 | throw new IllegalStateException("Should not continue from previous failures"); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | private static void setField(final Object obj, final String fieldName, final Object value) { |
michael@0 | 63 | try { |
michael@0 | 64 | final Field field = getClassField(obj.getClass(), fieldName); |
michael@0 | 65 | final boolean accessible = field.isAccessible(); |
michael@0 | 66 | field.setAccessible(true); |
michael@0 | 67 | field.set(obj, value); |
michael@0 | 68 | field.setAccessible(accessible); |
michael@0 | 69 | return; |
michael@0 | 70 | } catch (final NoSuchFieldException e) { |
michael@0 | 71 | // We expect a valid field name; if it's not valid, |
michael@0 | 72 | // the caller is doing something wrong and should be fixed. |
michael@0 | 73 | fFail("Argument field should be a valid field name: " + e.toString()); |
michael@0 | 74 | } catch (final IllegalAccessException e) { |
michael@0 | 75 | // This should not happen. If it does, setAccessible above is not working. |
michael@0 | 76 | fFail("Field should be accessible: " + e.toString()); |
michael@0 | 77 | } |
michael@0 | 78 | throw new IllegalStateException("Cannot continue from previous failures"); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | public static Context getViewContext(final View v) { |
michael@0 | 82 | return (Context) getField(v, "mContext"); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | public static void setViewContext(final View v, final Context c) { |
michael@0 | 86 | setField(v, "mContext", c); |
michael@0 | 87 | } |
michael@0 | 88 | } |