1/*
2 * Copyright (c) 2013, 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#include "core/html/LinkRelAttribute.h"
33#include "platform/RuntimeEnabledFeatures.h"
34
35#include "wtf/text/CString.h"
36#include <gtest/gtest.h>
37
38using namespace blink;
39
40namespace {
41
42class LinkRelAttributeTest : public testing::Test {
43protected:
44    virtual void SetUp()
45    {
46        m_touchIconLoadingEnabled = RuntimeEnabledFeatures::touchIconLoadingEnabled();
47    }
48
49    virtual void TearDown()
50    {
51        RuntimeEnabledFeatures::setTouchIconLoadingEnabled(m_touchIconLoadingEnabled);
52    }
53
54private:
55    bool m_touchIconLoadingEnabled;
56};
57
58static inline void testLinkRelAttribute(String value, bool isStyleSheet, IconType iconType, bool isAlternate, bool isDNSPrefetch, bool isLinkSubresource, bool isLinkPrerender, bool isImport = false)
59{
60    LinkRelAttribute linkRelAttribute(value);
61    ASSERT_EQ(isStyleSheet, linkRelAttribute.isStyleSheet()) << value.utf8().data();
62    ASSERT_EQ(iconType, linkRelAttribute.iconType()) << value.utf8().data();
63    ASSERT_EQ(isAlternate, linkRelAttribute.isAlternate()) << value.utf8().data();
64    ASSERT_EQ(isDNSPrefetch, linkRelAttribute.isDNSPrefetch()) << value.utf8().data();
65    ASSERT_EQ(isLinkSubresource, linkRelAttribute.isLinkSubresource()) << value.utf8().data();
66    ASSERT_EQ(isLinkPrerender, linkRelAttribute.isLinkPrerender()) << value.utf8().data();
67    ASSERT_EQ(isImport, linkRelAttribute.isImport()) << value.utf8().data();
68}
69
70TEST_F(LinkRelAttributeTest, Constructor)
71{
72    RuntimeEnabledFeatures::setTouchIconLoadingEnabled(false);
73
74    testLinkRelAttribute("stylesheet", true, InvalidIcon, false, false, false, false);
75    testLinkRelAttribute("sTyLeShEeT", true, InvalidIcon, false, false, false, false);
76
77    testLinkRelAttribute("icon", false, Favicon, false, false, false, false);
78    testLinkRelAttribute("iCoN", false, Favicon, false, false, false, false);
79    testLinkRelAttribute("shortcut icon", false, Favicon, false, false, false, false);
80    testLinkRelAttribute("sHoRtCuT iCoN", false, Favicon, false, false, false, false);
81
82    testLinkRelAttribute("dns-prefetch", false, InvalidIcon, false, true, false, false);
83    testLinkRelAttribute("dNs-pReFeTcH", false, InvalidIcon, false, true, false, false);
84
85    testLinkRelAttribute("apple-touch-icon", false, InvalidIcon, false, false, false, false);
86    testLinkRelAttribute("aPpLe-tOuCh-IcOn", false, InvalidIcon, false, false, false, false);
87    testLinkRelAttribute("apple-touch-icon-precomposed", false, InvalidIcon, false, false, false, false);
88    testLinkRelAttribute("aPpLe-tOuCh-IcOn-pReCoMpOsEd", false, InvalidIcon, false, false, false, false);
89
90    testLinkRelAttribute("alternate stylesheet", true, InvalidIcon, true, false, false, false);
91    testLinkRelAttribute("stylesheet alternate", true, InvalidIcon, true, false, false, false);
92    testLinkRelAttribute("aLtErNaTe sTyLeShEeT", true, InvalidIcon, true, false, false, false);
93    testLinkRelAttribute("sTyLeShEeT aLtErNaTe", true, InvalidIcon, true, false, false, false);
94
95    testLinkRelAttribute("stylesheet icon prerender aLtErNaTe", true, Favicon, true, false, false, true);
96    testLinkRelAttribute("alternate subresource", false, InvalidIcon, true, false, true, false);
97    testLinkRelAttribute("alternate icon stylesheet", true, Favicon, true, false, false, false);
98
99    testLinkRelAttribute("import", false, InvalidIcon, false, false, false, false, true);
100    // "import" is mutually exclusive and "stylesheet" wins when they conflict.
101    testLinkRelAttribute("stylesheet import", true, InvalidIcon, false, false, false, false, false);
102}
103
104TEST_F(LinkRelAttributeTest, ConstructorTouchIconLoadingEnabled)
105{
106    RuntimeEnabledFeatures::setTouchIconLoadingEnabled(true);
107
108    testLinkRelAttribute("stylesheet", true, InvalidIcon, false, false, false, false);
109    testLinkRelAttribute("sTyLeShEeT", true, InvalidIcon, false, false, false, false);
110
111    testLinkRelAttribute("icon", false, Favicon, false, false, false, false);
112    testLinkRelAttribute("iCoN", false, Favicon, false, false, false, false);
113    testLinkRelAttribute("shortcut icon", false, Favicon, false, false, false, false);
114    testLinkRelAttribute("sHoRtCuT iCoN", false, Favicon, false, false, false, false);
115
116    testLinkRelAttribute("dns-prefetch", false, InvalidIcon, false, true, false, false);
117    testLinkRelAttribute("dNs-pReFeTcH", false, InvalidIcon, false, true, false, false);
118    testLinkRelAttribute("alternate dNs-pReFeTcH", false, InvalidIcon, true, true, false, false);
119
120    testLinkRelAttribute("apple-touch-icon", false, TouchIcon, false, false, false, false);
121    testLinkRelAttribute("aPpLe-tOuCh-IcOn", false, TouchIcon, false, false, false, false);
122    testLinkRelAttribute("apple-touch-icon-precomposed", false, TouchPrecomposedIcon, false, false, false, false);
123    testLinkRelAttribute("aPpLe-tOuCh-IcOn-pReCoMpOsEd", false, TouchPrecomposedIcon, false, false, false, false);
124
125    testLinkRelAttribute("alternate stylesheet", true, InvalidIcon, true, false, false, false);
126    testLinkRelAttribute("stylesheet alternate", true, InvalidIcon, true, false, false, false);
127    testLinkRelAttribute("aLtErNaTe sTyLeShEeT", true, InvalidIcon, true, false, false, false);
128    testLinkRelAttribute("sTyLeShEeT aLtErNaTe", true, InvalidIcon, true, false, false, false);
129
130    testLinkRelAttribute("stylesheet icon prerender aLtErNaTe", true, Favicon, true, false, false, true);
131    testLinkRelAttribute("alternate subresource", false, InvalidIcon, true, false, true, false);
132    testLinkRelAttribute("alternate icon stylesheet", true, Favicon, true, false, false, false);
133
134    testLinkRelAttribute("import", false, InvalidIcon, false, false, false, false, true);
135    testLinkRelAttribute("alternate import", false, InvalidIcon, true, false, false, false, true);
136    testLinkRelAttribute("stylesheet import", true, InvalidIcon, false, false, false, false, false);
137}
138
139} // namespace
140