1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_TEST_BASE_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_TEST_BASE_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/test/test_utils.h"
18 class NavigationObserver
: public content::WebContentsObserver
{
20 explicit NavigationObserver(content::WebContents
* web_contents
);
21 ~NavigationObserver() override
;
23 // Normally Wait() will not return until a main frame navigation occurs.
24 // If a path is set, Wait() will return after this path has been seen,
25 // regardless of the frame that navigated. Useful for multi-frame pages.
26 void SetPathToWaitFor(const std::string
& path
) { wait_for_path_
= path
; }
28 // Normally Wait() will not return until a main frame navigation occurs.
29 // If quit_on_entry_committed is true Wait() will return on EntryCommited.
30 void set_quit_on_entry_committed(bool quit_on_entry_committed
) {
31 quit_on_entry_committed_
= quit_on_entry_committed
;
34 // Wait for navigation to succeed.
37 // Returns the RenderFrameHost that navigated.
38 content::RenderFrameHost
* render_frame_host() { return render_frame_host_
; }
40 // content::WebContentsObserver:
41 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
42 const GURL
& validated_url
) override
;
43 void NavigationEntryCommitted(
44 const content::LoadCommittedDetails
& load_details
) override
;
47 std::string wait_for_path_
;
48 content::RenderFrameHost
* render_frame_host_
;
49 bool quit_on_entry_committed_
;
50 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
52 DISALLOW_COPY_AND_ASSIGN(NavigationObserver
);
55 // Observes the save password prompt (bubble or infobar) for a specified
56 // WebContents, keeps track of whether or not it is currently shown, and allows
57 // accepting saving passwords through it.
58 class PromptObserver
{
60 virtual ~PromptObserver();
62 // Checks if the save prompt is being currently shown.
63 virtual bool IsShowingPrompt() const = 0;
65 // Checks if the update prompt is being currently shown.
66 virtual bool IsShowingUpdatePrompt() const;
68 // Expecting that the prompt is shown, saves the password. Checks that the
69 // prompt is no longer visible afterwards.
72 // Expecting that the prompt is shown, update |form| with the password from
73 // observed form. Checks that the prompt is no longer visible afterwards.
74 void AcceptUpdatePrompt(const autofill::PasswordForm
& form
) const;
76 // Chooses the right implementation of PromptObserver and creates an instance
78 static scoped_ptr
<PromptObserver
> Create(content::WebContents
* web_contents
);
83 // Accepts the password. The implementation can assume that the prompt is
84 // currently shown, but is required to verify that the prompt is eventually
86 virtual void AcceptImpl() const = 0;
88 // Accepts the password update. The implementation can assume that the prompt
89 // is currently shown, but is required to verify that the prompt is eventually
91 // TODO(dvadym): Make this method pure virtual as soon as update UI is
92 // implemented for infobar. http://crbug.com/359315
93 virtual void AcceptUpdatePromptImpl(
94 const autofill::PasswordForm
& form
) const {}
97 DISALLOW_COPY_AND_ASSIGN(PromptObserver
);
100 class PasswordManagerBrowserTestBase
: public InProcessBrowserTest
{
102 PasswordManagerBrowserTestBase();
103 ~PasswordManagerBrowserTestBase() override
;
105 // InProcessBrowserTest:
106 void SetUpOnMainThread() override
;
107 void TearDownOnMainThread() override
;
110 // Wrapper around ui_test_utils::NavigateToURL that waits until
111 // DidFinishLoad() fires. Normally this function returns after
112 // DidStopLoading(), which caused flakiness as the NavigationObserver
113 // would sometimes see the DidFinishLoad event from a previous navigation and
114 // return immediately.
115 void NavigateToFile(const std::string
& path
);
117 // Navigates to |filename| and runs |submission_script| to submit. Navigates
118 // back to |filename| and then verifies that |expected_element| has
120 void VerifyPasswordIsSavedAndFilled(const std::string
& filename
,
121 const std::string
& submission_script
,
122 const std::string
& expected_element
,
123 const std::string
& expected_value
);
125 // Waits until the "value" attribute of the HTML element with |element_id| is
126 // equal to |expected_value|. If the current value is not as expected, this
127 // waits until the "change" event is fired for the element. This also
128 // guarantees that once the real value matches the expected, the JavaScript
129 // event loop is spun to allow all other possible events to take place.
130 void WaitForElementValue(const std::string
& element_id
,
131 const std::string
& expected_value
);
132 // Same as above except the element |element_id| is in iframe |iframe_id|
133 void WaitForElementValue(const std::string
& iframe_id
,
134 const std::string
& element_id
,
135 const std::string
& expected_value
);
136 // Checks that the current "value" attribute of the HTML element with
137 // |element_id| is equal to |expected_value|.
138 void CheckElementValue(const std::string
& element_id
,
139 const std::string
& expected_value
);
140 // Same as above except the element |element_id| is in iframe |iframe_id|
141 void CheckElementValue(const std::string
& iframe_id
,
142 const std::string
& element_id
,
143 const std::string
& expected_value
);
146 content::WebContents
* WebContents();
147 content::RenderViewHost
* RenderViewHost();
150 DISALLOW_COPY_AND_ASSIGN(PasswordManagerBrowserTestBase
);
153 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_TEST_BASE_H_