Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShadowPreference doesn't implement getContext() #1671

Closed
ZacSweers opened this issue Apr 12, 2015 · 3 comments
Closed

ShadowPreference doesn't implement getContext() #1671

ZacSweers opened this issue Apr 12, 2015 · 3 comments

Comments

@ZacSweers
Copy link
Contributor

Real Preferences have a getContext() method, but the current ShadowPreference implementation currently doesn't. This breaks verifying custom styleable attributes on custom preference objects.

@erd
Copy link
Member

erd commented Apr 12, 2015

Methods that aren't shadowed simply call through to the underlying android object. If anything, ShadowPreference might need a constructor that calls the underlying Preference constructor. Could you provide some code that demonstrates the error you are seeing?

@ZacSweers
Copy link
Contributor Author

Sure. I have an annotation processing library that generates boilerplate TypedArray stuff for custom styleable attributes.

An example of it in use

Corresponding Robolectric test

The generated code looks like this:

public class CustomPreference$$Barbershop<T extends io.sweers.barber.sample.testing.CustomPreference> implements Barber.IBarbershop<T> {
  protected WeakHashSet lastStyledTargets = new WeakHashSet();

  @Override
  public void style(final T target, final AttributeSet set, final int[] attrs, final int defStyleAttr, final int defStyleRes) {
    this.lastStyledTargets.add(target);
    if (set == null) {
      return;
    }
    TypedArray a = target.getContext().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes);
    // Retrieve custom attributes
    if (a.hasValue(0)) {
      target.titleColor = a.getColor(0, -1);
    }
    if (a.hasValue(1)) {
      target.summaryColor = a.getColor(1, -1);
    }
    a.recycle();
  }

  protected boolean hasStyled(final T target) {
    return this.lastStyledTargets.contains(target);
  }
}

The problem occurs via NPE on this line:

TypedArray a = target.getContext().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes);

Because target.getContext() returns null. I thought this is because Robolectric is unable to find this method for some reason, but maybe it's just because getContext() itself is returning null. I assumed it was because it wasn't implemented, but I understand now that I misinterpreted how ShadowPreference worked.

Let me know if you need any other info!

@xian
Copy link
Member

xian commented Oct 31, 2018

Robolectric 4.0 fixes many longstanding issues; could you check to see if this remains a problem?

If so, please feel free to reopen this issue and include a link to a public git repo demonstrating the problem to help us diagnose. Thank you!

@xian xian closed this as completed Oct 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants