This source file includes following definitions.
- final_state_
- WaitForStates
- final_state
- OnSessionRestoreStateChanged
- SetupGaiaServerForNewAccount
- SetupGaiaServerForUnexpiredAccount
- SetupGaiaServerForExpiredAccount
- LoginAsExistingUser
- TryToLogin
- GetOAuthStatusFromLocalState
- profile
- AddUserToSession
- SetupGaiaServerWithAccessTokens
- CheckSessionState
- WaitForMergeSessionCompletion
- StartNewUserSession
- ReadCookies
- GetCookieValue
- ReadCookiesOnIOThread
- OnGetAllCookiesOnUIThread
- OnCookiesReadyOnUIThread
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- HandleRequest
- IsPageRequested
- WaitForPageRequest
- QuitRunnerOnUIThread
- start_event_
- UnblockMergeSession
- WaitForMergeSessionToStart
- HandleMergeSession
- QuitRunnerOnUIThread
- SetUpCommandLine
- SetUp
- UnblockMergeSession
- WaitForMergeSessionToStart
- JsExpect
- GetBackGroundPageUrl
- JsExpectOnBackgroundPage
- FindOrCreateVisibleBrowser
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/login/oauth2_login_manager.h"
#include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
#include "chrome/browser/chromeos/login/oobe_base_test.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/login/wizard_controller.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
#include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/process_manager.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_monster.h"
#include "net/cookies/cookie_store.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
using net::test_server::BasicHttpResponse;
using net::test_server::HttpRequest;
using net::test_server::HttpResponse;
namespace chromeos {
namespace {
const char kTestAccountId[] = "username@gmail.com";
const char kTestRawAccountId[] = "User.Name";
const char kTestAccountPassword[] = "fake-password";
const char kTestAuthCode[] = "fake-auth-code";
const char kTestGaiaUberToken[] = "fake-uber-token";
const char kTestAuthLoginAccessToken[] = "fake-access-token";
const char kTestRefreshToken[] = "fake-refresh-token";
const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie";
const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie";
const char kTestSessionSIDCookie[] = "fake-session-SID-cookie";
const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie";
const char kTestSession2SIDCookie[] = "fake-session2-SID-cookie";
const char kTestSession2LSIDCookie[] = "fake-session2-LSID-cookie";
const char kTestUserinfoToken[] = "fake-userinfo-token";
const char kTestLoginToken[] = "fake-login-token";
const char kTestSyncToken[] = "fake-sync-token";
const char kTestAuthLoginToken[] = "fake-oauthlogin-token";
class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer {
public:
explicit OAuth2LoginManagerStateWaiter(Profile* profile)
: profile_(profile),
waiting_for_state_(false),
final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) {
}
void WaitForStates(
const std::set<OAuth2LoginManager::SessionRestoreState>& states) {
DCHECK(!waiting_for_state_);
OAuth2LoginManager* login_manager =
OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_);
states_ = states;
if (states_.find(login_manager->state()) != states_.end()) {
final_state_ = login_manager->state();
return;
}
waiting_for_state_ = true;
login_manager->AddObserver(this);
runner_ = new content::MessageLoopRunner;
runner_->Run();
login_manager->RemoveObserver(this);
}
OAuth2LoginManager::SessionRestoreState final_state() { return final_state_; }
private:
virtual void OnSessionRestoreStateChanged(
Profile* user_profile,
OAuth2LoginManager::SessionRestoreState state) OVERRIDE {
if (!waiting_for_state_)
return;
if (states_.find(state) == states_.end())
return;
final_state_ = state;
waiting_for_state_ = false;
runner_->Quit();
}
Profile* profile_;
std::set<OAuth2LoginManager::SessionRestoreState> states_;
bool waiting_for_state_;
OAuth2LoginManager::SessionRestoreState final_state_;
scoped_refptr<content::MessageLoopRunner> runner_;
DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter);
};
}
class OAuth2Test : public OobeBaseTest {
protected:
OAuth2Test() {}
void SetupGaiaServerForNewAccount() {
FakeGaia::MergeSessionParams params;
params.auth_sid_cookie = kTestAuthSIDCookie;
params.auth_lsid_cookie = kTestAuthLSIDCookie;
params.auth_code = kTestAuthCode;
params.refresh_token = kTestRefreshToken;
params.access_token = kTestAuthLoginAccessToken;
params.gaia_uber_token = kTestGaiaUberToken;
params.session_sid_cookie = kTestSessionSIDCookie;
params.session_lsid_cookie = kTestSessionLSIDCookie;
fake_gaia_->SetMergeSessionParams(params);
SetupGaiaServerWithAccessTokens();
}
void SetupGaiaServerForUnexpiredAccount() {
FakeGaia::MergeSessionParams params;
params.email = kTestAccountId;
fake_gaia_->SetMergeSessionParams(params);
SetupGaiaServerWithAccessTokens();
}
void SetupGaiaServerForExpiredAccount() {
FakeGaia::MergeSessionParams params;
params.gaia_uber_token = kTestGaiaUberToken;
params.session_sid_cookie = kTestSession2SIDCookie;
params.session_lsid_cookie = kTestSession2LSIDCookie;
fake_gaia_->SetMergeSessionParams(params);
SetupGaiaServerWithAccessTokens();
}
void LoginAsExistingUser() {
content::WindowedNotificationObserver(
chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
content::NotificationService::AllSources()).Wait();
JsExpect("!!document.querySelector('#account-picker')");
JsExpect("!!document.querySelector('#pod-row')");
EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
User::OAUTH2_TOKEN_STATUS_VALID);
EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword));
Profile* profile = ProfileManager::GetPrimaryUserProfile();
WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_DONE);
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId));
EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
User::OAUTH2_TOKEN_STATUS_VALID);
}
bool TryToLogin(const std::string& username,
const std::string& password) {
if (!AddUserToSession(username, password))
return false;
if (const User* active_user = UserManager::Get()->GetActiveUser())
return active_user->email() == username;
return false;
}
User::OAuthTokenStatus GetOAuthStatusFromLocalState(
const std::string& user_id) const {
PrefService* local_state = g_browser_process->local_state();
const base::DictionaryValue* prefs_oauth_status =
local_state->GetDictionary("OAuthTokenStatus");
int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN;
if (prefs_oauth_status &&
prefs_oauth_status->GetIntegerWithoutPathExpansion(
user_id, &oauth_token_status)) {
User::OAuthTokenStatus result =
static_cast<User::OAuthTokenStatus>(oauth_token_status);
return result;
}
return User::OAUTH_TOKEN_STATUS_UNKNOWN;
}
protected:
virtual Profile* profile() OVERRIDE {
if (UserManager::Get()->GetActiveUser())
return ProfileManager::GetPrimaryUserProfile();
return OobeBaseTest::profile();
}
bool AddUserToSession(const std::string& username,
const std::string& password) {
ExistingUserController* controller =
ExistingUserController::current_controller();
if (!controller) {
ADD_FAILURE();
return false;
}
controller->Login(UserContext(username, password, std::string()));
content::WindowedNotificationObserver(
chrome::NOTIFICATION_SESSION_STARTED,
content::NotificationService::AllSources()).Wait();
const UserList& logged_users = UserManager::Get()->GetLoggedInUsers();
for (UserList::const_iterator it = logged_users.begin();
it != logged_users.end(); ++it) {
if ((*it)->email() == username)
return true;
}
return false;
}
void SetupGaiaServerWithAccessTokens() {
GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
FakeGaia::AccessTokenInfo userinfo_token_info;
userinfo_token_info.token = kTestUserinfoToken;
userinfo_token_info.scopes.insert(
"https://www.googleapis.com/auth/userinfo.email");
userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id();
userinfo_token_info.email = kTestAccountId;
fake_gaia_->IssueOAuthToken(kTestRefreshToken, userinfo_token_info);
FakeGaia::AccessTokenInfo userinfo_profile_token_info;
userinfo_profile_token_info.token = kTestUserinfoToken;
userinfo_profile_token_info.scopes.insert(
"https://www.googleapis.com/auth/userinfo.profile");
userinfo_profile_token_info.audience = gaia_urls->oauth2_chrome_client_id();
userinfo_profile_token_info.email = kTestAccountId;
fake_gaia_->IssueOAuthToken(kTestRefreshToken, userinfo_profile_token_info);
FakeGaia::AccessTokenInfo login_token_info;
login_token_info.token = kTestLoginToken;
login_token_info.scopes.insert(GaiaConstants::kAnyApiOAuth2Scope);
login_token_info.audience = gaia_urls->oauth2_chrome_client_id();
fake_gaia_->IssueOAuthToken(kTestRefreshToken, login_token_info);
FakeGaia::AccessTokenInfo sync_token_info;
sync_token_info.token = kTestSyncToken;
sync_token_info.scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
sync_token_info.audience = gaia_urls->oauth2_chrome_client_id();
fake_gaia_->IssueOAuthToken(kTestRefreshToken, sync_token_info);
FakeGaia::AccessTokenInfo auth_login_token_info;
auth_login_token_info.token = kTestAuthLoginToken;
auth_login_token_info.scopes.insert(GaiaConstants::kOAuth1LoginScope);
auth_login_token_info.audience = gaia_urls->oauth2_chrome_client_id();
fake_gaia_->IssueOAuthToken(kTestRefreshToken, auth_login_token_info);
}
void CheckSessionState(OAuth2LoginManager::SessionRestoreState state) {
OAuth2LoginManager* login_manager =
OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
profile());
ASSERT_EQ(state, login_manager->state());
}
void WaitForMergeSessionCompletion(
OAuth2LoginManager::SessionRestoreState final_state) {
std::set<OAuth2LoginManager::SessionRestoreState> states;
states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE);
states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED);
states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED);
OAuth2LoginManagerStateWaiter merge_session_waiter(profile());
merge_session_waiter.WaitForStates(states);
EXPECT_EQ(merge_session_waiter.final_state(), final_state);
}
void StartNewUserSession(bool wait_for_merge) {
SetupGaiaServerForNewAccount();
SimulateNetworkOnline();
chromeos::WizardController::SkipPostLoginScreensForTesting();
chromeos::WizardController* wizard_controller =
chromeos::WizardController::default_controller();
wizard_controller->SkipToLoginForTesting(LoginScreenContext());
content::WindowedNotificationObserver(
chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
content::NotificationService::AllSources()).Wait();
GetLoginDisplay()->ShowSigninScreenForCreds(kTestRawAccountId,
kTestAccountPassword);
content::WindowedNotificationObserver(
chrome::NOTIFICATION_SESSION_STARTED,
content::NotificationService::AllSources()).Wait();
if (wait_for_merge) {
WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_DONE);
}
}
DISALLOW_COPY_AND_ASSIGN(OAuth2Test);
};
class CookieReader : public base::RefCountedThreadSafe<CookieReader> {
public:
CookieReader() {
}
void ReadCookies(Profile* profile) {
context_ = profile->GetRequestContext();
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&CookieReader::ReadCookiesOnIOThread,
this));
runner_ = new content::MessageLoopRunner;
runner_->Run();
}
std::string GetCookieValue(const std::string& name) {
for (std::vector<net::CanonicalCookie>::const_iterator iter =
cookie_list_.begin();
iter != cookie_list_.end();
++iter) {
if (iter->Name() == name) {
return iter->Value();
}
}
return std::string();
}
private:
friend class base::RefCountedThreadSafe<CookieReader>;
virtual ~CookieReader() {
}
void ReadCookiesOnIOThread() {
context_->GetURLRequestContext()->cookie_store()->GetCookieMonster()->
GetAllCookiesAsync(base::Bind(
&CookieReader::OnGetAllCookiesOnUIThread,
this));
}
void OnGetAllCookiesOnUIThread(const net::CookieList& cookies) {
cookie_list_ = cookies;
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&CookieReader::OnCookiesReadyOnUIThread,
this));
}
void OnCookiesReadyOnUIThread() {
runner_->Quit();
}
scoped_refptr<net::URLRequestContextGetter> context_;
net::CookieList cookie_list_;
scoped_refptr<content::MessageLoopRunner> runner_;
DISALLOW_COPY_AND_ASSIGN(CookieReader);
};
IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_PRE_MergeSession) {
StartNewUserSession(true);
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(
profile());
EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId));
EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
User::OAUTH2_TOKEN_STATUS_VALID);
scoped_refptr<CookieReader> cookie_reader(new CookieReader());
cookie_reader->ReadCookies(profile());
EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie);
EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie);
}
IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_MergeSession) {
SetupGaiaServerForUnexpiredAccount();
SimulateNetworkOnline();
LoginAsExistingUser();
scoped_refptr<CookieReader> cookie_reader(new CookieReader());
cookie_reader->ReadCookies(profile());
EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie);
EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie);
}
IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_MergeSession) {
SetupGaiaServerForExpiredAccount();
SimulateNetworkOnline();
LoginAsExistingUser();
scoped_refptr<CookieReader> cookie_reader(new CookieReader());
cookie_reader->ReadCookies(profile());
EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSession2SIDCookie);
EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSession2LSIDCookie);
}
IN_PROC_BROWSER_TEST_F(OAuth2Test, MergeSession) {
SimulateNetworkOnline();
content::WindowedNotificationObserver(
chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
content::NotificationService::AllSources()).Wait();
JsExpect("!!document.querySelector('#account-picker')");
JsExpect("!!document.querySelector('#pod-row')");
EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
User::OAUTH2_TOKEN_STATUS_VALID);
EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword));
WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_FAILED);
EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
User::OAUTH2_TOKEN_STATUS_INVALID);
}
const char kGooglePageContent[] =
"<html><title>Hello!</title><script>alert('hello');</script>"
"<body>Hello Google!</body></html>";
const char kRandomPageContent[] =
"<html><title>SomthingElse</title><body>I am SomethingElse</body></html>";
const char kHelloPagePath[] = "/hello_google";
const char kRandomPagePath[] = "/non_google_page";
class FakeGoogle {
public:
FakeGoogle() : start_event_(true, false) {
}
~FakeGoogle() {}
scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
GURL request_url = GURL("http://localhost").Resolve(request.relative_url);
LOG(WARNING) << "Requesting page " << request.relative_url;
std::string request_path = request_url.path();
scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
if (request_path == kHelloPagePath) {
start_event_.Signal();
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&FakeGoogle::QuitRunnerOnUIThread,
base::Unretained(this)));
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("text/html");
http_response->set_content(kGooglePageContent);
} else if (request_path == kRandomPagePath) {
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("text/html");
http_response->set_content(kRandomPageContent);
} else {
return scoped_ptr<HttpResponse>();
}
return http_response.PassAs<HttpResponse>();
}
bool IsPageRequested () {
return start_event_.IsSignaled();
}
void WaitForPageRequest() {
if (start_event_.IsSignaled())
return;
runner_ = new content::MessageLoopRunner;
runner_->Run();
}
private:
void QuitRunnerOnUIThread() {
if (runner_.get())
runner_->Quit();
}
base::WaitableEvent start_event_;
scoped_refptr<content::MessageLoopRunner> runner_;
DISALLOW_COPY_AND_ASSIGN(FakeGoogle);
};
class DelayedFakeGaia : public FakeGaia {
public:
DelayedFakeGaia()
: blocking_event_(true, false),
start_event_(true, false) {
}
void UnblockMergeSession() {
blocking_event_.Signal();
}
void WaitForMergeSessionToStart() {
if (start_event_.IsSignaled())
return;
runner_ = new content::MessageLoopRunner;
runner_->Run();
}
private:
virtual void HandleMergeSession(const HttpRequest& request,
BasicHttpResponse* http_response) OVERRIDE {
start_event_.Signal();
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&DelayedFakeGaia::QuitRunnerOnUIThread,
base::Unretained(this)));
blocking_event_.Wait();
FakeGaia::HandleMergeSession(request, http_response);
}
void QuitRunnerOnUIThread() {
if (runner_.get())
runner_->Quit();
}
base::WaitableEvent blocking_event_;
base::WaitableEvent start_event_;
scoped_refptr<content::MessageLoopRunner> runner_;
DISALLOW_COPY_AND_ASSIGN(DelayedFakeGaia);
};
class MergeSessionTest : public OAuth2Test {
protected:
MergeSessionTest() : delayed_fake_gaia_(new DelayedFakeGaia()) {
fake_gaia_.reset(delayed_fake_gaia_);
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
OAuth2Test::SetUpCommandLine(command_line);
const GURL& server_url = embedded_test_server()->base_url();
std::string google_host("www.google.com");
GURL::Replacements replace_google_host;
replace_google_host.SetHostStr(google_host);
GURL google_url = server_url.ReplaceComponents(replace_google_host);
fake_google_page_url_ = google_url.Resolve(kHelloPagePath);
std::string non_google_host("www.somethingelse.org");
GURL::Replacements replace_non_google_host;
replace_non_google_host.SetHostStr(non_google_host);
GURL non_google_url = server_url.ReplaceComponents(replace_non_google_host);
non_google_page_url_ = non_google_url.Resolve(kRandomPagePath);
}
virtual void SetUp() OVERRIDE {
embedded_test_server()->RegisterRequestHandler(
base::Bind(&FakeGoogle::HandleRequest,
base::Unretained(&fake_google_)));
OAuth2Test::SetUp();
}
protected:
void UnblockMergeSession() {
delayed_fake_gaia_->UnblockMergeSession();
}
void WaitForMergeSessionToStart() {
delayed_fake_gaia_->WaitForMergeSessionToStart();
}
void JsExpect(content::WebContents* contents,
const std::string& expression) {
bool result;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
contents,
"window.domAutomationController.send(!!(" + expression + "));",
&result));
ASSERT_TRUE(result) << expression;
}
const GURL& GetBackGroundPageUrl(const std::string& extension_id) {
extensions::ProcessManager* manager =
extensions::ExtensionSystem::Get(profile())->process_manager();
extensions::ExtensionHost* host =
manager->GetBackgroundHostForExtension(extension_id);
return host->host_contents()->GetURL();
}
void JsExpectOnBackgroundPage(const std::string& extension_id,
const std::string& expression) {
extensions::ProcessManager* manager =
extensions::ExtensionSystem::Get(profile())->process_manager();
extensions::ExtensionHost* host =
manager->GetBackgroundHostForExtension(extension_id);
if (host == NULL) {
ADD_FAILURE() << "Extension " << extension_id
<< " has no background page.";
return;
}
JsExpect(host->host_contents(), expression);
}
FakeGoogle fake_google_;
DelayedFakeGaia* delayed_fake_gaia_;
GURL fake_google_page_url_;
GURL non_google_page_url_;
private:
DISALLOW_COPY_AND_ASSIGN(MergeSessionTest);
};
Browser* FindOrCreateVisibleBrowser(Profile* profile) {
chrome::ScopedTabbedBrowserDisplayer displayer(
profile, chrome::GetActiveDesktop());
Browser* browser = displayer.browser();
if (browser->tab_strip_model()->count() == 0)
chrome::AddTabAt(browser, GURL(), -1, true);
return browser;
}
IN_PROC_BROWSER_TEST_F(MergeSessionTest, PageThrottle) {
StartNewUserSession(false);
Browser* browser =
FindOrCreateVisibleBrowser(profile());
ui_test_utils::NavigateToURLWithDisposition(
browser,
fake_google_page_url_,
CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE);
WaitForMergeSessionToStart();
EXPECT_FALSE(fake_google_.IsPageRequested());
base::string16 title;
ui_test_utils::GetCurrentTabTitle(browser, &title);
DVLOG(1) << "Loaded page at the start : " << title;
UnblockMergeSession();
WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_DONE);
fake_google_.WaitForPageRequest();
AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
JavaScriptAppModalDialog* js_dialog =
static_cast<JavaScriptAppModalDialog*>(dialog);
js_dialog->native_dialog()->AcceptAppModalDialog();
ui_test_utils::GetCurrentTabTitle(browser, &title);
DVLOG(1) << "Loaded page at the end : " << title;
}
IN_PROC_BROWSER_TEST_F(MergeSessionTest, XHRThrottle) {
StartNewUserSession(false);
WaitForMergeSessionToStart();
Browser* browser = FindOrCreateVisibleBrowser(profile());
observer_.reset(new ExtensionTestNotificationObserver(browser));
ResultCatcher catcher;
scoped_ptr<ExtensionTestMessageListener> non_google_xhr_listener(
new ExtensionTestMessageListener("non-google-xhr-received", false));
const extensions::Extension* ext = LoadExtension(
test_data_dir_.AppendASCII("merge_session"));
ASSERT_TRUE(ext);
JsExpectOnBackgroundPage(
ext->id(),
base::StringPrintf("startThrottledTests('%s', '%s')",
fake_google_page_url_.spec().c_str(),
non_google_page_url_.spec().c_str()));
JsExpectOnBackgroundPage(ext->id(),
"googleRequestSent && !googleResponseReceived");
EXPECT_FALSE(fake_google_.IsPageRequested());
UnblockMergeSession();
WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_DONE);
ASSERT_TRUE(non_google_xhr_listener->WaitUntilSatisfied());
if (!catcher.GetNextResult()) {
std::string message = catcher.message();
ADD_FAILURE() << "Tests failed: " << message;
}
EXPECT_TRUE(fake_google_.IsPageRequested());
}
}