File: shared_style_parser_observer_test.cc

package info (click to toggle)
libkml 1.3.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,660 kB
  • sloc: cpp: 48,088; python: 2,008; xml: 1,806; ansic: 1,766; php: 223; java: 195; ruby: 109; perl: 108; sh: 42; makefile: 17
file content (151 lines) | stat: -rw-r--r-- 6,994 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2008, Google Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice, 
//     this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//  3. Neither the name of Google Inc. nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// This file contains the unit tests for the SharedStyleParserObserver class.

#include "kml/engine/shared_style_parser_observer.h"
#include "boost/scoped_ptr.hpp"
#include "kml/dom/kml_factory.h"
#include "gtest/gtest.h"

namespace kmlengine {

class SharedStyleParserObserverTest : public testing::Test {
 protected:
  virtual void SetUp() {
    // Make some elements used in most tests.
    kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
    document_ = factory->CreateDocument();
    folder_ = factory->CreateFolder();
    kStyle0Id_ = "style0";
    style0_ = factory->CreateStyle();
    style0_->set_id(kStyle0Id_);
    kStyle1Id_ = "style1";
    style1_ = factory->CreateStyle();
    style1_->set_id(kStyle1Id_);
    kStyleMap0Id_ = "stylemap0";
    stylemap0_ = factory->CreateStyleMap();
    stylemap0_->set_id(kStyleMap0Id_);
    style_no_id_ = factory->CreateStyle();
    // Create a non-strict SharedStyleParserObserver.
    shared_style_parser_observer_.reset(
        new SharedStyleParserObserver(&shared_style_map_, false));
  }

  kmldom::DocumentPtr document_;
  kmldom::FolderPtr folder_;
  string kStyle0Id_;
  kmldom::StylePtr style0_;
  string kStyle1Id_;
  kmldom::StylePtr style1_;
  kmldom::StylePtr style_no_id_;
  string kStyleMap0Id_;
  kmldom::StyleMapPtr stylemap0_;
  SharedStyleMap shared_style_map_;
  boost::scoped_ptr<SharedStyleParserObserver> shared_style_parser_observer_;
};

// Verify that AddChild() accepts shared StyleSelectors.
TEST_F(SharedStyleParserObserverTest, TestAddChildSharedStyle) {
  // Verify that AddChild() does not detect a dupe.
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(document_, style0_));
  // Verify that there is only one entry in the map.
  ASSERT_EQ(static_cast<size_t>(1), shared_style_map_.size());

  // Verify that the id maps to the Style.
  kmldom::StyleSelectorPtr object = shared_style_map_[kStyle0Id_];
  ASSERT_EQ(kStyle0Id_, object->get_id());
  ASSERT_EQ(kmldom::Type_Style, object->Type());

  // Verify that AddChild() does not detect a dupe.
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(document_, stylemap0_));
  // Verify that there are now 2 entries in the map.
  ASSERT_EQ(static_cast<size_t>(2), shared_style_map_.size());
  // Verify that the id maps to the StyleMap
  object = shared_style_map_[kStyleMap0Id_];
  ASSERT_EQ(kStyleMap0Id_, object->get_id());
  ASSERT_EQ(kmldom::Type_StyleMap, object->Type());
}

// Verify that AddChild() does _not_ accept inline StyleSelectors.
TEST_F(SharedStyleParserObserverTest, TestAddChildInlineStyle) {
  // Verify that AddChild() does not detect a dupe.
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(folder_, style0_));
  // Verify that AddChild() does not detect a dupe even when passed the same
  // parent-child again.
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(folder_, style0_));
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(folder_, stylemap0_));
  // Verify that none of the above added anything to the map because although
  // these were both StyleSelectors with ids neither was a child of Document.
  ASSERT_TRUE(shared_style_map_.empty());
}

// Verify that NewElement() exists and alwyas returns true.
TEST_F(SharedStyleParserObserverTest, TestNewElement) {
  // Verify that NewElement() accepts StyleSelectors.
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(style0_));
  // Verify that a 2nd call succeeds given that NewElement is a nop
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(style0_));
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(style1_));
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(stylemap0_));
  // Style with no id accepted fine:
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(style_no_id_));
  // Document, Folder both fine...
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(folder_));
  ASSERT_TRUE(shared_style_parser_observer_->NewElement(document_));
}

// Verify that AddChild() properly detects a duplicate Object id when
// strict_parse is true.
TEST_F(SharedStyleParserObserverTest, TestAddChildDetectsDupeId) {
  boost::scoped_ptr<SharedStyleParserObserver> shared_style_parser_observer(
      new SharedStyleParserObserver(&shared_style_map_, true));
  // Pass a parent-child that will be added to the map.
  ASSERT_TRUE(shared_style_parser_observer->AddChild(document_, style0_));
  // Verify that AddChild() detected the dupe.
  ASSERT_FALSE(shared_style_parser_observer->AddChild(document_, style0_));
  // Verify that the map was not affected.
  ASSERT_EQ(static_cast<size_t>(1), shared_style_map_.size());
  kmldom::ObjectPtr object = shared_style_map_[kStyle0Id_];
  ASSERT_EQ(kStyle0Id_, object->get_id());
  ASSERT_EQ(kmldom::Type_Style, object->Type());
}

// Verify that the destructor does not effect the map.
TEST_F(SharedStyleParserObserverTest, TestDestructor) {
  // Use AddChild() to put some entries in the map.
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(document_, style0_));
  ASSERT_TRUE(shared_style_parser_observer_->AddChild(document_, style1_));

  // Verify that deleting the observer does not affect the map.
  shared_style_parser_observer_.reset();

  // Verify that the object map has exactly the 2 expected mappings.
  ASSERT_EQ(static_cast<size_t>(2), shared_style_map_.size());
  ASSERT_EQ(kStyle0Id_, shared_style_map_[kStyle0Id_]->get_id());
  ASSERT_EQ(kStyle1Id_, shared_style_map_[kStyle1Id_]->get_id());
}

}  // end namespace kmlengine