blob: 358aeec331c04f1fa64bd99912dfad8ba2449cbe [file] [log] [blame]
package com.xtremelabs.robolectric.shadows;
import android.view.View;
import android.view.ViewGroup;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
/**
* Shadow for {@code ViewGroup} that simulates its implementation
*/
@SuppressWarnings({"UnusedDeclaration"})
@Implements(ViewGroup.class)
public class ShadowViewGroup extends ShadowView {
private List<View> children = new ArrayList<View>();
@Implementation
@Override
public View findViewById(int id) {
if (id == getId()) {
return realView;
}
for (View child : children) {
View found = child.findViewById(id);
if (found != null) {
return found;
}
}
return null;
}
@Implementation
public void addView(View child) {
children.add(child);
shadowOf(child).parent = this;
}
@Implementation
public int getChildCount() {
return children.size();
}
@Implementation
public View getChildAt(int index) {
return children.get(index);
}
@Implementation
public void removeAllViews() {
for (View child : children) {
shadowOf(child).parent = null;
}
children.clear();
}
@Implementation
public void removeViewAt(int position) {
shadowOf(children.remove(position)).parent = null;
}
@Override @Implementation
public boolean hasFocus() {
if (super.hasFocus()) return true;
for (View child : children) {
if (child.hasFocus()) return true;
}
return false;
}
@Implementation
@Override
public void clearFocus() {
if (hasFocus()) {
super.clearFocus();
for (View child : children) {
child.clearFocus();
}
}
}
/**
* Returns a string representation of this {@code ViewGroup} by concatenating all of the strings contained in all
* of the descendants of this {@code ViewGroup}.
* <p/>
* Robolectric extension.
*/
@Override
public String innerText() {
String innerText = "";
String delimiter = "";
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
String childText = shadowOf(child).innerText();
if (childText.length() > 0) {
innerText += delimiter;
delimiter = " ";
}
innerText += childText;
}
return innerText;
}
/**
* Non-Android method that dumps the state of this {@code ViewGroup} to {@code System.out}
*/
@Override public void dump(PrintStream out, int indent) {
dumpFirstPart(out, indent);
if (children.size() > 0) {
out.println(">");
for (View child : children) {
shadowOf(child).dump(out, indent + 2);
}
dumpIndent(out, indent);
out.println("</" + realView.getClass().getSimpleName() + ">");
} else {
out.println("/>");
}
}
}