1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 // This file contains the unit tests for the location utility functions.
27
28 #include "kml/engine/link_util.h"
29 #include "kml/dom.h"
30 #include "kml/base/net_cache_test_util.h"
31 #include "gtest/gtest.h"
32 #include "kml/engine/kml_cache.h"
33 #include "kml/engine/feature_visitor.h"
34 #include "kml/engine/find.h"
35
36 // The following define is a convenience for testing inside Google.
37 #ifdef GOOGLE_INTERNAL
38 #include "kml/base/google_internal_test.h"
39 #endif
40
41 #ifndef DATADIR
42 #error *** DATADIR must be defined! ***
43 #endif
44
45 using kmldom::GroundOverlayPtr;
46 using kmldom::IconPtr;
47 using kmldom::IconStylePtr;
48 using kmldom::IconStyleIconPtr;
49 using kmldom::ItemIconPtr;
50 using kmldom::KmlFactory;
51 using kmldom::LinkPtr;
52 using kmldom::ModelPtr;
53 using kmldom::NetworkLinkPtr;
54 using kmldom::OverlayPtr;
55 using kmldom::PhotoOverlayPtr;
56 using kmldom::ScreenOverlayPtr;
57
58 namespace kmlengine {
59
60 const char* kGroundOverlayHref = "ground.jpg";
61 const char* kIconHref = "goo.jpg";
62 const char* kIconStyleHref = "icon.png";
63 const char* kItemIconHref = "itemicon.png";
64 const char* kLinkHref = "foo.kml";
65 const char* kNetworkLinkHref = "http://example.com/cool/stuff/here.kml";
66 const char* kModelHref = "foo.kmz/hi.dae";
67 const char* kScreenOverlayHref = "screen.jpg";
68 const char* kPhotoOverlayHref = "photo.jpg";
69
SetOverlayIconHref(OverlayPtr overlay,const char * href)70 static void SetOverlayIconHref(OverlayPtr overlay, const char* href) {
71 IconPtr icon = KmlFactory::GetFactory()->CreateIcon();
72 icon->set_href(href);
73 overlay->set_icon(icon);
74 }
75
76 template<typename LP>
SetLinkParentLinkHref(LP link_parent,const char * href)77 static void SetLinkParentLinkHref(LP link_parent, const char* href) {
78 LinkPtr link = KmlFactory::GetFactory()->CreateLink();
79 link->set_href(href);
80 link_parent->set_icon(link);
81 }
82
83 class LinkUtilTest : public testing::Test {
84 protected:
SetUp()85 virtual void SetUp() {
86 factory_ = KmlFactory::GetFactory();
87 groundoverlay_ = factory_->CreateGroundOverlay();
88 SetOverlayIconHref(groundoverlay_, kGroundOverlayHref);
89 icon_ = factory_->CreateIcon();
90 icon_->set_href(kIconHref);
91 iconstyle_ = factory_->CreateIconStyle();
92 IconStyleIconPtr iconstyleicon = factory_->CreateIconStyleIcon();
93 iconstyleicon->set_href(kIconStyleHref);
94 iconstyle_->set_icon(iconstyleicon);
95 iconstyleicon_ = factory_->CreateIconStyleIcon();
96 itemicon_ = factory_->CreateItemIcon();
97 itemicon_->set_href(kItemIconHref);
98 link_ = factory_->CreateLink();
99 link_->set_href(kLinkHref);
100 model_ = factory_->CreateModel();
101 networklink_ = factory_->CreateNetworkLink();
102 photooverlay_ = factory_->CreatePhotoOverlay();
103 SetOverlayIconHref(photooverlay_, kPhotoOverlayHref);
104 screenoverlay_ = factory_->CreateScreenOverlay();
105 SetOverlayIconHref(screenoverlay_, kScreenOverlayHref);
106 kml_cache_.reset(new KmlCache(&testdata_net_fetcher_, 1));
107 }
108
109 void Init();
110 GroundOverlayPtr groundoverlay_;
111 KmlFactory* factory_;
112 IconPtr icon_;
113 IconStylePtr iconstyle_;
114 IconStyleIconPtr iconstyleicon_;
115 ItemIconPtr itemicon_;
116 LinkPtr link_;
117 ModelPtr model_;
118 NetworkLinkPtr networklink_;
119 PhotoOverlayPtr photooverlay_;
120 ScreenOverlayPtr screenoverlay_;
121 kmlbase::TestDataNetFetcher testdata_net_fetcher_;
122 boost::scoped_ptr<KmlCache> kml_cache_;
123 };
124
125 template<typename HP>
VerifyGetHref(const HP & href_parent,const char * want_href)126 static void VerifyGetHref(const HP& href_parent, const char* want_href) {
127 string got_href;
128 ASSERT_TRUE(GetHref(href_parent, &got_href));
129 ASSERT_EQ(string(want_href), got_href);
130 }
131
132 // This tests the GetHref() function template.
TEST_F(LinkUtilTest,TestGetHref)133 TEST_F(LinkUtilTest, TestGetHref) {
134 VerifyGetHref(itemicon_, kItemIconHref);
135 VerifyGetHref(link_, kLinkHref);
136 VerifyGetHref(icon_, kIconHref);
137 }
138
139 template<typename HP>
VerifyGetIconParentHref(const HP & icon_parent,const char * want_href)140 static void VerifyGetIconParentHref(const HP& icon_parent,
141 const char* want_href) {
142 string got_href;
143 ASSERT_TRUE(GetIconParentHref(icon_parent, &got_href));
144 ASSERT_EQ(string(want_href), got_href);
145 }
146
147 // This tests the GetIconParentHref() function template.
TEST_F(LinkUtilTest,TestGetIconParentHref)148 TEST_F(LinkUtilTest, TestGetIconParentHref) {
149 VerifyGetIconParentHref(groundoverlay_, kGroundOverlayHref);
150 VerifyGetIconParentHref(photooverlay_, kPhotoOverlayHref);
151 VerifyGetIconParentHref(screenoverlay_, kScreenOverlayHref);
152 VerifyGetIconParentHref(iconstyle_, kIconStyleHref);
153 }
154
155 template<typename LP>
VerifyGetLinkParentHref(const LP & link_parent,const char * want_href)156 static void VerifyGetLinkParentHref(const LP& link_parent,
157 const char* want_href) {
158 string got_href;
159 ASSERT_TRUE(GetLinkParentHref(link_parent, &got_href));
160 ASSERT_EQ(string(want_href), got_href);
161 }
162
163 template<typename LP>
SetLinkHref(const LP & link_parent,LinkPtr link,const string & href)164 static void SetLinkHref(const LP& link_parent, LinkPtr link,
165 const string& href) {
166 link->set_href(href);
167 link_parent->set_link(link);
168 }
169
170 // This tests the GetLinkParentHref() function template.
TEST_F(LinkUtilTest,TestGetLinkParentHref)171 TEST_F(LinkUtilTest, TestGetLinkParentHref) {
172 SetLinkHref(networklink_, factory_->CreateLink(), kNetworkLinkHref);
173 SetLinkHref(model_, factory_->CreateLink(), kModelHref);
174 VerifyGetLinkParentHref(networklink_, kNetworkLinkHref);
175 VerifyGetLinkParentHref(model_, kModelHref);
176 }
177
TEST_F(LinkUtilTest,TestFetchLink)178 TEST_F(LinkUtilTest, TestFetchLink) {
179 const string kBase("http://host.com/kmz/radar-animation.kmz");
180 KmlFilePtr base_kml_file = kml_cache_->FetchKmlAbsolute(kBase);
181 ASSERT_TRUE(base_kml_file);
182 ElementVector networklink_vector;
183 GetElementsById(base_kml_file->get_root(), kmldom::Type_NetworkLink,
184 &networklink_vector);
185 // The default KML file in radar-animation.kmz has 1 NetworkLink.
186 ASSERT_EQ(static_cast<size_t>(1), networklink_vector.size());
187 KmlFilePtr target_kml_file = FetchLink(base_kml_file,
188 AsNetworkLink(networklink_vector[0]));
189 ASSERT_TRUE(target_kml_file);
190 kmldom::DocumentPtr document =
191 AsDocument(GetRootFeature(target_kml_file->get_root()));
192 ASSERT_TRUE(document);
193 // This is kmz/radar-animation.kmz/level00/0.kml.
194 ASSERT_EQ(string("0130_256_-1"), document->get_name());
195 }
196
TEST_F(LinkUtilTest,TestFetchIcon)197 TEST_F(LinkUtilTest, TestFetchIcon) {
198 const string kBase("http://host.com/kmz/rumsey/kml/lc01.kmz");
199 KmlFilePtr kml_file = kml_cache_->FetchKmlAbsolute(kBase);
200 ASSERT_TRUE(kml_file);
201 ElementVector groundoverlay_vector;
202 GetElementsById(kml_file->get_root(), kmldom::Type_GroundOverlay,
203 &groundoverlay_vector);
204 // The default KML file in lc01.kmz has 2 GroundOverlays.
205 ASSERT_EQ(static_cast<size_t>(2), groundoverlay_vector.size());
206 string data;
207 ASSERT_TRUE(FetchIcon(kml_file, AsGroundOverlay(groundoverlay_vector[0]),
208 &data));
209 // This is kmz/rumsey/imagery/01_4.png
210 ASSERT_EQ(static_cast<size_t>(6742), data.size());
211 ASSERT_TRUE(FetchIcon(kml_file, AsGroundOverlay(groundoverlay_vector[1]),
212 &data));
213 // This is kmz/rumsey/imagery/01_8.png
214 ASSERT_EQ(static_cast<size_t>(7364), data.size());
215 }
216
217 } // end namespace kmlengine
218