package org.robolectric.shadows;

import android.app.Notification;
import android.os.Parcel;
import android.view.accessibility.AccessibilityEvent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.TestRunners;

import static org.assertj.core.api.Assertions.assertThat;
import static org.robolectric.Shadows.shadowOf;

@RunWith(TestRunners.WithDefaults.class)
public class ShadowAccessibilityEventTest {

  private AccessibilityEvent event;
  private ShadowAccessibilityEvent shadow;

  @Before
  public void setUp() {
    ShadowAccessibilityEvent.resetObtainedInstances();
    assertThat(ShadowAccessibilityEvent.areThereUnrecycledEvents(true)).isEqualTo(false);
    event = ShadowAccessibilityEvent.obtain();
    shadow = shadowOf(event);
    assertThat(shadow != null).isEqualTo(true);
  }

  @Test
  public void shouldHaveObtainedEvent() {
    assertThat(ShadowAccessibilityEvent.areThereUnrecycledEvents(false)).isEqualTo(true);
  }

  @Test
  public void shouldRecordParcelables() {
    final Notification notification = new Notification();
    event.setParcelableData(notification);
    AccessibilityEvent anotherEvent = AccessibilityEvent.obtain(event);
    assertThat(anotherEvent.getParcelableData() instanceof Notification).isEqualTo(true);
    assertThat(anotherEvent.getParcelableData()).isEqualTo(notification);
    anotherEvent.recycle();
  }

  @Test
  public void shouldEqualToClonedEvent() {
    shadow.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
    AccessibilityEvent newEvent = ShadowAccessibilityEvent.obtain(event);
    assertThat(shadow.equals(newEvent)).isEqualTo(true);
    newEvent.recycle();
  }

  @Test
  public void shouldWriteAndReadFromParcelCorrectly() {
    Parcel p = Parcel.obtain();
    event.setContentDescription("test");
    event.writeToParcel(p, 0);
    p.setDataPosition(0);
    AccessibilityEvent anotherEvent = AccessibilityEvent.CREATOR.createFromParcel(p);
    assertThat(event).isEqualTo(anotherEvent);
    event.setContentDescription(null);
    anotherEvent.recycle();
  }

  @After
  public void tearDown() {
    shadow.recycle();
    assertThat(ShadowAccessibilityEvent.areThereUnrecycledEvents(true)).isEqualTo(false);
  }
}


@@ -139,16 +139,48 @@
 tyEvent;
+%0Aimport android.widget.TextView;
 %0A%0Aimport
@@ -278,16 +278,59 @@
 unWith;%0A
+import org.robolectric.RuntimeEnvironment;%0A
 import o
@@ -2178,16 +2178,430 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void shouldHaveCurrentSourceId() %7B%0A    TextView rootView = new TextView(RuntimeEnvironment.application);%0A    event.setSource(rootView);%0A    assertThat(shadow.getSourceRoot()).isEqualTo(rootView);%0A    assertThat(shadow.getVirtualDescendantId()).isEqualTo(ShadowAccessibilityRecord.NO_VIRTUAL_ID);%0A    event.setSource(rootView, 1);%0A    assertThat(shadow.getVirtualDescendantId()).isEqualTo(1);%0A  %7D%0A%0A
   @After

add test