This source file includes following definitions.
- SimulateRendererCrash
- request_count_
- HandleRequest
- GetActiveWebContents
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/files/file_path.h"
#include "base/strings/stringprintf.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/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_transition_types.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::NavigationController;
using content::OpenURLParams;
using content::Referrer;
using content::WebContents;
#if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
namespace {
void SimulateRendererCrash(Browser* browser) {
content::WindowedNotificationObserver observer(
content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
content::NotificationService::AllSources());
browser->OpenURL(OpenURLParams(
GURL(content::kChromeUICrashURL), Referrer(), CURRENT_TAB,
content::PAGE_TRANSITION_TYPED, false));
observer.Wait();
}
class CacheMaxAgeHandler {
public:
explicit CacheMaxAgeHandler(const std::string& path)
: path_(path), request_count_(0) { }
scoped_ptr<net::test_server::HttpResponse> HandleRequest(
const net::test_server::HttpRequest& request) {
if (request.relative_url != path_)
return scoped_ptr<net::test_server::HttpResponse>();
request_count_++;
scoped_ptr<net::test_server::BasicHttpResponse> response(
new net::test_server::BasicHttpResponse);
response->set_content(base::StringPrintf("<title>%d</title>",
request_count_));
response->set_content_type("text/html");
response->AddCustomHeader("Cache-Control", "max-age=99999");
return response.PassAs<net::test_server::HttpResponse>();
}
private:
std::string path_;
int request_count_;
};
}
class CrashRecoveryBrowserTest : public InProcessBrowserTest {
protected:
WebContents* GetActiveWebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
};
IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, Reload) {
GURL url(
"data:text/html,<script>document.title=new Date().valueOf()</script>");
ui_test_utils::NavigateToURL(browser(), url);
base::string16 title_before_crash;
base::string16 title_after_crash;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
&title_before_crash));
SimulateRendererCrash(browser());
chrome::Reload(browser(), CURRENT_TAB);
content::WaitForLoadStop(GetActiveWebContents());
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
&title_after_crash));
EXPECT_NE(title_before_crash, title_after_crash);
}
IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, ReloadCacheRevalidate) {
const char kTestPath[] = "/test";
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
embedded_test_server()->RegisterRequestHandler(
base::Bind(&CacheMaxAgeHandler::HandleRequest,
base::Owned(new CacheMaxAgeHandler(kTestPath))));
ui_test_utils::NavigateToURL(browser(),
embedded_test_server()->GetURL(kTestPath));
base::string16 title_before_crash;
base::string16 title_after_crash;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
&title_before_crash));
SimulateRendererCrash(browser());
chrome::Reload(browser(), CURRENT_TAB);
content::WaitForLoadStop(GetActiveWebContents());
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
&title_after_crash));
EXPECT_NE(title_before_crash, title_after_crash);
}
IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, LoadInNewTab) {
const base::FilePath::CharType* kTitle2File =
FILE_PATH_LITERAL("title2.html");
ui_test_utils::NavigateToURL(
browser(), ui_test_utils::GetTestUrl(
base::FilePath(base::FilePath::kCurrentDirectory),
base::FilePath(kTitle2File)));
base::string16 title_before_crash;
base::string16 title_after_crash;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
&title_before_crash));
SimulateRendererCrash(browser());
chrome::Reload(browser(), CURRENT_TAB);
content::WaitForLoadStop(GetActiveWebContents());
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
&title_after_crash));
EXPECT_EQ(title_before_crash, title_after_crash);
}
IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, DoubleReloadWithError) {
GURL url("chrome://bogus");
ui_test_utils::NavigateToURL(browser(), url);
ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
SimulateRendererCrash(browser());
chrome::Reload(browser(), CURRENT_TAB);
content::WaitForLoadStop(GetActiveWebContents());
ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
chrome::Reload(browser(), CURRENT_TAB);
content::WaitForLoadStop(GetActiveWebContents());
ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
}
#endif