package org.robolectric.shadows;

import static android.content.Context.ACCESSIBILITY_SERVICE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.robolectric.Shadows.shadowOf;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.view.accessibility.AccessibilityManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.TestRunners;
import org.robolectric.util.ReflectionHelpers;

@RunWith(TestRunners.MultiApiSelfTest.class)
public class ShadowAccessibilityManagerTest {

  private ShadowAccessibilityManager shadowAccessibilityManager;

  @Before
  public void setUp() throws Exception {
    shadowAccessibilityManager = shadowOf(getAccessibilityManager());
  }

  // Emulates Android framework behavior, e.g.,
  // AccessibilityManager.getInstance(context).isEnabled().
  private AccessibilityManager getAccessibilityManager() throws Exception {
    return ReflectionHelpers.callStaticMethod(AccessibilityManager.class, "getInstance",
            ReflectionHelpers.ClassParameter.from(Context.class, RuntimeEnvironment.application));
  }

  @Test
  public void shouldReturnTrueWhenEnabled() {
    shadowAccessibilityManager.setEnabled(true);
    assertThat(getAccessibilityManager().isEnabled()).isTrue();
  }

  @Test
  public void shouldReturnTrueForTouchExplorationWhenEnabled() {
    shadowAccessibilityManager.setTouchExplorationEnabled(true);
    assertThat(getAccessibilityManager().isTouchExplorationEnabled()).isTrue();
  }

  @Test
  public void shouldReturnExpectedEnabledServiceList() {
    List<AccessibilityServiceInfo> expected = new ArrayList<>(Arrays.asList(new AccessibilityServiceInfo()));
    shadowAccessibilityManager.setEnabledAccessibilityServiceList(expected);
    assertThat(getAccessibilityManager().getEnabledAccessibilityServiceList(0)).isEqualTo(expected);
  }

  @Test
  public void shouldReturnExpectedInstalledServiceList() {
    List<AccessibilityServiceInfo> expected = new ArrayList<>(Arrays.asList(new AccessibilityServiceInfo()));
    shadowAccessibilityManager.setInstalledAccessibilityServiceList(expected);
    assertThat(getAccessibilityManager().getInstalledAccessibilityServiceList()).isEqualTo(expected);
  }

  @Test
  public void shouldReturnExpectedAccessibilityServiceList() {
    List<ServiceInfo> expected = new ArrayList<>(Arrays.asList(new ServiceInfo()));
    shadowAccessibilityManager.setAccessibilityServiceList(expected);
    assertThat(getAccessibilityManager().getAccessibilityServiceList()).isEqualTo(expected);
  }
}

@@ -1372,35 +1372,52 @@
 rueWhenEnabled()
+ throws Exception
  %7B%0A
-
     shadowAccess
@@ -1588,16 +1588,33 @@
 nabled()
+ throws Exception
  %7B%0A    s
@@ -1812,32 +1812,49 @@
 ledServiceList()
+ throws Exception
  %7B%0A    List%3CAcce
@@ -2189,32 +2189,49 @@
 ledServiceList()
+ throws Exception
  %7B%0A    List%3CAcce
@@ -2513,32 +2513,32 @@
 );%0A  %7D%0A%0A  @Test%0A
-
   public void sh
@@ -2581,16 +2581,33 @@
 ceList()
+ throws Exception
  %7B%0A    L

Handle exceptions