PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  28. #include "llsdserialize.h"
  29. #include "../llcontrol.h"
  30. #include "../test/lltut.h"
  31. namespace tut
  32. {
  33. struct control_group
  34. {
  35. LLControlGroup* mCG;
  36. std::string mTestConfigDir;
  37. std::string mTestConfigFile;
  38. static bool mListenerFired;
  39. control_group()
  40. {
  41. mCG = new LLControlGroup("foo");
  42. LLUUID random;
  43. random.generate();
  44. // generate temp dir
  45. std::ostringstream oStr;
  46. #ifdef LL_WINDOWS
  47. char* tmp_dir = getenv("TMP");
  48. if(tmp_dir)
  49. {
  50. oStr << tmp_dir << "/llcontrol-test-" << random << "/";
  51. }
  52. else
  53. {
  54. oStr << "c:/tmp/llcontrol-test-" << random << "/";
  55. }
  56. #else
  57. oStr << "/tmp/llcontrol-test-" << random << "/";
  58. #endif
  59. mTestConfigDir = oStr.str();
  60. mTestConfigFile = mTestConfigDir + "settings.xml";
  61. LLFile::mkdir(mTestConfigDir);
  62. LLSD config;
  63. config["TestSetting"]["Comment"] = "Dummy setting used for testing";
  64. config["TestSetting"]["Persist"] = 1;
  65. config["TestSetting"]["Type"] = "U32";
  66. config["TestSetting"]["Value"] = 12;
  67. writeSettingsFile(config);
  68. }
  69. ~control_group()
  70. {
  71. //Remove test files
  72. delete mCG;
  73. }
  74. void writeSettingsFile(const LLSD& config)
  75. {
  76. llofstream file(mTestConfigFile);
  77. if (file.is_open())
  78. {
  79. LLSDSerialize::toPrettyXML(config, file);
  80. }
  81. file.close();
  82. }
  83. static bool handleListenerTest()
  84. {
  85. control_group::mListenerFired = true;
  86. return true;
  87. }
  88. };
  89. bool control_group::mListenerFired = false;
  90. typedef test_group<control_group> control_group_test;
  91. typedef control_group_test::object control_group_t;
  92. control_group_test tut_control_group("control_group");
  93. //load settings from files - LLSD
  94. template<> template<>
  95. void control_group_t::test<1>()
  96. {
  97. int results = mCG->loadFromFile(mTestConfigFile.c_str());
  98. ensure("number of settings", (results == 1));
  99. ensure("value of setting", (mCG->getU32("TestSetting") == 12));
  100. }
  101. //save settings to files
  102. template<> template<>
  103. void control_group_t::test<2>()
  104. {
  105. int results = mCG->loadFromFile(mTestConfigFile.c_str());
  106. mCG->setU32("TestSetting", 13);
  107. ensure_equals("value of changed setting", mCG->getU32("TestSetting"), 13);
  108. LLControlGroup test_cg("foo2");
  109. std::string temp_test_file = (mTestConfigDir + "setting_llsd_temp.xml");
  110. mCG->saveToFile(temp_test_file.c_str(), TRUE);
  111. results = test_cg.loadFromFile(temp_test_file.c_str());
  112. ensure("number of changed settings loaded", (results == 1));
  113. ensure("value of changed settings loaded", (test_cg.getU32("TestSetting") == 13));
  114. }
  115. //priorities
  116. template<> template<>
  117. void control_group_t::test<3>()
  118. {
  119. int results = mCG->loadFromFile(mTestConfigFile.c_str());
  120. LLControlVariable* control = mCG->getControl("TestSetting");
  121. LLSD new_value = 13;
  122. control->setValue(new_value, FALSE);
  123. ensure_equals("value of changed setting", mCG->getU32("TestSetting"), 13);
  124. LLControlGroup test_cg("foo3");
  125. std::string temp_test_file = (mTestConfigDir + "setting_llsd_persist_temp.xml");
  126. mCG->saveToFile(temp_test_file.c_str(), TRUE);
  127. results = test_cg.loadFromFile(temp_test_file.c_str());
  128. //If we haven't changed any settings, then we shouldn't have any settings to load
  129. ensure("number of non-persisted changed settings loaded", (results == 0));
  130. }
  131. //listeners
  132. template<> template<>
  133. void control_group_t::test<4>()
  134. {
  135. int results = mCG->loadFromFile(mTestConfigFile.c_str());
  136. ensure("number of settings", (results == 1));
  137. mCG->getControl("TestSetting")->getSignal()->connect(boost::bind(&this->handleListenerTest));
  138. mCG->setU32("TestSetting", 13);
  139. ensure("listener fired on changed setting", mListenerFired);
  140. }
  141. }