This source file includes following definitions.
- RunOnDBThread
- DoneRunOnMainThread
- SetUpCommandLine
- GetPrefs
- GetProfile
- GetHistoryContents
- GetTestUrl
- WaitForHistoryBackendToRun
- ExpectEmptyHistory
- LoadAndWaitForURL
- LoadAndWaitForFile
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include <vector>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/history/history_db_task.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_browser_thread.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace {
class WaitForHistoryTask : public history::HistoryDBTask {
public:
WaitForHistoryTask() {}
virtual bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) OVERRIDE {
return true;
}
virtual void DoneRunOnMainThread() OVERRIDE {
base::MessageLoop::current()->Quit();
}
private:
virtual ~WaitForHistoryTask() {}
DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask);
};
}
class HistoryBrowserTest : public InProcessBrowserTest {
protected:
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kEnableFileCookies);
}
PrefService* GetPrefs() {
return GetProfile()->GetPrefs();
}
Profile* GetProfile() {
return browser()->profile();
}
std::vector<GURL> GetHistoryContents() {
ui_test_utils::HistoryEnumerator enumerator(GetProfile());
return enumerator.urls();
}
GURL GetTestUrl() {
return ui_test_utils::GetTestUrl(
base::FilePath(base::FilePath::kCurrentDirectory),
base::FilePath(FILE_PATH_LITERAL("title2.html")));
}
void WaitForHistoryBackendToRun() {
CancelableRequestConsumerTSimple<int> request_consumer;
scoped_refptr<history::HistoryDBTask> task(new WaitForHistoryTask());
HistoryService* history =
HistoryServiceFactory::GetForProfile(GetProfile(),
Profile::EXPLICIT_ACCESS);
history->HistoryService::ScheduleDBTask(task.get(), &request_consumer);
content::RunMessageLoop();
}
void ExpectEmptyHistory() {
std::vector<GURL> urls(GetHistoryContents());
EXPECT_EQ(0U, urls.size());
}
void LoadAndWaitForURL(const GURL& url) {
base::string16 expected_title(base::ASCIIToUTF16("OK"));
content::TitleWatcher title_watcher(
browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
ui_test_utils::NavigateToURL(browser(), url);
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
}
void LoadAndWaitForFile(const char* filename) {
GURL url = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("History"),
base::FilePath().AppendASCII(filename));
LoadAndWaitForURL(url);
}
};
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryEnabled) {
EXPECT_FALSE(GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled));
EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
GetProfile(), Profile::EXPLICIT_ACCESS));
EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
GetProfile(), Profile::IMPLICIT_ACCESS));
ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
browser()->profile(), Profile::EXPLICIT_ACCESS));
ExpectEmptyHistory();
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
WaitForHistoryBackendToRun();
{
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1U, urls.size());
EXPECT_EQ(GetTestUrl().spec(), urls[0].spec());
}
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryDisabled) {
GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true);
EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
GetProfile(), Profile::EXPLICIT_ACCESS));
EXPECT_FALSE(HistoryServiceFactory::GetForProfile(
GetProfile(), Profile::IMPLICIT_ACCESS));
ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
browser()->profile(), Profile::EXPLICIT_ACCESS));
ExpectEmptyHistory();
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
WaitForHistoryBackendToRun();
ExpectEmptyHistory();
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryEnabledThenDisabled) {
EXPECT_FALSE(GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled));
ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
browser()->profile(), Profile::EXPLICIT_ACCESS));
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
WaitForHistoryBackendToRun();
{
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1U, urls.size());
EXPECT_EQ(GetTestUrl().spec(), urls[0].spec());
}
GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true);
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
WaitForHistoryBackendToRun();
{
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1U, urls.size());
EXPECT_EQ(GetTestUrl().spec(), urls[0].spec());
}
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryDisabledThenEnabled) {
GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true);
ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
browser()->profile(), Profile::EXPLICIT_ACCESS));
ExpectEmptyHistory();
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
WaitForHistoryBackendToRun();
ExpectEmptyHistory();
GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, false);
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
WaitForHistoryBackendToRun();
{
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1U, urls.size());
EXPECT_EQ(GetTestUrl().spec(), urls[0].spec());
}
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, VerifyHistoryLength1) {
LoadAndWaitForFile("history_length_test_page_1.html");
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, VerifyHistoryLength2) {
LoadAndWaitForFile("history_length_test_page_2.html");
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, VerifyHistoryLength3) {
LoadAndWaitForFile("history_length_test_page_1.html");
LoadAndWaitForFile("history_length_test_page_2.html");
LoadAndWaitForFile("history_length_test_page_4.html");
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest,
ConsiderRedirectAfterGestureAsUserInitiated) {
LoadAndWaitForFile("history_length_test_page_11.html");
content::SimulateMouseClick(
browser()->tab_strip_model()->GetActiveWebContents(), 0,
blink::WebMouseEvent::ButtonLeft);
LoadAndWaitForFile("history_length_test_page_11.html");
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest,
ConsiderSlowRedirectAsUserInitiated) {
LoadAndWaitForFile("history_length_test_page_21.html");
}
#if defined(OS_LINUX)
#define MAYBE_HistorySearchXSS DISABLED_HistorySearchXSS
#else
#define MAYBE_HistorySearchXSS HistorySearchXSS
#endif
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, MAYBE_HistorySearchXSS) {
GURL url(std::string(chrome::kChromeUIHistoryURL) +
"#q=%3Cimg%20src%3Dx%3Ax%20onerror%3D%22document.title%3D'XSS'%22%3E");
ui_test_utils::NavigateToURL(browser(), url);
int num = ui_test_utils::FindInPage(
browser()->tab_strip_model()->GetActiveWebContents(),
base::ASCIIToUTF16("<img"), true,
true, NULL, NULL);
EXPECT_GT(num, 0);
EXPECT_EQ(base::ASCIIToUTF16("History"),
browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, PRE_HistoryPersists) {
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1u, urls.size());
ASSERT_EQ(GetTestUrl(), urls[0]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, HistoryPersists) {
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1u, urls.size());
ASSERT_EQ(GetTestUrl(), urls[0]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, InvalidURLNoHistory) {
GURL non_existant = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("History"),
base::FilePath().AppendASCII("non_existant_file.html"));
ui_test_utils::NavigateToURL(browser(), non_existant);
ExpectEmptyHistory();
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, NewTabNoHistory) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
ExpectEmptyHistory();
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, IncognitoNoHistory) {
ui_test_utils::NavigateToURL(CreateIncognitoBrowser(), GetTestUrl());
ExpectEmptyHistory();
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, NavigateMultiTimes) {
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
ui_test_utils::NavigateToURL(browser(), GetTestUrl());
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(1u, urls.size());
ASSERT_EQ(GetTestUrl(), urls[0]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, MultiTabsWindowsHistory) {
GURL url1 = GetTestUrl();
GURL url2 = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
GURL url3 = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title3.html")));
GURL url4 = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("simple.html")));
ui_test_utils::NavigateToURL(browser(), url1);
Browser* browser2 = CreateBrowser(browser()->profile());
ui_test_utils::NavigateToURL(browser2, url2);
ui_test_utils::NavigateToURLWithDisposition(
browser2, url3, NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
ui_test_utils::NavigateToURLWithDisposition(
browser2, url4, NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(4u, urls.size());
ASSERT_EQ(url4, urls[0]);
ASSERT_EQ(url3, urls[1]);
ASSERT_EQ(url2, urls[2]);
ASSERT_EQ(url1, urls[3]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, DownloadNoHistory) {
GURL download_url = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("downloads"),
base::FilePath().AppendASCII("a_zip_file.zip"));
ui_test_utils::DownloadURL(browser(), download_url);
ExpectEmptyHistory();
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, RedirectHistory) {
GURL redirector = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("History"),
base::FilePath().AppendASCII("redirector.html"));
GURL landing_url = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("History"),
base::FilePath().AppendASCII("landing.html"));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), redirector, 2);
ASSERT_EQ(landing_url,
browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(landing_url, urls[0]);
ASSERT_EQ(redirector, urls[1]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, NavigateBringPageToTop) {
GURL url1 = GetTestUrl();
GURL url2 = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title3.html")));
ui_test_utils::NavigateToURL(browser(), url1);
ui_test_utils::NavigateToURL(browser(), url2);
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(url2, urls[0]);
ASSERT_EQ(url1, urls[1]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, ReloadBringPageToTop) {
GURL url1 = GetTestUrl();
GURL url2 = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title3.html")));
ui_test_utils::NavigateToURL(browser(), url1);
ui_test_utils::NavigateToURLWithDisposition(
browser(), url2, NEW_BACKGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(url2, urls[0]);
ASSERT_EQ(url1, urls[1]);
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
tab->GetController().Reload(false);
content::WaitForLoadStop(tab);
urls = GetHistoryContents();
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(url1, urls[0]);
ASSERT_EQ(url2, urls[1]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, BackForwardBringPageToTop) {
GURL url1 = GetTestUrl();
GURL url2 = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title3.html")));
ui_test_utils::NavigateToURL(browser(), url1);
ui_test_utils::NavigateToURL(browser(), url2);
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
chrome::GoBack(browser(), CURRENT_TAB);
content::WaitForLoadStop(tab);
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(url1, urls[0]);
ASSERT_EQ(url2, urls[1]);
chrome::GoForward(browser(), CURRENT_TAB);
content::WaitForLoadStop(tab);
urls = GetHistoryContents();
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(url2, urls[0]);
ASSERT_EQ(url1, urls[1]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SubmitFormAddsTargetPage) {
GURL form = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("History"),
base::FilePath().AppendASCII("form.html"));
GURL target = ui_test_utils::GetTestUrl(
base::FilePath().AppendASCII("History"),
base::FilePath().AppendASCII("target.html"));
ui_test_utils::NavigateToURL(browser(), form);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
base::string16 expected_title(base::ASCIIToUTF16("Target Page"));
content::TitleWatcher title_watcher(
browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
ASSERT_TRUE(content::ExecuteScript(
web_contents, "document.getElementById('form').submit()"));
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
std::vector<GURL> urls(GetHistoryContents());
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(target, urls[0]);
ASSERT_EQ(form, urls[1]);
}
IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, OneHistoryTabPerWindow) {
GURL history_url(chrome::kChromeUIHistoryURL);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
base::string16 expected_title(base::ASCIIToUTF16("History"));
content::TitleWatcher title_watcher(web_contents, expected_title);
chrome::ExecuteCommand(browser(), IDC_SHOW_HISTORY);
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(content::kAboutBlankURL), NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
chrome::ExecuteCommand(browser(), IDC_SHOW_HISTORY);
content::WebContents* active_web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_EQ(web_contents, active_web_contents);
ASSERT_EQ(history_url, active_web_contents->GetURL());
content::WebContents* second_tab =
browser()->tab_strip_model()->GetWebContentsAt(1);
ASSERT_NE(history_url, second_tab->GetURL());
}