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 #include "kml/dom/stylemap.h"
27 #include "kml/dom/kml_factory.h"
28 #include "kml/dom/kml_ptr.h"
29 #include "gtest/gtest.h"
30 
31 namespace kmldom {
32 
33 class PairTest : public testing::Test {
34  protected:
SetUp()35   virtual void SetUp() {
36     pair_ = KmlFactory::GetFactory()->CreatePair();
37   }
38 
39   PairPtr pair_;
40 };
41 
TEST_F(PairTest,TestType)42 TEST_F(PairTest, TestType) {
43   ASSERT_EQ(Type_Pair, pair_->Type());
44   ASSERT_TRUE(pair_->IsA(Type_Pair));
45   ASSERT_TRUE(pair_->IsA(Type_Object));
46 }
47 
48 // Verify proper defaults:
TEST_F(PairTest,TestDefaults)49 TEST_F(PairTest, TestDefaults) {
50   ASSERT_FALSE(pair_->has_key());
51   ASSERT_EQ(STYLESTATE_NORMAL, pair_->get_key());
52   ASSERT_FALSE(pair_->has_styleurl());
53   ASSERT_EQ(string(""), pair_->get_styleurl());
54   ASSERT_FALSE(pair_->has_styleselector());
55   ASSERT_TRUE(NULL == pair_->get_styleselector());
56 }
57 
58 // Verify setting default makes has_xxx() true:
TEST_F(PairTest,TestSetToDefaultValues)59 TEST_F(PairTest, TestSetToDefaultValues) {
60   pair_->set_key(pair_->get_key());
61   ASSERT_TRUE(pair_->has_key());
62   pair_->set_styleurl(pair_->get_styleurl());
63   ASSERT_TRUE(pair_->has_styleurl());
64   pair_->set_styleselector(NULL);
65   ASSERT_FALSE(pair_->has_styleselector()); // ptr is null
66 }
67 
68 // Verify set, get, has, clear:
TEST_F(PairTest,TestSetGetHasClear)69 TEST_F(PairTest, TestSetGetHasClear) {
70   // Non-default values:
71   StyleStateEnum key = STYLESTATE_HIGHLIGHT;
72   string styleurl("#url");
73   StylePtr styleselector(KmlFactory::GetFactory()->CreateStyle());
74 
75   // Set all fields:
76   pair_->set_key(key);
77   pair_->set_styleurl(styleurl);
78   pair_->set_styleselector(styleselector);
79 
80   // Verify getter and has_xxx():
81   ASSERT_TRUE(pair_->has_key());
82   ASSERT_EQ(key, pair_->get_key());
83   ASSERT_TRUE(pair_->has_styleurl());
84   ASSERT_EQ(styleurl, pair_->get_styleurl());
85   ASSERT_TRUE(pair_->has_styleselector());
86   ASSERT_EQ(styleselector, pair_->get_styleselector());
87 
88   // Clear all fields:
89   pair_->clear_key();
90   pair_->clear_styleurl();
91   pair_->clear_styleselector();
92 }
93 
94 class StyleMapTest : public testing::Test {
95  protected:
SetUp()96   virtual void SetUp() {
97     stylemap_ = KmlFactory::GetFactory()->CreateStyleMap();
98   }
99 
100   StyleMapPtr stylemap_;
101 };
102 
TEST_F(StyleMapTest,TestType)103 TEST_F(StyleMapTest, TestType) {
104   ASSERT_EQ(Type_StyleMap, stylemap_->Type());
105   ASSERT_TRUE(stylemap_->IsA(Type_StyleMap));
106   ASSERT_TRUE(stylemap_->IsA(Type_StyleSelector));
107   ASSERT_TRUE(stylemap_->IsA(Type_Object));
108 }
109 
TEST_F(StyleMapTest,TestLists)110 TEST_F(StyleMapTest, TestLists) {
111   ASSERT_EQ(static_cast<size_t>(0), stylemap_->get_pair_array_size());
112   stylemap_->add_pair(KmlFactory::GetFactory()->CreatePair());
113   stylemap_->add_pair(KmlFactory::GetFactory()->CreatePair());
114   ASSERT_EQ(static_cast<size_t>(2), stylemap_->get_pair_array_size());
115   ASSERT_EQ(Type_Pair, stylemap_->get_pair_array_at(0)->Type());
116   ASSERT_EQ(Type_Pair, stylemap_->get_pair_array_at(1)->Type());
117 }
118 
119 }  // end namespace kmldom
120