@Test
  public void attachView_thenDetach_shouldSetMRaidWebView_thenShouldClear() {
    assertThat(subjectBanner.getMraidWebView()).isEqualTo(mockBannerWebView);

    subjectBanner.detach();
    assertThat(subjectBanner.getMraidWebView()).isNull();
  }
  public void runCommand_requiresClick_clicked_shouldNotThrowException()
      throws MraidCommandException {
    subjectBanner.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("uri", "http://valid-url");

    subjectBanner.runCommand(MraidJavascriptCommand.PLAY_VIDEO, params);
  }
  @Test
  public void runCommand_interstitial_requiresClick_clicked_shouldNotThrowException()
      throws MraidCommandException {
    subjectInterstitial.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("url", "http://valid-url");

    subjectInterstitial.runCommand(MraidJavascriptCommand.OPEN, params);
  }
  @Test(expected = MraidCommandException.class)
  public void runCommand_interstitial_requiresClick_notClicked_shouldThrowException()
      throws MraidCommandException {
    subjectInterstitial.setClicked(false);
    Map<String, String> params = new HashMap<String, String>();
    params.put("uri", "http://valid-url");

    subjectInterstitial.runCommand(MraidJavascriptCommand.OPEN, params);
  }
  @Test
  public void runCommand_expand_shouldCallListener() throws MraidCommandException {
    subjectBanner.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("shouldUseCustomClose", "true");

    subjectBanner.runCommand(MraidJavascriptCommand.EXPAND, params);

    verify(mockBridgeListener).onExpand(null, true);
  }
  @Test(expected = MraidCommandException.class)
  public void runCommand_requiresClick_notClicked_shouldThrowException()
      throws MraidCommandException {
    subjectBanner = new MraidBridge(mockAdReport, PlacementType.INLINE);
    subjectBanner.attachView(mockBannerWebView);
    subjectBanner.setClicked(false);
    Map<String, String> params = new HashMap<String, String>();
    params.put("uri", "http://valid-url");

    subjectBanner.runCommand(MraidJavascriptCommand.PLAY_VIDEO, params);
  }
  @Test
  public void runCommand_createCalendarEvent_shouldCallListener() throws MraidCommandException {
    subjectBanner.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("eventName", "Dinner at my house");

    subjectBanner.runCommand(MraidJavascriptCommand.CREATE_CALENDAR_EVENT, params);

    verify(mockNativeCommandHandler)
        .createCalendarEvent(any(Context.class), anyMapOf(String.class, String.class));
  }
  @Test
  public void runCommand_storePicture_shouldCallListener() throws MraidCommandException {
    subjectBanner.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("uri", "http://valid-url");

    subjectBanner.runCommand(MraidJavascriptCommand.STORE_PICTURE, params);

    verify(mockNativeCommandHandler)
        .storePicture(
            any(Context.class), eq("http://valid-url"), any(MraidCommandFailureListener.class));
  }
  @Test
  public void runCommand_playVideo_shouldCallListener() throws MraidCommandException {
    subjectBanner.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("uri", "http://valid-url");

    subjectBanner.runCommand(MraidJavascriptCommand.PLAY_VIDEO, params);

    ArgumentCaptor<URI> uriCaptor = ArgumentCaptor.forClass(URI.class);
    verify(mockBridgeListener).onPlayVideo(uriCaptor.capture());
    assertThat(uriCaptor.getValue().toString()).isEqualTo("http://valid-url");
  }
  @Test
  public void runCommand_expand_withUrl_shouldCallListener() throws MraidCommandException {
    subjectBanner.setClicked(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("url", "http://valid-url");
    params.put("shouldUseCustomClose", "true");

    subjectBanner.runCommand(MraidJavascriptCommand.EXPAND, params);

    ArgumentCaptor<URI> uriCaptor = ArgumentCaptor.forClass(URI.class);
    verify(mockBridgeListener).onExpand(uriCaptor.capture(), eq(true));
    assertThat(uriCaptor.getValue().toString()).isEqualTo("http://valid-url");
  }
  @Test
  public void handleShouldOverrideUrl_smsUrl_clicked_shouldStartActivity() {
    subjectBanner.setClicked(true);
    reset(mockBannerWebView);
    when(mockBannerWebView.getContext()).thenReturn(activity);

    boolean result = subjectBanner.handleShouldOverrideUrl("sms://123456789");

    Intent startedIntent = Robolectric.getShadowApplication().getNextStartedActivity();
    assertThat(startedIntent).isNotNull();
    assertThat(startedIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK).isNotEqualTo(0);
    assertThat(startedIntent.getComponent()).isNull();
    assertThat(result).isTrue();
  }
  @Test
  public void handleShouldOverrideUrl_mraidUrl_invalid_shouldFireErrorEvent_shouldReturnTrue() {
    boolean result = subjectBanner.handleShouldOverrideUrl("mraid://bad-command");

    verify(mockBannerWebView).loadUrl(startsWith("javascript:window.mraidbridge.notifyErrorEvent"));
    assertThat(result).isTrue();
  }
  @Test
  public void handleShouldOverrideUrl_mopubUrl_shouldNeverLoadUrl_shouldReturnTrue() {
    boolean result = subjectBanner.handleShouldOverrideUrl("mopub://special-mopub-command");

    verify(mockBannerWebView, never()).loadUrl(anyString());
    assertThat(result).isTrue();
  }
  @Before
  public void setUp() {
    activity = Robolectric.buildActivity(Activity.class).create().get();

    subjectBanner = new MraidBridge(mockAdReport, PlacementType.INLINE, mockNativeCommandHandler);
    subjectBanner.setMraidBridgeListener(mockBridgeListener);
    subjectBanner.attachView(mockBannerWebView);

    subjectInterstitial =
        new MraidBridge(mockAdReport, PlacementType.INTERSTITIAL, mockNativeCommandHandler);
    subjectInterstitial.setMraidBridgeListener(mockBridgeListener);
    subjectInterstitial.attachView(mockInterstitialWebView);

    verify(mockBannerWebView).setWebViewClient(bannerWebViewClientCaptor.capture());
    reset(mockBannerWebView);
  }
  @Test
  public void runCommand_close_shouldCallListener() throws MraidCommandException {
    Map<String, String> params = new HashMap<String, String>();

    subjectBanner.runCommand(MraidJavascriptCommand.CLOSE, params);

    verify(mockBridgeListener).onClose();
  }
  @Test
  public void bridgeOnVisibilityChanged_withTwoPartBridgeAttached_shouldNotNotifyVisibility() {
    when(mockTwoPartBridge.isAttached()).thenReturn(true);

    bridgeListenerCaptor.getValue().onVisibilityChanged(true);
    bridgeListenerCaptor.getValue().onVisibilityChanged(false);

    verify(mockBridge, never()).notifyViewability(anyBoolean());
    verify(mockTwoPartBridge, never()).notifyViewability(anyBoolean());
  }
  @Test
  public void handlePageLoad_shouldNotifyBridgeOfVisibilityPlacementScreenSizeAndSupports() {
    when(mockBridge.isVisible()).thenReturn(true);

    subject.handlePageLoad();

    verify(mockBridge).notifyViewability(true);
    verify(mockBridge).notifyPlacementType(PlacementType.INLINE);
    verify(mockBridge).notifyScreenMetrics(any(MraidScreenMetrics.class));

    // The actual values here are supplied by the Mraids class, which has separate tests.
    verify(mockBridge).notifySupports(false, false, false, false, false);
  }
  @Test
  public void
      handleClose_fromTwoPartExpandedState_shouldDetachTwoPartBridge_shouldMoveWebViewToOriginalContainer_shouldNotFireOnClose()
          throws MraidCommandException {
    URI uri = URI.create("https://two-part-url");

    // Move to two part EXPANDED state
    subject.handlePageLoad();
    subject.handleExpand(uri, false);
    when(mockTwoPartBridge.isAttached()).thenReturn(true);

    subject.handleClose();

    verify(mockTwoPartBridge).detach();
    assertThat(subject.getExpandedAdContainer().getChildCount()).isEqualTo(1);
    assertThat(subject.getAdContainer().getChildCount()).isEqualTo(1);
    assertThat(subject.getViewState()).isEqualTo(ViewState.DEFAULT);

    verify(mockMraidListener, never()).onClose();
  }
  @Before
  public void setUp() {
    ShadowApplication.setDisplayMetricsDensity(1.0f);

    activity = spy(Robolectric.buildActivity(Activity.class).create().get());
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    rootView = new FrameLayout(activity);
    when(mockBridge.isVisible()).thenReturn(true);

    // By default, immediately fulfill a screen metrics wait request. Individual tests can
    // reset this, if desired.
    when(mockScreenMetricsWaiter.waitFor(Mockito.<View>anyVararg())).thenReturn(mockWaitRequest);
    doAnswer(
            new Answer<Void>() {
              @Override
              public Void answer(final InvocationOnMock invocation) throws Throwable {
                Runnable runnable = (Runnable) invocation.getArguments()[0];
                runnable.run();
                return null;
              }
            })
        .when(mockWaitRequest)
        .start(any(Runnable.class));

    subject =
        new MraidController(
            activity,
            mockAdReport,
            PlacementType.INLINE,
            mockBridge,
            mockTwoPartBridge,
            mockScreenMetricsWaiter);
    subject.setMraidListener(mockMraidListener);
    subject.setOrientationBroadcastReceiver(mockOrientationBroadcastReceiver);
    subject.setRootView(rootView);
    subject.loadContent("fake_html_data");

    verify(mockBridge).setMraidBridgeListener(bridgeListenerCaptor.capture());
    verify(mockTwoPartBridge).setMraidBridgeListener(twoPartBridgeListenerCaptor.capture());
  }
  @Test
  public void attachView_thenSetContentHtml_shouldCallLoadDataWithBaseURL() {
    subjectBanner.setContentHtml("test-html");

    verify(mockBannerWebView).loadDataWithBaseURL(null, "test-html", "text/html", "UTF-8", null);
  }
  @Test
  public void handleShouldOverrideUrl_smsUrl_notClicked_shouldReturnFalse() {
    boolean result = subjectBanner.handleShouldOverrideUrl("sms://123456789");

    assertThat(result).isFalse();
  }
  @Test
  public void handleShouldOverrideUrl_normalUrl_shouldReturnFalse() {
    boolean result = subjectBanner.handleShouldOverrideUrl("http://www.mopub.com");

    assertThat(result).isFalse();
  }