@SmallTest
  public void testInitialIntent() throws URISyntaxException {
    TabRedirectHandler redirectHandler = new TabRedirectHandler(new TestContext());
    Intent ytIntent = Intent.parseUri("http://youtube.com/", Intent.URI_INTENT_SCHEME);
    Intent fooIntent = Intent.parseUri("http://foo.com/", Intent.URI_INTENT_SCHEME);
    int transTypeLinkFromIntent = PageTransition.LINK | PageTransition.FROM_API;

    // Ignore if url is redirected, transition type is IncomingIntent and a new intent doesn't
    // have any new resolver.
    redirectHandler.updateIntent(ytIntent);
    redirectHandler.updateNewUrlLoading(transTypeLinkFromIntent, false, false, 0, 0);
    redirectHandler.updateNewUrlLoading(transTypeLinkFromIntent, true, false, 0, 0);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        transTypeLinkFromIntent,
        REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);
    // Do not ignore if a new intent has any new resolver.
    redirectHandler.updateIntent(fooIntent);
    redirectHandler.updateNewUrlLoading(transTypeLinkFromIntent, false, false, 0, 0);
    redirectHandler.updateNewUrlLoading(transTypeLinkFromIntent, true, false, 0, 0);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        transTypeLinkFromIntent,
        REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT,
        START_ACTIVITY);
  }
  @SmallTest
  public void testIgnoreEffectiveRedirectFromIntentFallbackUrl() {
    // We cannot resolve any intent, so fall-back URL will be used.
    mDelegate.setCanResolveActivity(false);

    TabRedirectHandler redirectHandler = new TabRedirectHandler(null);

    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, true, 0, 0);
    check(
        INTENT_URL_WITH_CHAIN_FALLBACK_URL,
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.OVERRIDE_WITH_CLOBBERING_TAB,
        IGNORE);

    // As a result of intent resolution fallback, we have clobberred the current tab.
    // The fall-back URL was HTTP-schemed, but it was effectively redirected to a new intent
    // URL using javascript. However, we do not allow chained fallback intent, so we do NOT
    // override URL loading here.
    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, false, 0, 0);
    check(
        INTENT_URL_WITH_FALLBACK_URL,
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);

    // Now enough time (2 seconds) have passed.
    // New URL loading should not be affected.
    // (The URL happened to be the same as previous one.)
    // TODO(changwan): this is not likely cause flakiness, but it may be better to refactor
    // systemclock or pass the new time as parameter.
    long lastUserInteractionTimeInMillis = SystemClock.elapsedRealtime() + 2 * 1000L;
    redirectHandler.updateNewUrlLoading(
        PageTransition.LINK, false, true, lastUserInteractionTimeInMillis, 1);
    check(
        INTENT_URL_WITH_FALLBACK_URL,
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.OVERRIDE_WITH_CLOBBERING_TAB,
        IGNORE);
  }
  @SmallTest
  public void testFallback_UseFallbackUrlForRedirectionFromTypedInUrl() {
    TabRedirectHandler redirectHandler = new TabRedirectHandler(null);

    redirectHandler.updateNewUrlLoading(PageTransition.TYPED, false, false, 0, 0);
    check(
        "http://goo.gl/abcdefg",
        null, /* referrer */
        false, /* incognito */
        PageTransition.TYPED,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);

    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, false, 0, 0);
    check(
        INTENT_URL_WITH_FALLBACK_URL_WITHOUT_PACKAGE_NAME,
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.OVERRIDE_WITH_CLOBBERING_TAB,
        IGNORE);

    // Now the user opens a link.
    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, true, 0, 1);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);
  }
  @SmallTest
  public void testNavigationFromLinkWithoutUserGesture() {
    TabRedirectHandler redirectHandler = new TabRedirectHandler(null);

    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, false, 1, 0);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);

    redirectHandler.updateNewUrlLoading(PageTransition.LINK, true, false, 1, 0);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);

    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, false, 1, 1);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);
  }
  @SmallTest
  public void testIgnoreEffectiveRedirectFromUserTyping() {
    TabRedirectHandler redirectHandler = new TabRedirectHandler(null);

    redirectHandler.updateNewUrlLoading(PageTransition.TYPED, false, false, 0, 0);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.TYPED,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);

    redirectHandler.updateNewUrlLoading(PageTransition.TYPED, true, false, 0, 0);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.TYPED,
        REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);

    redirectHandler.updateNewUrlLoading(PageTransition.LINK, false, false, 0, 1);
    check(
        "http://m.youtube.com/",
        null, /* referrer */
        false, /* incognito */
        PageTransition.LINK,
        NO_REDIRECT,
        true,
        false,
        redirectHandler,
        OverrideUrlLoadingResult.NO_OVERRIDE,
        IGNORE);
  }