blob: 411894a13fba08b438fc85afbf68aecb12140130 [file] [log] [blame]
[email protected]ae26b282014-05-15 16:40:161// 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
vivek.vgd155cb802014-12-22 14:37:105#include "chrome/test/base/chrome_render_view_test.h"
mekcbb4b862014-09-05 03:08:226#include "content/public/renderer/render_frame.h"
7#include "content/public/test/frame_load_waiter.h"
[email protected]ae26b282014-05-15 16:40:168#include "extensions/renderer/script_context.h"
Jeremy Roman1a4d9b82017-12-08 01:46:409#include "extensions/renderer/script_context_set.h"
Blink Reformata30d4232018-04-07 15:31:0610#include "third_party/blink/public/web/web_document.h"
11#include "third_party/blink/public/web/web_local_frame.h"
[email protected]ae26b282014-05-15 16:40:1612#include "url/gurl.h"
13
Daniel Cheng971cd4522017-05-31 21:58:2214using blink::WebLocalFrame;
[email protected]ae26b282014-05-15 16:40:1615
16namespace extensions {
17namespace {
18
vivek.vgd155cb802014-12-22 14:37:1019class ScriptContextTest : public ChromeRenderViewTest {
[email protected]ae26b282014-05-15 16:40:1620 protected:
lukaszabedb4b22017-06-23 00:00:1321 GURL GetEffectiveDocumentURL(WebLocalFrame* frame) {
[email protected]ae26b282014-05-15 16:40:1622 return ScriptContext::GetEffectiveDocumentURL(
Blink Reformat1c4d759e2017-04-09 16:34:5423 frame, frame->GetDocument().Url(), true);
[email protected]ae26b282014-05-15 16:40:1624 }
25};
26
mekcbb4b862014-09-05 03:08:2227TEST_F(ScriptContextTest, GetEffectiveDocumentURL) {
[email protected]ae26b282014-05-15 16:40:1628 GURL top_url("http://example.com/");
29 GURL different_url("http://example.net/");
30 GURL blank_url("about:blank");
31 GURL srcdoc_url("about:srcdoc");
32
33 const char frame_html[] =
34 "<iframe name='frame1' srcdoc=\""
35 " <iframe name='frame1_1'></iframe>"
36 " <iframe name='frame1_2' sandbox=''></iframe>"
37 "\"></iframe>"
38 "<iframe name='frame2' sandbox='' srcdoc=\""
39 " <iframe name='frame2_1'></iframe>"
40 "\"></iframe>"
41 "<iframe name='frame3'></iframe>";
42
43 const char frame3_html[] = "<iframe name='frame3_1'></iframe>";
44
Daniel Cheng971cd4522017-05-31 21:58:2245 WebLocalFrame* frame = GetMainFrame();
[email protected]ae26b282014-05-15 16:40:1646 ASSERT_TRUE(frame);
47
Blink Reformat1c4d759e2017-04-09 16:34:5448 frame->LoadHTMLString(frame_html, top_url);
mekcbb4b862014-09-05 03:08:2249 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait();
[email protected]ae26b282014-05-15 16:40:1650
Daniel Cheng971cd4522017-05-31 21:58:2251 WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1652 ASSERT_TRUE(frame1);
Blink Reformat1c4d759e2017-04-09 16:34:5453 ASSERT_EQ("frame1", frame1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2254 WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1655 ASSERT_TRUE(frame1_1);
Blink Reformat1c4d759e2017-04-09 16:34:5456 ASSERT_EQ("frame1_1", frame1_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2257 WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1658 ASSERT_TRUE(frame1_2);
Blink Reformat1c4d759e2017-04-09 16:34:5459 ASSERT_EQ("frame1_2", frame1_2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2260 WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1661 ASSERT_TRUE(frame2);
Blink Reformat1c4d759e2017-04-09 16:34:5462 ASSERT_EQ("frame2", frame2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2263 WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1664 ASSERT_TRUE(frame2_1);
Blink Reformat1c4d759e2017-04-09 16:34:5465 ASSERT_EQ("frame2_1", frame2_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2266 WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1667 ASSERT_TRUE(frame3);
Blink Reformat1c4d759e2017-04-09 16:34:5468 ASSERT_EQ("frame3", frame3->AssignedName());
[email protected]ae26b282014-05-15 16:40:1669
70 // Load a blank document in a frame from a different origin.
Blink Reformat1c4d759e2017-04-09 16:34:5471 frame3->LoadHTMLString(frame3_html, different_url);
mekcbb4b862014-09-05 03:08:2272 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait();
[email protected]ae26b282014-05-15 16:40:1673
Daniel Cheng971cd4522017-05-31 21:58:2274 WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1675 ASSERT_TRUE(frame3_1);
Blink Reformat1c4d759e2017-04-09 16:34:5476 ASSERT_EQ("frame3_1", frame3_1->AssignedName());
[email protected]ae26b282014-05-15 16:40:1677
78 // Top-level frame
79 EXPECT_EQ(GetEffectiveDocumentURL(frame), top_url);
80 // top -> srcdoc = inherit
81 EXPECT_EQ(GetEffectiveDocumentURL(frame1), top_url);
82 // top -> srcdoc -> about:blank = inherit
83 EXPECT_EQ(GetEffectiveDocumentURL(frame1_1), top_url);
84 // top -> srcdoc -> about:blank sandboxed = same URL
85 EXPECT_EQ(GetEffectiveDocumentURL(frame1_2), blank_url);
86
87 // top -> srcdoc [sandboxed] = same URL
88 EXPECT_EQ(GetEffectiveDocumentURL(frame2), srcdoc_url);
89 // top -> srcdoc [sandboxed] -> about:blank = same URL
90 EXPECT_EQ(GetEffectiveDocumentURL(frame2_1), blank_url);
91
92 // top -> different origin = different origin
93 EXPECT_EQ(GetEffectiveDocumentURL(frame3), different_url);
94 // top -> different origin -> about:blank = inherit
95 EXPECT_EQ(GetEffectiveDocumentURL(frame3_1), different_url);
96}
97
Jeremy Roman1a4d9b82017-12-08 01:46:4098TEST_F(ScriptContextTest, GetMainWorldContextForFrame) {
99 // ScriptContextSet::GetMainWorldContextForFrame should work, even without an
100 // existing v8::HandleScope.
101 content::RenderFrame* render_frame =
102 content::RenderFrame::FromWebFrame(GetMainFrame());
103 ScriptContext* script_context =
104 ScriptContextSet::GetMainWorldContextForFrame(render_frame);
105 ASSERT_TRUE(script_context);
106 EXPECT_EQ(render_frame, script_context->GetRenderFrame());
107}
108
[email protected]ae26b282014-05-15 16:40:16109} // namespace
110} // namespace extensions