Skip to content

Instantly share code, notes, and snippets.

@justinmuller
Forked from xian/00_README.md
Last active December 31, 2015 18:09
Show Gist options
  • Save justinmuller/8025465 to your computer and use it in GitHub Desktop.
Save justinmuller/8025465 to your computer and use it in GitHub Desktop.
Test code that uses ActionBarSherlock with Robolectric 2.2+

You need to add the files below, and do this in @Before (as mentioned in https://gist.github.com/marsucsb/6059760)

ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockNative.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockCompat.class);
// Copyright 2012 Square, Inc. License: Apache 2.
package com.squareup.test;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import com.actionbarsherlock.ActionBarSherlock;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.internal.ActionBarSherlockCompat;
import com.actionbarsherlock.internal.ActionBarSherlockNative;
import com.actionbarsherlock.view.MenuInflater;
import com.squareup.test.actionbarsherlock.SherlockMenuInflater;
import static org.robolectric.Robolectric.shadowOf;
/**
* During initialization, {@link ActionBarSherlock} figures out which {@link
* com.actionbarsherlock.app.ActionBar} to use based on the API level. It does this by checking the
* Build.Version.SDK_INT value which depends on the hidden <i>SystemProperties</i> class.
*
* Because Roboelectric does not have this, it always returns <code>0</code> for its API level
* causing {@link ActionBarSherlock} to crash. This class helps resolve this issue by providing
* an {@link ActionBarSherlockNative} implementation for API level 0.
* @see ActionBarSherlock#registerImplementation(Class)
*/
@ActionBarSherlock.Implementation(api = 0)
public class ActionBarSherlockRobolectric extends ActionBarSherlockCompat {
final private ActionBar actionBar;
public ActionBarSherlockRobolectric(Activity activity, int flags) {
super(activity, flags);
actionBar = new MockActionBar(activity);
}
@Override public void setContentView(int layoutResId) {
LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
View contentView = layoutInflater.inflate(layoutResId, null);
setContentView(contentView);
}
@Override public void setContentView(View view) {
mActivity.getWindow().setContentView(view);
mActivity.onContentChanged();
}
@Override public ActionBar getActionBar() {
return actionBar;
}
@Override protected Context getThemedContext() {
return mActivity;
}
@Override public MenuInflater getMenuInflater() {
if (mMenuInflater == null) {
mMenuInflater = new SherlockMenuInflater(mActivity);
}
return mMenuInflater;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment