// Copyright 2015 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;

import android.test.suitebuilder.annotation.SmallTest;

import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.ssl.ConnectionSecurityLevel;
import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType;
import org.chromium.chrome.test.ChromeActivityTestCaseBase;
import org.chromium.content.browser.test.util.CallbackHelper;

/**
 * Tests for Tab class.
 */
public class TabTest extends ChromeActivityTestCaseBase<ChromeActivity> {
    private Tab mTab;
    private CallbackHelper mOnTitleUpdatedHelper;

    private final TabObserver mTabObserver = new EmptyTabObserver() {
        @Override
        public void onTitleUpdated(Tab tab) {
            mOnTitleUpdatedHelper.notifyCalled();
        }
    };

    public TabTest() {
        super(ChromeActivity.class);
    }

    @Override
    public void startMainActivity() throws InterruptedException {
        startMainActivityOnBlankPage();
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mTab = getActivity().getActivityTab();
        mTab.addObserver(mTabObserver);
        mOnTitleUpdatedHelper = new CallbackHelper();
    }

    @SmallTest
    @Feature({"Tab"})
    public void testTitleDelayUpdate() throws Throwable {
        final String oldTitle = "oldTitle";
        final String newTitle = "newTitle";

        loadUrl("data:text/html;charset=utf-8,<html><head><title>"
                + oldTitle + "</title></head><body/></html>");
        assertEquals("title does not match initial title", oldTitle, mTab.getTitle());
        int currentCallCount = mOnTitleUpdatedHelper.getCallCount();
        runJavaScriptCodeInCurrentTab("document.title='" + newTitle + "';");
        mOnTitleUpdatedHelper.waitForCallback(currentCallCount);
        assertEquals("title does not update", newTitle, mTab.getTitle());
    }

    @SmallTest
    @Feature({"Tab"})
    public void testTabRestoredIfKilledWhileActivityStopped() {
        // Ensure the tab is showing before stopping the activity.
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                mTab.show(TabSelectionType.FROM_NEW);
            }
        });

        assertFalse(mTab.needsReload());
        assertFalse(mTab.isHidden());
        assertFalse(mTab.isShowingSadTab());

        // Stop the activity and simulate a killed renderer.
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                getInstrumentation().callActivityOnPause(getActivity());
                getInstrumentation().callActivityOnStop(getActivity());
                mTab.simulateRendererKilledForTesting(false);
            }
        });

        assertTrue(mTab.needsReload());
        assertFalse(mTab.isHidden());
        assertFalse(mTab.isShowingSadTab());

        // Resume the activity.
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                getInstrumentation().callActivityOnStart(getActivity());
                getInstrumentation().callActivityOnResume(getActivity());
            }
        });

        // The tab should be restored and visible.
        assertFalse(mTab.isHidden());
        assertFalse(mTab.needsReload());
        assertFalse(mTab.isShowingSadTab());
    }

    @SmallTest
    @Feature({"Tab"})
    public void testTabSecurityLevel() {
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                assertEquals(ConnectionSecurityLevel.NONE, mTab.getSecurityLevel());
            }
        });
    }
}

@@ -291,16 +291,65 @@
 dUtils;%0A
+import org.chromium.base.test.util.DisabledTest;%0A
 import o
@@ -2136,32 +2136,44 @@
 e());%0A    %7D%0A%0A   
+ /**%0A      *
  @SmallTest%0A    
@@ -2163,32 +2163,36 @@
 * @SmallTest%0A   
+   *
  @Feature(%7B%22Tab%22
@@ -2186,32 +2186,96 @@
 eature(%7B%22Tab%22%7D)%0A
+      * Bug: http://crbug.com/508524%0A      */%0A    @DisabledTest%0A
     public void 

Disable testTabRestoredIfKilledWhileActivityStopped