1/* 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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. 17 * 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. 29 */ 30 31#include "config.h" 32 33#include "web/FrameLoaderClientImpl.h" 34 35#include "core/loader/FrameLoader.h" 36#include "platform/weborigin/KURL.h" 37#include "public/web/WebFrameClient.h" 38#include "public/web/WebSettings.h" 39#include "public/web/WebView.h" 40#include "web/WebLocalFrameImpl.h" 41#include "web/tests/FrameTestHelpers.h" 42#include "wtf/text/CString.h" 43#include "wtf/text/WTFString.h" 44 45#include <gtest/gtest.h> 46 47using namespace blink; 48 49namespace { 50 51class TestWebFrameClient : public WebFrameClient { 52public: 53 WebString userAgentOverride(WebLocalFrame* frame, const WebURL& url) OVERRIDE 54 { 55 if (m_userAgentOverride.isEmpty()) 56 return WebString(); 57 58 return m_userAgentOverride; 59 } 60 61 void setUserAgentOverride(const WebString& userAgent) 62 { 63 m_userAgentOverride = userAgent; 64 } 65 66private: 67 WebString m_userAgentOverride; 68}; 69 70class FrameLoaderClientImplTest : public testing::Test { 71public: 72 void SetUp() 73 { 74 FrameTestHelpers::TestWebViewClient webViewClient; 75 m_webView = WebView::create(&webViewClient); 76 // FIXME: http://crbug.com/363843. This needs to find a better way to 77 // not create graphics layers. 78 m_webView->settings()->setAcceleratedCompositingEnabled(false); 79 m_mainFrame = WebLocalFrame::create(&m_webFrameClient); 80 m_webView->setMainFrame(m_mainFrame); 81 m_frameLoaderClientImpl = toFrameLoaderClientImpl(toWebLocalFrameImpl(m_webView->mainFrame())->frame()->loader().client()); 82 } 83 84 void TearDown() 85 { 86 m_webView->close(); 87 m_mainFrame->close(); 88 } 89 90 void setUserAgentOverride(const WebString& userAgent) 91 { 92 return m_webFrameClient.setUserAgentOverride(userAgent); 93 } 94 95 const WebString userAgent() 96 { 97 // The test always returns the same user agent, regardless of the URL passed in. 98 KURL dummyURL(ParsedURLString, "about:blank"); 99 WTF::CString userAgent = m_frameLoaderClientImpl->userAgent(dummyURL).utf8(); 100 return WebString::fromUTF8(userAgent.data(), userAgent.length()); 101 } 102 103protected: 104 TestWebFrameClient m_webFrameClient; 105 FrameLoaderClientImpl* m_frameLoaderClientImpl; 106 WebView* m_webView; 107 WebFrame* m_mainFrame; 108}; 109 110TEST_F(FrameLoaderClientImplTest, UserAgentOverride) 111{ 112 const WebString defaultUserAgent = userAgent(); 113 const WebString override = WebString::fromUTF8("dummy override"); 114 115 // Override the user agent and make sure we get it back. 116 setUserAgentOverride(override); 117 EXPECT_TRUE(override.equals(userAgent())); 118 119 // Remove the override and make sure we get the original back. 120 setUserAgentOverride(WebString()); 121 EXPECT_TRUE(defaultUserAgent.equals(userAgent())); 122} 123 124} // namespace 125