This source file includes following definitions.
- infobar_service_
- SetPathToWaitFor
- Observe
- DidFinishLoad
- infobar_shown
- infobar_removed
- disable_should_automatically_accept_infobar
- Wait
- SetUpOnMainThread
- NavigateToFile
- CheckScriptReturnValue
- 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 <string>
#include "base/command_line.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/infobars/confirm_infobar_delegate.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_manager.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/password_manager/test_password_store_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace {
class NavigationObserver : public content::NotificationObserver,
public content::WebContentsObserver {
public:
explicit NavigationObserver(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
message_loop_runner_(new content::MessageLoopRunner),
infobar_shown_(false),
infobar_removed_(false),
should_automatically_accept_infobar_(true),
infobar_service_(InfoBarService::FromWebContents(web_contents)) {
registrar_.Add(this,
chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
content::Source<InfoBarService>(infobar_service_));
registrar_.Add(this,
chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
content::Source<InfoBarService>(infobar_service_));
}
virtual ~NavigationObserver() {}
void SetPathToWaitFor(const std::string& path) {
wait_for_path_ = path;
}
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE {
switch (type) {
case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED:
if (should_automatically_accept_infobar_) {
infobar_service_->infobar_manager()
->infobar_at(0)
->delegate()
->AsConfirmInfoBarDelegate()
->Accept();
}
infobar_shown_ = true;
return;
case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED:
infobar_removed_ = true;
return;
default:
NOTREACHED();
return;
}
}
virtual void DidFinishLoad(
int64 frame_id,
const GURL& validated_url,
bool is_main_frame,
content::RenderViewHost* render_view_host) OVERRIDE {
if (!wait_for_path_.empty()) {
if (validated_url.path() == wait_for_path_)
message_loop_runner_->Quit();
} else if (is_main_frame) {
message_loop_runner_->Quit();
}
}
bool infobar_shown() const { return infobar_shown_; }
bool infobar_removed() const { return infobar_removed_; }
void disable_should_automatically_accept_infobar() {
should_automatically_accept_infobar_ = false;
}
void Wait() {
message_loop_runner_->Run();
}
private:
std::string wait_for_path_;
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
bool infobar_shown_;
bool infobar_removed_;
bool should_automatically_accept_infobar_;
content::NotificationRegistrar registrar_;
InfoBarService* infobar_service_;
DISALLOW_COPY_AND_ASSIGN(NavigationObserver);
};
}
class PasswordManagerBrowserTest : public InProcessBrowserTest {
public:
PasswordManagerBrowserTest() {}
virtual ~PasswordManagerBrowserTest() {}
virtual void SetUpOnMainThread() OVERRIDE {
PasswordStoreFactory::GetInstance()->SetTestingFactory(
browser()->profile(), TestPasswordStoreService::Build);
}
protected:
content::WebContents* WebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
content::RenderViewHost* RenderViewHost() {
return WebContents()->GetRenderViewHost();
}
void NavigateToFile(const std::string& path) {
if (!embedded_test_server()->Started())
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
NavigationObserver observer(WebContents());
GURL url = embedded_test_server()->GetURL(path);
ui_test_utils::NavigateToURL(browser(), url);
observer.Wait();
}
void CheckScriptReturnValue(std::string& script, bool expected_return_value);
private:
DISALLOW_COPY_AND_ASSIGN(PasswordManagerBrowserTest);
};
void PasswordManagerBrowserTest::CheckScriptReturnValue(
std::string& script,
bool expected_return_value) {
const std::string wrapped_script =
std::string("window.domAutomationController.send(") + script + ");";
bool return_value = !expected_return_value;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
RenderViewHost(), wrapped_script, &return_value));
EXPECT_EQ(expected_return_value, return_value) << "script = " << script;
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForNormalSubmit) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForSubmitWithInPageNavigation) {
NavigateToFile("/password/password_navigate_before_submit.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
LoginSuccessWithUnrelatedForm) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_unrelated').value = 'temp';"
"document.getElementById('password_unrelated').value = 'random';"
"document.getElementById('submit_unrelated').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, LoginFailed) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_failed').value = 'temp';"
"document.getElementById('password_failed').value = 'random';"
"document.getElementById('submit_failed').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, Redirects) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_redirect').value = 'temp';"
"document.getElementById('password_redirect').value = 'random';"
"document.getElementById('submit_redirect').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.disable_should_automatically_accept_infobar();
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(),
"window.location.href = 'done.html';"));
observer.Wait();
EXPECT_FALSE(observer.infobar_removed());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForSubmitUsingJavaScript) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
DISABLED_PromptForDynamicForm) {
NavigateToFile("/password/dynamic_password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('create_form_button').click();"
"window.setTimeout(function() {"
" document.dynamic_form.username.value = 'tempro';"
" document.dynamic_form.password.value = 'random';"
" document.dynamic_form.submit();"
"}, 0)";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptForNavigation) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(),
"window.location.href = 'done.html';"));
observer.Wait();
EXPECT_FALSE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForSubFrameNavigation) {
NavigateToFile("/password/multi_frames.html");
NavigationObserver observer(WebContents());
observer.SetPathToWaitFor("/password/done.html");
std::string fill =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';";
std::string navigate_frame =
"var second_iframe = document.getElementById('second_frame');"
"second_iframe.contentWindow.location.href = 'done.html';";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill));
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame));
observer.Wait();
EXPECT_FALSE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptAfterSubmitWithSubFrameNavigation) {
NavigateToFile("/password/multi_frames.html");
NavigationObserver observer(WebContents());
observer.SetPathToWaitFor("/password/done.html");
std::string navigate_frame =
"var second_iframe = document.getElementById('second_frame');"
"second_iframe.contentWindow.location.href = 'other.html';";
std::string fill_and_submit =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';"
"frame_doc.getElementById('input_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame));
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForXHRSubmit) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
NavigateToFile("/password/password_xhr_submit.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForXHRWithoutOnSubmit) {
NavigateToFile("/password/password_xhr_submit.html");
NavigationObserver observer(WebContents());
std::string fill_and_navigate =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"send_xhr()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_navigate));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptIfLinkClicked) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_click_link =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('link').click();";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_click_link));
observer.Wait();
EXPECT_FALSE(observer.infobar_shown());
}
#if !defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
VerifyPasswordGenerationUpload) {
net::TestURLFetcherFactory factory;
autofill::test::DisableSystemServices(browser()->profile()->GetPrefs());
NavigateToFile("/password/signup_form.html");
NavigationObserver first_observer(WebContents());
std::string fill_and_submit =
"document.getElementById('other_info').value = 'stuff';"
"document.getElementById('username_field').value = 'my_username';"
"document.getElementById('password_field').value = 'password';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
first_observer.Wait();
ASSERT_TRUE(first_observer.infobar_shown());
NavigateToFile("/password/password_form.html");
content::SimulateMouseClick(
WebContents(), 0, blink::WebMouseEvent::ButtonLeft);
std::string get_username =
"window.domAutomationController.send("
"document.getElementById('username_field').value);";
std::string actual_username;
ASSERT_TRUE(content::ExecuteScriptAndExtractString(RenderViewHost(),
get_username,
&actual_username));
ASSERT_EQ("my_username", actual_username);
NavigationObserver second_observer(WebContents());
std::string submit_form =
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), submit_form));
second_observer.Wait();
EXPECT_FALSE(second_observer.infobar_shown());
base::HistogramBase* upload_histogram =
base::StatisticsRecorder::FindHistogram(
"PasswordGeneration.UploadStarted");
ASSERT_TRUE(upload_histogram);
scoped_ptr<base::HistogramSamples> snapshot =
upload_histogram->SnapshotSamples();
EXPECT_EQ(0, snapshot->GetCount(0 ));
EXPECT_EQ(1, snapshot->GetCount(1 ));
}
#endif
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForSubmitFromIframe) {
NavigateToFile("/password/password_submit_from_iframe.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"var iframe = document.getElementById('test_iframe');"
"var iframe_doc = iframe.contentDocument;"
"iframe_doc.getElementById('username_field').value = 'temp';"
"iframe_doc.getElementById('password_field').value = 'random';"
"iframe_doc.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForInputElementWithoutName) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field_no_name').value = 'temp';"
"document.getElementById('password_field_no_name').value = 'random';"
"document.getElementById('input_submit_button_no_name').click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForInputElementWithoutId) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementsByName('username_field_no_id')[0].value = 'temp';"
"document.getElementsByName('password_field_no_id')[0].value = 'random';"
"document.getElementsByName('input_submit_button_no_id')[0].click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForInputElementWithoutIdAndName) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"var form = document.getElementById('testform_elements_no_id_no_name');"
"var username = form.children[0];"
"username.value = 'temp';"
"var password = form.children[1];"
"password.value = 'random';"
"form.children[2].click()";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(observer.infobar_shown());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, DeleteFrameBeforeSubmit) {
NavigateToFile("/password/multi_frames.html");
NavigationObserver observer(WebContents());
std::string save_and_remove =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';"
"frame_doc.getElementById('input_submit_button').click();"
"first_frame.parentNode.removeChild(first_frame);";
std::string navigate_frame =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click();"
"window.location.href = 'done.html';";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove));
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame));
observer.Wait();
}
#if defined(OS_WIN) || defined(OS_ANDROID)
#define MAYBE_PasswordValueAccessible DISABLED_PasswordValueAccessible
#else
#define MAYBE_PasswordValueAccessible PasswordValueAccessible
#endif
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
MAYBE_PasswordValueAccessible) {
NavigateToFile("/password/form_and_link.html");
EXPECT_EQ(1, browser()->tab_strip_model()->count());
std::string click =
"document.getElementById('testlink').click();";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), click));
EXPECT_EQ(2, browser()->tab_strip_model()->count());
browser()->tab_strip_model()->ActivateTabAt(0, false);
NavigationObserver form_submit_observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
form_submit_observer.Wait();
EXPECT_TRUE(form_submit_observer.infobar_shown());
NavigationObserver reload_observer(WebContents());
NavigateToFile("/password/form_and_link.html");
reload_observer.Wait();
std::string check_username =
"document.getElementById('username_field').value == 'temp'";
std::string check_password =
"document.getElementById('password_field').value == 'random'";
CheckScriptReturnValue(check_username, true);
CheckScriptReturnValue(check_password, false);
content::SimulateMouseClick(
WebContents(), 0, blink::WebMouseEvent::ButtonLeft);
CheckScriptReturnValue(check_username, true);
CheckScriptReturnValue(check_password, true);
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
DontPromptForPasswordFormWithDefaultValue) {
NavigateToFile("/password/password_form_with_default_value.html");
NavigationObserver observer(WebContents());
NavigateToFile("/password/done.html");
observer.Wait();
EXPECT_FALSE(observer.infobar_shown());
}