Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / test / test_web_contents.cc
1 // Copyright (c) 2012 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.
4
5 #include "content/test/test_web_contents.h"
6
7 #include <utility>
8
9 #include "content/browser/browser_url_handler_impl.h"
10 #include "content/browser/frame_host/cross_process_frame_connector.h"
11 #include "content/browser/frame_host/navigation_entry_impl.h"
12 #include "content/browser/frame_host/navigator.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/site_instance_impl.h"
15 #include "content/common/frame_messages.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/common/page_state.h"
21 #include "content/public/common/page_transition_types.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/test/test_render_view_host.h"
24
25 namespace content {
26
27 TestWebContents::TestWebContents(BrowserContext* browser_context)
28     : WebContentsImpl(browser_context, NULL),
29       transition_cross_site(false),
30       delegate_view_override_(NULL),
31       expect_set_history_length_and_prune_(false),
32       expect_set_history_length_and_prune_site_instance_(NULL),
33       expect_set_history_length_and_prune_history_length_(0),
34       expect_set_history_length_and_prune_min_page_id_(-1) {
35 }
36
37 TestWebContents* TestWebContents::Create(BrowserContext* browser_context,
38                                          SiteInstance* instance) {
39   TestWebContents* test_web_contents = new TestWebContents(browser_context);
40   test_web_contents->Init(WebContents::CreateParams(browser_context, instance));
41   return test_web_contents;
42 }
43
44 TestWebContents::~TestWebContents() {
45   EXPECT_FALSE(expect_set_history_length_and_prune_);
46 }
47
48 RenderViewHost* TestWebContents::GetPendingRenderViewHost() const {
49   return GetRenderManager()->pending_render_view_host();
50 }
51
52 TestRenderViewHost* TestWebContents::pending_test_rvh() const {
53   return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
54 }
55
56 void TestWebContents::TestDidNavigate(RenderViewHost* render_view_host,
57                                       int page_id,
58                                       const GURL& url,
59                                       PageTransition transition) {
60   TestDidNavigateWithReferrer(render_view_host,
61                               page_id,
62                               url,
63                               Referrer(),
64                               transition);
65 }
66
67 void TestWebContents::TestDidNavigateWithReferrer(
68     RenderViewHost* render_view_host,
69     int page_id,
70     const GURL& url,
71     const Referrer& referrer,
72     PageTransition transition) {
73   FrameHostMsg_DidCommitProvisionalLoad_Params params;
74
75   params.page_id = page_id;
76   params.url = url;
77   params.referrer = referrer;
78   params.transition = transition;
79   params.redirects = std::vector<GURL>();
80   params.should_update_history = false;
81   params.searchable_form_url = GURL();
82   params.searchable_form_encoding = std::string();
83   params.security_info = std::string();
84   params.gesture = NavigationGestureUser;
85   params.was_within_same_page = false;
86   params.is_post = false;
87   params.page_state = PageState::CreateFromURL(url);
88
89   RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(render_view_host);
90   RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(
91       rvh->GetProcess()->GetID(), rvh->main_frame_routing_id());
92   frame_tree_.root()->navigator()->DidNavigate(rfh, params);
93 }
94
95 WebPreferences TestWebContents::TestGetWebkitPrefs() {
96   return GetWebkitPrefs();
97 }
98
99 bool TestWebContents::CreateRenderViewForRenderManager(
100     RenderViewHost* render_view_host,
101     int opener_route_id,
102     CrossProcessFrameConnector* frame_connector) {
103   UpdateMaxPageIDIfNecessary(render_view_host);
104   // This will go to a TestRenderViewHost.
105   static_cast<RenderViewHostImpl*>(
106       render_view_host)->CreateRenderView(base::string16(),
107                                           opener_route_id,
108                                           -1, false);
109   return true;
110 }
111
112 WebContents* TestWebContents::Clone() {
113   WebContentsImpl* contents =
114       Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
115   contents->GetController().CopyStateFrom(controller_);
116   return contents;
117 }
118
119 void TestWebContents::NavigateAndCommit(const GURL& url) {
120   GetController().LoadURL(
121       url, Referrer(), PAGE_TRANSITION_LINK, std::string());
122   GURL loaded_url(url);
123   bool reverse_on_redirect = false;
124   BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
125       &loaded_url, GetBrowserContext(), &reverse_on_redirect);
126
127   // LoadURL created a navigation entry, now simulate the RenderView sending
128   // a notification that it actually navigated.
129   CommitPendingNavigation();
130 }
131
132 void TestWebContents::TestSetIsLoading(bool value) {
133   SetIsLoading(GetRenderViewHost(), value, true, NULL);
134 }
135
136 void TestWebContents::CommitPendingNavigation() {
137   // If we are doing a cross-site navigation, this simulates the current RVH
138   // notifying that it has unloaded so the pending RVH is resumed and can
139   // navigate.
140   ProceedWithCrossSiteNavigation();
141   RenderViewHost* old_rvh = GetRenderViewHost();
142   TestRenderViewHost* rvh =
143       static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
144   if (!rvh)
145     rvh = static_cast<TestRenderViewHost*>(old_rvh);
146
147   const NavigationEntry* entry = GetController().GetPendingEntry();
148   DCHECK(entry);
149   int page_id = entry->GetPageID();
150   if (page_id == -1) {
151     // It's a new navigation, assign a never-seen page id to it.
152     page_id = GetMaxPageIDForSiteInstance(rvh->GetSiteInstance()) + 1;
153   }
154
155   rvh->SendNavigate(page_id, entry->GetURL());
156   // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
157   // (old_rvh != rvh).
158   if (old_rvh != rvh)
159     static_cast<RenderViewHostImpl*>(old_rvh)->OnSwappedOut(false);
160 }
161
162 void TestWebContents::ProceedWithCrossSiteNavigation() {
163   if (!GetPendingRenderViewHost())
164     return;
165   TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>(
166       GetRenderViewHost());
167   rvh->SendBeforeUnloadACK(true);
168 }
169
170 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
171   if (delegate_view_override_)
172     return delegate_view_override_;
173   return WebContentsImpl::GetDelegateView();
174 }
175
176 void TestWebContents::SetOpener(TestWebContents* opener) {
177   // This is normally only set in the WebContents constructor, which also
178   // registers an observer for when the opener gets closed.
179   opener_ = opener;
180   AddDestructionObserver(opener_);
181 }
182
183 void TestWebContents::AddPendingContents(TestWebContents* contents) {
184   // This is normally only done in WebContentsImpl::CreateNewWindow.
185   pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
186   AddDestructionObserver(contents);
187 }
188
189 void TestWebContents::ExpectSetHistoryLengthAndPrune(
190     const SiteInstance* site_instance,
191     int history_length,
192     int32 min_page_id) {
193   expect_set_history_length_and_prune_ = true;
194   expect_set_history_length_and_prune_site_instance_ =
195       static_cast<const SiteInstanceImpl*>(site_instance);
196   expect_set_history_length_and_prune_history_length_ = history_length;
197   expect_set_history_length_and_prune_min_page_id_ = min_page_id;
198 }
199
200 void TestWebContents::SetHistoryLengthAndPrune(
201     const SiteInstance* site_instance, int history_length,
202     int32 min_page_id) {
203   EXPECT_TRUE(expect_set_history_length_and_prune_);
204   expect_set_history_length_and_prune_ = false;
205   EXPECT_EQ(expect_set_history_length_and_prune_site_instance_, site_instance);
206   EXPECT_EQ(expect_set_history_length_and_prune_history_length_,
207             history_length);
208   EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id);
209 }
210
211 void TestWebContents::TestDidFinishLoad(const GURL& url) {
212   FrameHostMsg_DidFinishLoad msg(0, url);
213   frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
214 }
215
216 void TestWebContents::TestDidFailLoadWithError(
217     const GURL& url,
218     int error_code,
219     const base::string16& error_description) {
220   FrameHostMsg_DidFailLoadWithError msg(
221       0, url, error_code, error_description);
222   frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
223 }
224
225 void TestWebContents::CreateNewWindow(
226     int render_process_id,
227     int route_id,
228     int main_frame_route_id,
229     const ViewHostMsg_CreateWindow_Params& params,
230     SessionStorageNamespace* session_storage_namespace) {
231 }
232
233 void TestWebContents::CreateNewWidget(int render_process_id,
234                                       int route_id,
235                                       blink::WebPopupType popup_type) {
236 }
237
238 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
239                                                 int route_id) {
240 }
241
242 void TestWebContents::ShowCreatedWindow(int route_id,
243                                         WindowOpenDisposition disposition,
244                                         const gfx::Rect& initial_pos,
245                                         bool user_gesture) {
246 }
247
248 void TestWebContents::ShowCreatedWidget(int route_id,
249                                         const gfx::Rect& initial_pos) {
250 }
251
252 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
253 }
254
255 }  // namespace content