Merge CrosDisksClientStubImpl into FakeCrosDisksClient
[chromium-blink-merge.git] / extensions / renderer / script_context_browsertest.cc
blobaeb74e45495ad43f370df2ba29a5c9be312913c5
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.
5 #include "content/public/renderer/render_frame.h"
6 #include "content/public/test/frame_load_waiter.h"
7 #include "content/public/test/render_view_test.h"
8 #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"
13 using blink::WebFrame;
15 namespace extensions {
16 namespace {
18 class ScriptContextTest : public content::RenderViewTest {
19 protected:
20 GURL GetEffectiveDocumentURL(const WebFrame* frame) {
21 return ScriptContext::GetEffectiveDocumentURL(
22 frame, frame->document().url(), true);
26 TEST_F(ScriptContextTest, GetEffectiveDocumentURL) {
27 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");
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>";
42 const char frame3_html[] = "<iframe name='frame3_1'></iframe>";
44 WebFrame* frame = GetMainFrame();
45 ASSERT_TRUE(frame);
47 frame->loadHTMLString(frame_html, top_url);
48 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait();
50 WebFrame* frame1 = frame->findChildByName("frame1");
51 ASSERT_TRUE(frame1);
52 WebFrame* frame1_1 = frame1->findChildByName("frame1_1");
53 ASSERT_TRUE(frame1_1);
54 WebFrame* frame1_2 = frame1->findChildByName("frame1_2");
55 ASSERT_TRUE(frame1_2);
56 WebFrame* frame2 = frame->findChildByName("frame2");
57 ASSERT_TRUE(frame2);
58 WebFrame* frame2_1 = frame2->findChildByName("frame2_1");
59 ASSERT_TRUE(frame2_1);
60 WebFrame* frame3 = frame->findChildByName("frame3");
61 ASSERT_TRUE(frame3);
63 // Load a blank document in a frame from a different origin.
64 frame3->loadHTMLString(frame3_html, different_url);
65 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait();
67 WebFrame* frame3_1 = frame->findChildByName("frame3");
68 ASSERT_TRUE(frame3_1);
70 // Top-level frame
71 EXPECT_EQ(GetEffectiveDocumentURL(frame), top_url);
72 // top -> srcdoc = inherit
73 EXPECT_EQ(GetEffectiveDocumentURL(frame1), top_url);
74 // top -> srcdoc -> about:blank = inherit
75 EXPECT_EQ(GetEffectiveDocumentURL(frame1_1), top_url);
76 // top -> srcdoc -> about:blank sandboxed = same URL
77 EXPECT_EQ(GetEffectiveDocumentURL(frame1_2), blank_url);
79 // top -> srcdoc [sandboxed] = same URL
80 EXPECT_EQ(GetEffectiveDocumentURL(frame2), srcdoc_url);
81 // top -> srcdoc [sandboxed] -> about:blank = same URL
82 EXPECT_EQ(GetEffectiveDocumentURL(frame2_1), blank_url);
84 // top -> different origin = different origin
85 EXPECT_EQ(GetEffectiveDocumentURL(frame3), different_url);
86 // top -> different origin -> about:blank = inherit
87 EXPECT_EQ(GetEffectiveDocumentURL(frame3_1), different_url);
90 } // namespace
91 } // namespace extensions