Views { public static void removeFromParent(View view) { if (view == null || view.getParent() == null) { return; } if (view.getParent() instanceof ViewGroup) { ((ViewGroup) view.getParent()).removeView(view); } }  static void removeFromParent(View view);  }
@Test public void removeFromParent_shouldRemoveViewFromParent() throws Exception { assertThat(parent.getChildCount()).isEqualTo(0); parent.addView(subject); assertThat(parent.getChildCount()).isEqualTo(1); assertThat(subject.getParent()).isEqualTo(parent); Views.removeFromParent(subject); assertThat(parent.getChildCount()).isEqualTo(0); assertThat(subject.getParent()).isNull(); }
@Test public void removeFromParent_withMultipleChildren_shouldRemoveCorrectChild() throws Exception { parent.addView(new TextView(context)); assertThat(parent.getChildCount()).isEqualTo(1); parent.addView(subject); assertThat(parent.getChildCount()).isEqualTo(2); Views.removeFromParent(subject); assertThat(parent.getChildCount()).isEqualTo(1); assertThat(parent.getChildAt(0)).isInstanceOf(TextView.class); }
@Test public void removeFromParent_whenViewIsNull_shouldPass() throws Exception { Views.removeFromParent(null); }
@Test public void removeFromParent_whenViewsParentIsNull_shouldPass() throws Exception { assertThat(subject.getParent()).isNull(); Views.removeFromParent(subject); }