PageRenderTime 37ms CodeModel.GetById 13ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llxml/tests/llcontrol_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 155 lines | 108 code | 14 blank | 33 comment | 8 complexity | 0487fc051c6b3c54e26fabeb3583c5da MD5 | raw file
  1/** 
  2 * @file llcontrol_tut.cpp
  3 * @date   February 2008
  4 * @brief control group unit tests
  5 *
  6 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include "linden_common.h"
 29#include "llsdserialize.h"
 30
 31#include "../llcontrol.h"
 32
 33#include "../test/lltut.h"
 34
 35namespace tut
 36{
 37
 38	struct control_group
 39	{
 40		LLControlGroup* mCG;
 41		std::string mTestConfigDir;
 42		std::string mTestConfigFile;
 43		static bool mListenerFired;
 44		control_group()
 45		{
 46			mCG = new LLControlGroup("foo");
 47			LLUUID random;
 48			random.generate();
 49			// generate temp dir
 50			std::ostringstream oStr;
 51
 52#ifdef LL_WINDOWS
 53			char* tmp_dir = getenv("TMP");
 54			if(tmp_dir)
 55			{
 56				oStr << tmp_dir << "/llcontrol-test-" << random << "/";
 57			}
 58			else
 59			{
 60				oStr << "c:/tmp/llcontrol-test-" << random << "/";
 61			}
 62#else
 63			oStr << "/tmp/llcontrol-test-" << random << "/";
 64#endif
 65
 66			mTestConfigDir = oStr.str();
 67			mTestConfigFile = mTestConfigDir + "settings.xml";
 68			LLFile::mkdir(mTestConfigDir);
 69			LLSD config;
 70			config["TestSetting"]["Comment"] = "Dummy setting used for testing";
 71			config["TestSetting"]["Persist"] = 1;
 72			config["TestSetting"]["Type"] = "U32";
 73			config["TestSetting"]["Value"] = 12;
 74			writeSettingsFile(config);
 75		}
 76		~control_group()
 77		{
 78			//Remove test files
 79			delete mCG;
 80		}
 81		void writeSettingsFile(const LLSD& config)
 82		{
 83			llofstream file(mTestConfigFile);
 84			if (file.is_open())
 85			{
 86				LLSDSerialize::toPrettyXML(config, file);
 87			}
 88			file.close();
 89		}
 90		static bool handleListenerTest()
 91		{
 92			control_group::mListenerFired = true;
 93			return true;
 94		}
 95	};
 96
 97	bool control_group::mListenerFired = false;
 98
 99	typedef test_group<control_group> control_group_test;
100	typedef control_group_test::object control_group_t;
101	control_group_test tut_control_group("control_group");
102
103	//load settings from files - LLSD
104	template<> template<>
105	void control_group_t::test<1>()
106	{
107		int results = mCG->loadFromFile(mTestConfigFile.c_str());
108		ensure("number of settings", (results == 1));
109		ensure("value of setting", (mCG->getU32("TestSetting") == 12));
110	}
111
112	//save settings to files
113	template<> template<>
114	void control_group_t::test<2>()
115	{
116		int results = mCG->loadFromFile(mTestConfigFile.c_str());
117		mCG->setU32("TestSetting", 13);
118		ensure_equals("value of changed setting", mCG->getU32("TestSetting"), 13);
119		LLControlGroup test_cg("foo2");
120		std::string temp_test_file = (mTestConfigDir + "setting_llsd_temp.xml");
121		mCG->saveToFile(temp_test_file.c_str(), TRUE);
122		results = test_cg.loadFromFile(temp_test_file.c_str());
123		ensure("number of changed settings loaded", (results == 1));
124		ensure("value of changed settings loaded", (test_cg.getU32("TestSetting") == 13));
125	}
126   
127	//priorities
128	template<> template<>
129	void control_group_t::test<3>()
130	{
131		int results = mCG->loadFromFile(mTestConfigFile.c_str());
132		LLControlVariable* control = mCG->getControl("TestSetting");
133		LLSD new_value = 13;
134		control->setValue(new_value, FALSE);
135		ensure_equals("value of changed setting", mCG->getU32("TestSetting"), 13);
136		LLControlGroup test_cg("foo3");
137		std::string temp_test_file = (mTestConfigDir + "setting_llsd_persist_temp.xml");
138		mCG->saveToFile(temp_test_file.c_str(), TRUE);
139		results = test_cg.loadFromFile(temp_test_file.c_str());
140		//If we haven't changed any settings, then we shouldn't have any settings to load
141		ensure("number of non-persisted changed settings loaded", (results == 0));
142	}
143
144	//listeners
145	template<> template<>
146	void control_group_t::test<4>()
147	{
148		int results = mCG->loadFromFile(mTestConfigFile.c_str());
149		ensure("number of settings", (results == 1));
150		mCG->getControl("TestSetting")->getSignal()->connect(boost::bind(&this->handleListenerTest));
151		mCG->setU32("TestSetting", 13);
152		ensure("listener fired on changed setting", mListenerFired);	   
153	}
154
155}