1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.widget.Checkable;
4 import android.widget.CompoundButton;
5 import com.xtremelabs.robolectric.internal.Implementation;
6 import com.xtremelabs.robolectric.internal.Implements;
7 
8 /**
9  * Shadows the {@code android.widget.CompoundButton} class.
10  * <p/>
11  * Keeps track of whether or not its "checked" state is set and deals with listeners in an appropriate way.
12  */
13 @SuppressWarnings({"UnusedDeclaration"})
14 @Implements(CompoundButton.class)
15 public class ShadowCompoundButton extends ShadowTextView implements Checkable {
16     private boolean checked;
17     private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
18 
applyAttributes()19     @Override public void applyAttributes() {
20         super.applyAttributes();
21         setChecked(this.attributeSet.getAttributeBooleanValue("android", "checked", false));
22     }
23 
24     @Implementation
toggle()25     @Override public void toggle() {
26         setChecked(!checked);
27     }
28 
29     @Implementation
performClick()30     @Override public boolean performClick() {
31         toggle();
32         return super.performClick();
33     }
34 
35     @Implementation
isChecked()36     @Override public boolean isChecked() {
37         return checked;
38     }
39 
40     @Implementation
setChecked(boolean checked)41     @Override public void setChecked(boolean checked) {
42         if (this.checked != checked) {
43             this.checked = checked;
44             if (onCheckedChangeListener != null) {
45                 onCheckedChangeListener.onCheckedChanged((CompoundButton) realView, this.checked);
46             }
47         }
48     }
49 
50     @Implementation
setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)51     public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
52         onCheckedChangeListener = listener;
53     }
54 
getOnCheckedChangeListener()55     public CompoundButton.OnCheckedChangeListener getOnCheckedChangeListener() {
56     	return onCheckedChangeListener;
57     }
58 }
59