// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.translate;

import android.test.suitebuilder.annotation.MediumTest;


import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.infobar.InfoBar;
import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.test.util.InfoBarTestAnimationListener;
import org.chromium.chrome.test.util.InfoBarUtil;
import org.chromium.chrome.test.util.TranslateUtil;
import org.chromium.chrome.test.util.TestHttpServerClient;
import org.chromium.chrome.testshell.ChromiumTestShellTestBase;

import java.util.List;

/**
 * Tests for the translate infobar, assumes it runs on a system with language
 * preferences set to English.
 */
public class TranslateInfoBarTest extends ChromiumTestShellTestBase {
    private static final String TRANSLATE_PAGE = "chrome/test/data/translate/fr_test.html";

    private InfoBarTestAnimationListener mListener;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        InfoBarContainer container = getActivity().getActiveTab().getInfoBarContainer();
        mListener =  new InfoBarTestAnimationListener();
        container.setAnimationListener(mListener);
    }

    private static final String NEVER_TRANSLATE_MESSAGE =
        "Would you like Google Chrome to offer to translate French pages from this" +
        " site next time?";

    /**
     * Test the translate language panel.
     */
    @MediumTest
    @Feature({"Browser", "Main"})
    public void testTranslateLanguagePanel() throws InterruptedException {
        List<InfoBar> infoBars = getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
        loadUrlWithSanitization(TestHttpServerClient.getUrl(TRANSLATE_PAGE));
        assertTrue("InfoBar not opened.", mListener.addInfoBarAnimationFinished());
        InfoBar infoBar = infoBars.get(0);
        assertTrue(InfoBarUtil.hasPrimaryButton(this, infoBar));
        assertTrue(InfoBarUtil.hasSecondaryButton(this, infoBar));
        assertTrue("Language Panel not opened.", TranslateUtil.openLanguagePanel(this, infoBar));
    }


    /**
     *  Test the "never translate" panel.
     */
    @MediumTest
    @Feature({"Browser", "Main"})
    public void testTranslateNeverPanel() throws InterruptedException {
        List<InfoBar> infoBars = getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
        loadUrlWithSanitization(TestHttpServerClient.getUrl(TRANSLATE_PAGE));
        assertTrue("InfoBar not opened.", mListener.addInfoBarAnimationFinished());
        InfoBar infoBar = infoBars.get(0);

        assertTrue(InfoBarUtil.clickCloseButton(this, infoBar));
        assertTrue(mListener.removeInfoBarAnimationFinished());

        // Reload the page so the infobar shows again
        loadUrlWithSanitization(TestHttpServerClient.getUrl(TRANSLATE_PAGE));
        assertTrue("InfoBar not opened", mListener.addInfoBarAnimationFinished());
        infoBar = infoBars.get(0);
        assertTrue(InfoBarUtil.clickCloseButton(this, infoBar));

       assertTrue("Never Panel not opened.",
           TranslateUtil.verifyInfoBarText(infoBar, NEVER_TRANSLATE_MESSAGE));
    }
}

@@ -203,24 +203,55 @@
 translate;%0A%0A
+import android.test.FlakyTest;%0A
 import andro
@@ -295,17 +295,16 @@
 mTest;%0A%0A
-%0A
 import o
@@ -1610,37 +1610,32 @@
 ge panel.%0A     *
-/%0A   
  @MediumTest%0A   
@@ -1626,32 +1626,35 @@
  @MediumTest%0A   
+  *
  @Feature(%7B%22Brow
@@ -1660,32 +1660,86 @@
 wser%22, %22Main%22%7D)%0A
+     * http://crbug.com/311197%0A     */%0A    @FlakyTest%0A
     public void 
@@ -2353,17 +2353,16 @@
 *%0A     *
- 
  Test th
@@ -2394,21 +2394,16 @@
 .%0A     *
-/%0A   
  @Medium
@@ -2410,16 +2410,19 @@
 Test%0A   
+  *
  @Featur
@@ -2440,24 +2440,78 @@
 %22, %22Main%22%7D)%0A
+     * http://crbug.com/311197%0A     */%0A    @FlakyTest%0A
     public v

Mark tests in TranslateInfoBarTest as flaky.