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