Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.content.Context;
      4 import android.graphics.drawable.Drawable;
      5 import android.view.MotionEvent;
      6 import android.view.View;
      7 import android.view.WindowManager;
      8 import android.widget.LinearLayout;
      9 import android.widget.PopupWindow;
     10 import com.xtremelabs.robolectric.internal.Implementation;
     11 import com.xtremelabs.robolectric.internal.Implements;
     12 import com.xtremelabs.robolectric.internal.RealObject;
     13 
     14 @SuppressWarnings({"UnusedDeclaration"})
     15 @Implements(PopupWindow.class)
     16 public class ShadowPopupWindow {
     17     @RealObject
     18     protected PopupWindow realPopupWindow;
     19     private View contentView;
     20     private int width;
     21     private int height;
     22     private boolean focusable;
     23     private boolean touchable;
     24     private boolean outSideTouchable;
     25     private boolean showing;
     26     private Drawable background;
     27     private View.OnTouchListener onTouchInterceptor;
     28     private Context context;
     29     private LinearLayout containerView;
     30     private int xOffset;
     31     private int yOffset;
     32 
     33     public void __constructor__(View contentView) {
     34         setContentView(contentView);
     35         getWindowManager();
     36     }
     37 
     38     public void __constructor__(View contentView, int width, int height, boolean focusable) {
     39         __constructor__(contentView);
     40         this.width = width;
     41         this.height = height;
     42         this.focusable = focusable;
     43     }
     44 
     45     @Implementation
     46     public void setContentView(View contentView) {
     47         this.contentView = contentView;
     48         context = contentView.getContext();
     49     }
     50 
     51     @Implementation
     52     public View getContentView() {
     53         return contentView;
     54     }
     55 
     56     @Implementation
     57     public void setWidth(int width) {
     58         this.width = width;
     59     }
     60 
     61     @Implementation
     62     public int getWidth() {
     63         return width;
     64     }
     65 
     66     @Implementation
     67     public void setHeight(int height) {
     68         this.height = height;
     69     }
     70 
     71     @Implementation
     72     public int getHeight() {
     73         return height;
     74     }
     75 
     76     @Implementation
     77     public void setFocusable(boolean focusable) {
     78         this.focusable = focusable;
     79     }
     80 
     81     @Implementation
     82     public boolean isFocusable() {
     83         return focusable;
     84     }
     85 
     86     @Implementation
     87     public void setTouchable(boolean touchable) {
     88         this.touchable = touchable;
     89     }
     90 
     91     @Implementation
     92     public boolean isTouchable() {
     93         return touchable;
     94     }
     95 
     96     @Implementation
     97     public void setOutsideTouchable(boolean touchable) {
     98         outSideTouchable = touchable;
     99     }
    100 
    101     @Implementation
    102     public boolean isOutsideTouchable() {
    103         return outSideTouchable;
    104     }
    105 
    106     /**
    107      * non-android setter for testing
    108      *
    109      * @param showing true if popup window is showing
    110      */
    111     public void setShowing(boolean showing) {
    112         this.showing = showing;
    113     }
    114 
    115     @Implementation
    116     public boolean isShowing() {
    117         return showing;
    118     }
    119 
    120     @Implementation
    121     public void dismiss() {
    122         if (context != null) {
    123             getWindowManager().removeView(containerView);
    124         }
    125         showing = false;
    126     }
    127 
    128     @Implementation
    129     public void setBackgroundDrawable(Drawable background) {
    130         this.background = background;
    131     }
    132 
    133     @Implementation
    134     public Drawable getBackground() {
    135         return background;
    136     }
    137 
    138     @Implementation
    139     public void setTouchInterceptor(android.view.View.OnTouchListener l) {
    140         onTouchInterceptor = l;
    141     }
    142 
    143     @Implementation
    144     public void showAsDropDown(View anchor) {
    145         containerView = new LinearLayout(context);
    146         containerView.addView(contentView);
    147         containerView.setBackgroundDrawable(background);
    148         getWindowManager().addView(containerView, null);
    149     }
    150 
    151     @Implementation
    152     public void showAsDropDown(View anchor, int xoff, int yoff) {
    153         xOffset = xoff;
    154         yOffset = yoff;
    155         showAsDropDown(anchor);
    156     }
    157 
    158     public boolean dispatchTouchEvent(MotionEvent e) {
    159         return onTouchInterceptor != null && onTouchInterceptor.onTouch(realPopupWindow.getContentView(), e);
    160     }
    161 
    162     private WindowManager getWindowManager() {
    163         return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    164     }
    165 
    166     public int getXOffset() {
    167         return xOffset;
    168     }
    169 
    170     public int getYOffset() {
    171         return yOffset;
    172     }
    173 }
    174