tizen 2.3 release
[framework/web/wearable/wrt-security.git] / tests / ace / AttributeSetter.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    AttributeSetter.h
18  * @author  Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version 0.1
20  * @brief   Stub for attribute_facade.h
21  */
22 #ifndef _TEST_ACE_TESTS_ATTRIBUTE_SETTER_
23 #define _TEST_ACE_TESTS_ATTRIBUTE_SETTER_
24
25 #include <list>
26 #include <map>
27 #include <string>
28
29 #include <dpl/singleton.h>
30 #include <dpl/foreach.h>
31 #include <dpl/log/log.h>
32
33 #include <ace/Request.h>
34 #include <ace/WRT_INTERFACE.h>
35
36 class AttributeSetter {
37 public:
38     typedef std::list<std::string> ValueList;
39     typedef std::map<std::string, ValueList> AttributeMap;
40     typedef std::map<int, AttributeMap> CollectionHandlerMap;
41
42     typedef std::list<ATTRIBUTE> Evil;
43
44     void get(const Request &request,
45             const Evil* evil)
46     {
47         int handle = request.getWidgetHandle();
48
49         CollectionHandlerMap::iterator widgetContextIt =
50             m_collectionMap.find(handle);
51
52         if (widgetContextIt == m_collectionMap.end()) {
53             LogError("WidgetContext is empty");
54             return;
55         }
56
57         FOREACH(it, *evil)
58         {
59             std::string attrName = *(it->first);
60
61             if (attrName == "device-cap") {
62                 Request::DeviceCapabilitySet devCapSet =
63                     request.getDeviceCapabilitySet();
64                 std::copy(devCapSet.begin(),
65                           devCapSet.end(),
66                           std::back_inserter(*(it->second)));
67                 continue;
68             }
69
70             AttributeMap::iterator attrIt =
71                 widgetContextIt->second.find(attrName);
72
73             if (attrIt == widgetContextIt->second.end()) {
74                 LogError("No attribute: " << attrName <<
75                     " in context nr: " << handle);
76                 const ATTRIBUTE &a = *it;
77                 const_cast<ATTRIBUTE&>(a).second = NULL;
78                 continue;
79             }
80
81             ValueList valueList = attrIt->second;
82
83             std::copy(valueList.begin(),
84                       valueList.end(),
85                       std::back_inserter(*(it->second)));
86         }
87     }
88
89     void addValue(int handler,
90                   const std::string &attributeName,
91                   const std::string &attributeValue)
92     {
93         m_collectionMap[handler][attributeName].push_back(attributeValue);
94     }
95
96     std::string getValue(int handler, const std::string &attributeName)
97     {
98         std::list<std::string> lst = m_collectionMap[handler][attributeName];
99         if (lst.empty())
100             return "fake-device-name-that-should-not-match-with-anything";
101         return lst.front();
102     }
103
104     void addValue(int handler,
105                   const char *attributeName,
106                   const char *attributeValue)
107     {
108         std::string aN = attributeName;
109         std::string aV = attributeValue;
110         addValue(handler, aN, aV);
111     }
112
113     void clear() {
114         m_collectionMap.clear();
115     }
116 protected:
117     AttributeSetter(){}
118     ~AttributeSetter(){}
119 private:
120     CollectionHandlerMap m_collectionMap;
121 };
122
123 typedef DPL::Singleton<AttributeSetter> AttributeSetterSingleton;
124
125 #endif // _TEST_ACE_TESTS_ATTRIBUTE_SETTER_