Search in sources :

Example 1 with RealObject

use of org.robolectric.annotation.RealObject in project robolectric by robolectric.

the class ShadowFilter method filter.

@Implementation
public void filter(CharSequence constraint, Filter.FilterListener listener) {
    try {
        Class<?> forName = Class.forName("android.widget.Filter$FilterResults");
        Object filtering = ReflectionHelpers.callInstanceMethod(realObject, "performFiltering", ClassParameter.from(CharSequence.class, constraint));
        ReflectionHelpers.callInstanceMethod(realObject, "publishResults", ClassParameter.from(CharSequence.class, constraint), ClassParameter.from(forName, filtering));
        if (listener != null) {
            int count = filtering == null ? -1 : (int) ReflectionHelpers.getField(filtering, "count");
            listener.onFilterComplete(count);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot load android.widget.Filter$FilterResults");
    }
}
Also used : RealObject(org.robolectric.annotation.RealObject) Implementation(org.robolectric.annotation.Implementation)

Example 2 with RealObject

use of org.robolectric.annotation.RealObject in project robolectric by robolectric.

the class ShadowContextImpl method getSystemService.

@Implementation
public Object getSystemService(String name) {
    if (name.equals(Context.LAYOUT_INFLATER_SERVICE)) {
        return new RoboLayoutInflater(RuntimeEnvironment.application);
    }
    Object service = systemServices.get(name);
    if (service == null) {
        String serviceClassName = SYSTEM_SERVICE_MAP.get(name);
        if (serviceClassName == null) {
            System.err.println("WARNING: unknown service " + name);
            return null;
        }
        try {
            Class<?> clazz = Class.forName(serviceClassName);
            if (serviceClassName.equals("android.app.admin.DevicePolicyManager")) {
                if (getApiLevel() >= N) {
                    service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(IDevicePolicyManager.class, null), ClassParameter.from(boolean.class, false));
                } else {
                    service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(Handler.class, null));
                }
            } else if (serviceClassName.equals("android.app.SearchManager") || serviceClassName.equals("android.app.ActivityManager") || serviceClassName.equals("android.app.WallpaperManager")) {
                service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(Handler.class, null));
            } else if (serviceClassName.equals("android.os.storage.StorageManager")) {
                service = ReflectionHelpers.callConstructor(clazz);
            } else if (serviceClassName.equals("android.nfc.NfcManager") || serviceClassName.equals("android.telecom.TelecomManager")) {
                service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application));
            } else if (serviceClassName.equals("android.hardware.display.DisplayManager") || serviceClassName.equals("android.telephony.SubscriptionManager")) {
                service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application));
            } else if (serviceClassName.equals("android.view.accessibility.AccessibilityManager")) {
                service = AccessibilityManager.getInstance(realObject);
            } else if (getApiLevel() >= JELLY_BEAN_MR1 && serviceClassName.equals("android.view.WindowManagerImpl")) {
                Class<?> windowMgrImplClass = Class.forName("android.view.WindowManagerImpl");
                if (getApiLevel() >= N) {
                    service = ReflectionHelpers.callConstructor(windowMgrImplClass, ClassParameter.from(Context.class, realObject));
                } else {
                    Display display = newInstanceOf(Display.class);
                    service = ReflectionHelpers.callConstructor(windowMgrImplClass, ClassParameter.from(Display.class, display));
                }
            } else if (serviceClassName.equals("android.accounts.AccountManager")) {
                service = ReflectionHelpers.callConstructor(Class.forName("android.accounts.AccountManager"), ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(IAccountManager.class, null));
            } else if (serviceClassName.equals("android.net.wifi.p2p.WifiP2pManager")) {
                service = new WifiP2pManager(ReflectionHelpers.createNullProxy(IWifiP2pManager.class));
            } else if (getApiLevel() >= KITKAT && serviceClassName.equals("android.print.PrintManager")) {
                service = ReflectionHelpers.callConstructor(Class.forName("android.print.PrintManager"), ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(android.print.IPrintManager.class, null), ClassParameter.from(int.class, -1), ClassParameter.from(int.class, -1));
            } else {
                service = newInstanceOf(clazz);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        systemServices.put(name, service);
    }
    return service;
}
Also used : Context(android.content.Context) IAccountManager(android.accounts.IAccountManager) Handler(android.os.Handler) RealObject(org.robolectric.annotation.RealObject) WifiP2pManager(android.net.wifi.p2p.WifiP2pManager) IWifiP2pManager(android.net.wifi.p2p.IWifiP2pManager) Display(android.view.Display) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Implementation (org.robolectric.annotation.Implementation)2 RealObject (org.robolectric.annotation.RealObject)2 IAccountManager (android.accounts.IAccountManager)1 Context (android.content.Context)1 IWifiP2pManager (android.net.wifi.p2p.IWifiP2pManager)1 WifiP2pManager (android.net.wifi.p2p.WifiP2pManager)1 Handler (android.os.Handler)1 Display (android.view.Display)1