blob: d7047ef3b7649e0b7f63147ad7b9ae53df23972d [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"
9#include "third_party/WebKit/public/web/WebDocument.h"
10#include "third_party/WebKit/public/web/WebLocalFrame.h"
11#include "url/gurl.h"
12
Daniel Cheng971cd4522017-05-31 21:58:2213using blink::WebLocalFrame;
[email protected]ae26b282014-05-15 16:40:1614
15namespace extensions {
16namespace {
17
vivek.vgd155cb802014-12-22 14:37:1018class ScriptContextTest : public ChromeRenderViewTest {
[email protected]ae26b282014-05-15 16:40:1619 protected:
Daniel Cheng971cd4522017-05-31 21:58:2220 GURL GetEffectiveDocumentURL(const WebLocalFrame* frame) {
[email protected]ae26b282014-05-15 16:40:1621 return ScriptContext::GetEffectiveDocumentURL(
Blink Reformat1c4d759e2017-04-09 16:34:5422 frame, frame->GetDocument().Url(), true);
[email protected]ae26b282014-05-15 16:40:1623 }
24};
25
mekcbb4b862014-09-05 03:08:2226TEST_F(ScriptContextTest, GetEffectiveDocumentURL) {
[email protected]ae26b282014-05-15 16:40:1627 GURL top_url("http://example.com/");
28 GURL different_url("http://example.net/");
29 GURL blank_url("about:blank");
30 GURL srcdoc_url("about:srcdoc");
31
32 const char frame_html[] =
33 "<iframe name='frame1' srcdoc=\""
34 " <iframe name='frame1_1'></iframe>"
35 " <iframe name='frame1_2' sandbox=''></iframe>"
36 "\"></iframe>"
37 "<iframe name='frame2' sandbox='' srcdoc=\""
38 " <iframe name='frame2_1'></iframe>"
39 "\"></iframe>"
40 "<iframe name='frame3'></iframe>";
41
42 const char frame3_html[] = "<iframe name='frame3_1'></iframe>";
43
Daniel Cheng971cd4522017-05-31 21:58:2244 WebLocalFrame* frame = GetMainFrame();
[email protected]ae26b282014-05-15 16:40:1645 ASSERT_TRUE(frame);
46
Blink Reformat1c4d759e2017-04-09 16:34:5447 frame->LoadHTMLString(frame_html, top_url);
mekcbb4b862014-09-05 03:08:2248 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait();
[email protected]ae26b282014-05-15 16:40:1649
Daniel Cheng971cd4522017-05-31 21:58:2250 WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1651 ASSERT_TRUE(frame1);
Blink Reformat1c4d759e2017-04-09 16:34:5452 ASSERT_EQ("frame1", frame1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2253 WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1654 ASSERT_TRUE(frame1_1);
Blink Reformat1c4d759e2017-04-09 16:34:5455 ASSERT_EQ("frame1_1", frame1_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2256 WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1657 ASSERT_TRUE(frame1_2);
Blink Reformat1c4d759e2017-04-09 16:34:5458 ASSERT_EQ("frame1_2", frame1_2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2259 WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1660 ASSERT_TRUE(frame2);
Blink Reformat1c4d759e2017-04-09 16:34:5461 ASSERT_EQ("frame2", frame2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2262 WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1663 ASSERT_TRUE(frame2_1);
Blink Reformat1c4d759e2017-04-09 16:34:5464 ASSERT_EQ("frame2_1", frame2_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2265 WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1666 ASSERT_TRUE(frame3);
Blink Reformat1c4d759e2017-04-09 16:34:5467 ASSERT_EQ("frame3", frame3->AssignedName());
[email protected]ae26b282014-05-15 16:40:1668
69 // Load a blank document in a frame from a different origin.
Blink Reformat1c4d759e2017-04-09 16:34:5470 frame3->LoadHTMLString(frame3_html, different_url);
mekcbb4b862014-09-05 03:08:2271 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait();
[email protected]ae26b282014-05-15 16:40:1672
Daniel Cheng971cd4522017-05-31 21:58:2273 WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1674 ASSERT_TRUE(frame3_1);
Blink Reformat1c4d759e2017-04-09 16:34:5475 ASSERT_EQ("frame3_1", frame3_1->AssignedName());
[email protected]ae26b282014-05-15 16:40:1676
77 // Top-level frame
78 EXPECT_EQ(GetEffectiveDocumentURL(frame), top_url);
79 // top -> srcdoc = inherit
80 EXPECT_EQ(GetEffectiveDocumentURL(frame1), top_url);
81 // top -> srcdoc -> about:blank = inherit
82 EXPECT_EQ(GetEffectiveDocumentURL(frame1_1), top_url);
83 // top -> srcdoc -> about:blank sandboxed = same URL
84 EXPECT_EQ(GetEffectiveDocumentURL(frame1_2), blank_url);
85
86 // top -> srcdoc [sandboxed] = same URL
87 EXPECT_EQ(GetEffectiveDocumentURL(frame2), srcdoc_url);
88 // top -> srcdoc [sandboxed] -> about:blank = same URL
89 EXPECT_EQ(GetEffectiveDocumentURL(frame2_1), blank_url);
90
91 // top -> different origin = different origin
92 EXPECT_EQ(GetEffectiveDocumentURL(frame3), different_url);
93 // top -> different origin -> about:blank = inherit
94 EXPECT_EQ(GetEffectiveDocumentURL(frame3_1), different_url);
95}
96
97} // namespace
98} // namespace extensions