Home | History | Annotate | Download | only in tests
      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 "wtf/text/CString.h"
     42 #include "wtf/text/WTFString.h"
     43 
     44 #include <gtest/gtest.h>
     45 
     46 using namespace blink;
     47 
     48 namespace {
     49 
     50 class TestWebFrameClient : public WebFrameClient {
     51 public:
     52     WebString userAgentOverride(WebLocalFrame* frame, const WebURL& url) OVERRIDE
     53     {
     54         if (m_userAgentOverride.isEmpty())
     55             return WebString();
     56 
     57         return m_userAgentOverride;
     58     }
     59 
     60     void setUserAgentOverride(const WebString& userAgent)
     61     {
     62         m_userAgentOverride = userAgent;
     63     }
     64 
     65 private:
     66     WebString m_userAgentOverride;
     67 };
     68 
     69 class FrameLoaderClientImplTest : public testing::Test {
     70 public:
     71     void SetUp()
     72     {
     73         m_webView = WebView::create(0);
     74         // FIXME: http://crbug.com/363843. This needs to find a better way to
     75         // not create graphics layers.
     76         m_webView->settings()->setAcceleratedCompositingEnabled(false);
     77         m_mainFrame = WebLocalFrame::create(&m_webFrameClient);
     78         m_webView->setMainFrame(m_mainFrame);
     79         m_frameLoaderClientImpl = toFrameLoaderClientImpl(toWebLocalFrameImpl(m_webView->mainFrame())->frame()->loader().client());
     80     }
     81 
     82     void TearDown()
     83     {
     84         m_webView->close();
     85         m_mainFrame->close();
     86     }
     87 
     88     void setUserAgentOverride(const WebString& userAgent)
     89     {
     90         return m_webFrameClient.setUserAgentOverride(userAgent);
     91     }
     92 
     93     const WebString userAgent()
     94     {
     95         // The test always returns the same user agent, regardless of the URL passed in.
     96         WebCore::KURL dummyURL(WebCore::ParsedURLString, "about:blank");
     97         WTF::CString userAgent = m_frameLoaderClientImpl->userAgent(dummyURL).utf8();
     98         return WebString::fromUTF8(userAgent.data(), userAgent.length());
     99     }
    100 
    101 protected:
    102     TestWebFrameClient m_webFrameClient;
    103     FrameLoaderClientImpl* m_frameLoaderClientImpl;
    104     WebView* m_webView;
    105     WebFrame* m_mainFrame;
    106 };
    107 
    108 TEST_F(FrameLoaderClientImplTest, UserAgentOverride)
    109 {
    110     const WebString defaultUserAgent = userAgent();
    111     const WebString override = WebString::fromUTF8("dummy override");
    112 
    113     // Override the user agent and make sure we get it back.
    114     setUserAgentOverride(override);
    115     EXPECT_TRUE(override.equals(userAgent()));
    116 
    117     // Remove the override and make sure we get the original back.
    118     setUserAgentOverride(WebString());
    119     EXPECT_TRUE(defaultUserAgent.equals(userAgent()));
    120 }
    121 
    122 } // namespace
    123