Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / FrameLoaderClientImplTest.cpp
blob48ef6cfd952de1f9631853dc1d04c7a5233b5440
1 /*
2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "web/FrameLoaderClientImpl.h"
34 #include "core/loader/FrameLoader.h"
35 #include "platform/weborigin/KURL.h"
36 #include "public/web/WebFrameClient.h"
37 #include "public/web/WebSettings.h"
38 #include "public/web/WebView.h"
39 #include "web/WebLocalFrameImpl.h"
40 #include "web/tests/FrameTestHelpers.h"
41 #include "wtf/text/CString.h"
42 #include "wtf/text/WTFString.h"
43 #include <gmock/gmock.h>
44 #include <gtest/gtest.h>
46 using testing::_;
47 using testing::Mock;
48 using testing::Return;
50 namespace blink {
51 namespace {
53 class MockWebFrameClient : public WebFrameClient {
54 public:
55 ~MockWebFrameClient() override { }
57 MOCK_METHOD2(userAgentOverride, WebString(WebLocalFrame*, const WebURL&));
60 class FrameLoaderClientImplTest : public ::testing::Test {
61 protected:
62 void SetUp() override
64 ON_CALL(m_webFrameClient, userAgentOverride(_, _)).WillByDefault(Return(WebString()));
66 FrameTestHelpers::TestWebViewClient webViewClient;
67 m_webView = WebView::create(&webViewClient);
68 // FIXME: http://crbug.com/363843. This needs to find a better way to
69 // not create graphics layers.
70 m_webView->settings()->setAcceleratedCompositingEnabled(false);
71 m_mainFrame = WebLocalFrame::create(WebTreeScopeType::Document, &m_webFrameClient);
72 m_webView->setMainFrame(m_mainFrame);
75 void TearDown() override
77 m_webView->close();
78 m_mainFrame->close();
81 WebString userAgent()
83 // The test always returns the same user agent, regardless of the URL passed in.
84 KURL dummyURL(ParsedURLString, "about:blank");
85 WTF::CString userAgent = frameLoaderClient().userAgent(dummyURL).utf8();
86 return WebString::fromUTF8(userAgent.data(), userAgent.length());
89 WebLocalFrameImpl* mainFrame() { return toWebLocalFrameImpl(m_webView->mainFrame()); }
90 Document& document() { return *toWebLocalFrameImpl(m_mainFrame)->frame()->document(); }
91 MockWebFrameClient& webFrameClient() { return m_webFrameClient; }
92 FrameLoaderClient& frameLoaderClient() { return *toFrameLoaderClientImpl(toWebLocalFrameImpl(m_webView->mainFrame())->frame()->loader().client()); }
94 private:
95 MockWebFrameClient m_webFrameClient;
96 WebView* m_webView;
97 WebFrame* m_mainFrame;
100 TEST_F(FrameLoaderClientImplTest, UserAgentOverride)
102 const WebString defaultUserAgent = userAgent();
103 const WebString overrideUserAgent = WebString::fromUTF8("dummy override");
105 // Override the user agent and make sure we get it back.
106 EXPECT_CALL(webFrameClient(), userAgentOverride(_, _)).WillOnce(Return(overrideUserAgent));
107 EXPECT_TRUE(overrideUserAgent.equals(userAgent()));
108 Mock::VerifyAndClearExpectations(&webFrameClient());
110 // Remove the override and make sure we get the original back.
111 EXPECT_CALL(webFrameClient(), userAgentOverride(_, _)).WillOnce(Return(WebString()));
112 EXPECT_TRUE(defaultUserAgent.equals(userAgent()));
115 } // namespace
116 } // namespace blink