Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / transition_browsertest.cc
1 // Copyright 2014 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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "content/browser/loader/cross_site_resource_handler.h"
8 #include "content/browser/loader/resource_dispatcher_host_impl.h"
9 #include "content/browser/loader/resource_request_info_impl.h"
10 #include "content/browser/transition_request_manager.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/content_browser_test.h"
16 #include "content/public/test/content_browser_test_utils.h"
17 #include "content/public/test/test_utils.h"
18 #include "content/shell/browser/shell.h"
19 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "net/url_request/url_request.h"
22
23 namespace content {
24
25 class TransitionBrowserTest : public ContentBrowserTest {
26  public:
27   TransitionBrowserTest() {}
28
29   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
30     command_line->AppendSwitch(
31         switches::kEnableExperimentalWebPlatformFeatures);
32   }
33
34  private:
35   DISALLOW_COPY_AND_ASSIGN(TransitionBrowserTest);
36 };
37
38 class TransitionBrowserTestObserver
39     : public WebContentsObserver,
40       public ShellResourceDispatcherHostDelegate {
41  public:
42   TransitionBrowserTestObserver(WebContents* web_contents)
43       : WebContentsObserver(web_contents),
44         request_(NULL),
45         did_defer_response_(false),
46         is_transition_request_(false) {
47   }
48
49   virtual void RequestBeginning(
50       net::URLRequest* request,
51       ResourceContext* resource_context,
52       AppCacheService* appcache_service,
53       ResourceType resource_type,
54       int child_id,
55       int route_id,
56       ScopedVector<ResourceThrottle>* throttles) OVERRIDE {
57     CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
58     ShellResourceDispatcherHostDelegate::RequestBeginning(request,
59                                                           resource_context,
60                                                           appcache_service,
61                                                           resource_type,
62                                                           child_id,
63                                                           route_id,
64                                                           throttles);
65     request_ = request;
66
67     ResourceRequestInfoImpl* info =
68         ResourceRequestInfoImpl::ForRequest(request_);
69
70     if (is_transition_request_) {
71       TransitionRequestManager::GetInstance()->AddPendingTransitionRequestData(
72           child_id, info->GetRenderFrameID(), "*", "", "");
73     }
74   }
75
76   virtual void OnResponseStarted(
77       net::URLRequest* request,
78       ResourceContext* resource_context,
79       ResourceResponse* response,
80       IPC::Sender* sender) OVERRIDE {
81     ResourceRequestInfoImpl* info =
82         ResourceRequestInfoImpl::ForRequest(request_);
83
84     did_defer_response_ = info->cross_site_handler()->did_defer_for_testing();
85   }
86
87   void set_pending_transition_request(bool is_transition_request) {
88     is_transition_request_ = is_transition_request;
89   }
90
91   bool did_defer_response() const { return did_defer_response_; }
92
93  private:
94   net::URLRequest* request_;
95   bool did_defer_response_;
96   bool is_transition_request_;
97 };
98
99 // This tests that normal navigations don't defer at first response.
100 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest,
101                        NormalNavigationNotDeferred) {
102   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
103   scoped_ptr<TransitionBrowserTestObserver> observer(
104       new TransitionBrowserTestObserver(shell()->web_contents()));
105
106   ResourceDispatcherHost::Get()->SetDelegate(observer.get());
107
108   NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
109
110   EXPECT_FALSE(observer->did_defer_response());
111 }
112
113 // This tests that when a navigation transition is detected, the response is
114 // deferred.
115 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest,
116                        TransitionNavigationIsDeferred) {
117   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
118   scoped_ptr<TransitionBrowserTestObserver> observer(
119       new TransitionBrowserTestObserver(shell()->web_contents()));
120
121   ResourceDispatcherHost::Get()->SetDelegate(observer.get());
122   observer->set_pending_transition_request(true);
123
124   NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
125
126   EXPECT_TRUE(observer->did_defer_response());
127 }
128
129 // This tests that the renderer is reused between the outgoing and transition.
130 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest,
131                        TransitionNavigationSharesRenderer) {
132   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
133
134   NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
135
136   int outgoing_process_id =
137       shell()->web_contents()->GetRenderProcessHost()->GetID();
138
139   WebContents::CreateParams create_params(
140       shell()->web_contents()->GetBrowserContext(),
141       shell()->web_contents()->GetSiteInstance());
142   scoped_ptr<WebContents> transition_web_contents(
143       WebContents::Create(create_params));
144
145   GURL about_blank(url::kAboutBlankURL);
146   NavigationController::LoadURLParams params(about_blank);
147   transition_web_contents->GetController().LoadURLWithParams(params);
148   transition_web_contents->Focus();
149
150   WaitForLoadStop(transition_web_contents.get());
151
152   int transition_process_id =
153       transition_web_contents->GetRenderProcessHost()->GetID();
154
155   EXPECT_EQ(outgoing_process_id, transition_process_id);
156 }
157
158 }  // namespace content