2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Unit test of Config component
21 * Created on: Mar 18, 2014
27 #include <boost/test/unit_test.hpp>
28 #include <boost/any.hpp>
30 #include "BrowserLogger.h"
33 BOOST_AUTO_TEST_SUITE(config)
35 BOOST_AUTO_TEST_CASE(config_simple_get_set)
37 std::unique_ptr<tizen_browser::config::DefaultConfig> defconf(new tizen_browser::config::DefaultConfig());
40 boost::any testvalue = defconf->get(std::string("testkey"));
41 BOOST_CHECK(testvalue.empty());
44 defconf->set("intTestKey", testval);
45 int retval = boost::any_cast<int>(defconf->get(std::string("intTestKey")));
46 BOOST_CHECK_EQUAL(testval, retval);
50 * This is test case of load and store methods.
52 BOOST_AUTO_TEST_CASE(config_load_store)
54 std::unique_ptr<tizen_browser::config::DefaultConfig> configuration1(new tizen_browser::config::DefaultConfig());
55 std::unique_ptr<tizen_browser::config::DefaultConfig> configuration2(new tizen_browser::config::DefaultConfig());
56 BOOST_CHECK(&configuration1);
57 std::string teststr("UseEFL");
58 configuration1->set("userInterface", teststr);
59 configuration1->store(std::string("config_file.cfg"));
60 configuration1.reset();
62 configuration2->load(std::string("config_file.cfg"));
64 std::string retstring;
66 retstring = boost::any_cast<std::string>(configuration2->get(std::string("userInterface")));
67 } catch(boost::bad_any_cast & e){
68 /// \todo Need to resolve bad type (void *) from boost::any(empty_string))
69 BROWSER_LOGI("[i] Catched error, msg: %s",e.what());
70 BROWSER_LOGI("[i] std::map not found map[key] and returns NULL to boost::any_cast as type (void*) instead of std::string (this case)\n");
72 /// \todo Below test should be enabled when saving and loading to/from config file will be implemented.
73 /// BOOST_CHECK_EQUAL(teststr, retstring);
74 configuration2.reset();
79 * This is test of boundary conditions
81 BOOST_AUTO_TEST_CASE(config_boundary_conditions)
83 std::unique_ptr<tizen_browser::config::DefaultConfig> configuration(new tizen_browser::config::DefaultConfig());
84 BOOST_CHECK(&configuration);
85 std::string retstring;
88 BOOST_CHECK(retstring.empty());
91 retany = configuration->get(NULL);
92 } catch(std::logic_error & e){
93 /// \todo get() function expects string and cannot construct empty string from NULL
94 BROWSER_LOGI("[i] Catched error, msg: %s",e.what());
95 BROWSER_LOGI("[i] get() function expects string and cannot construct empty string from NULL\n");
99 retstring = boost::any_cast<std::string>(retany);
100 BOOST_CHECK(retstring.empty());
101 }catch(boost::bad_any_cast & e){
102 /// \todo Need to resolve bad type (void *) from boost::any(empty_string))
103 BROWSER_LOGI("[i] Catched error, msg: %s",e.what());
104 BROWSER_LOGI("[i] std::map not found map[key] and returns NULL to boost::any_cast as type (void*) instead of std::string (this case)\n");
106 configuration->set(std::string(""), std::string("value"));
107 retstring = boost::any_cast<std::string>(configuration->get(std::string("")));
108 BOOST_CHECK_EQUAL(retstring, std::string("value"));
110 configuration->set(std::string(" "), std::string("anothervalue"));
111 retstring = boost::any_cast<std::string>(configuration->get(std::string(" ")));
112 BOOST_CHECK_EQUAL(retstring, std::string("anothervalue"));
114 configuration->set(std::string(" "), std::string("value3"));
115 retstring = boost::any_cast<std::string>(configuration->get(std::string(" ")));
116 BOOST_CHECK_EQUAL(retstring, std::string("value3"));
119 // NOTE Check that value is allowed to be empty.
120 configuration->set(std::string("TestKey"), std::string(""));
121 retstring = boost::any_cast<std::string>(configuration->get(std::string("TestKey")));
122 BOOST_CHECK(retstring.empty());
124 configuration->set(std::string("TestKey"), std::string(" "));
125 retstring = boost::any_cast<std::string>(configuration->get(std::string("TestKey")));
126 BOOST_CHECK(!retstring.empty());
128 configuration->set(std::string("AnotherTestKey"), std::string(" "));
129 retstring = boost::any_cast<std::string>(configuration->get(std::string("AnotherTestKey")));
130 BOOST_CHECK(!retstring.empty());
132 // Set two the same keys or values
133 // NOTE Check that key or value are allowed have duplicates. This test case not allowed duplicates
134 configuration->set(std::string("SameTestKey"), std::string("valueA"));
135 configuration->set(std::string("SameTestKey"), std::string("valueB"));
136 retstring = boost::any_cast<std::string>(configuration->get(std::string("SameTestKey")));
137 BOOST_CHECK_EQUAL(std::string("valueB"), retstring);
138 BOOST_CHECK_PREDICATE( std::not_equal_to<std::string>(), (retstring)(std::string("valueA")) );
141 BOOST_AUTO_TEST_SUITE_END()