wavepacket-lib/lib/dialog/test/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Test for the dialog library.
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "common/wave_ex.h"
00036 #include "crypto/crypto.h"
00037 #include "datahash/datahash_text.h"
00038 #include "dialog/dialog.h"
00039 #include "dialog/request.h"
00040 #include "perf/perf.h"
00041 
00042 
00043 ////////////////////////////////////////////////////////////////////////////////
00044 //
00045 //      static helper methods
00046 //
00047 ////////////////////////////////////////////////////////////////////////////////
00048 
00049 class TestHost : public dialog::Host {
00050 public:
00051         ~TestHost(void) throw() { }
00052 
00053         // dialog::Host class interface methods --------------------------------
00054         void notifySubmit(IN const char * id,
00055                                 IN const Datahash * data) {
00056                         DPRINTF("Just got submit, id='%s'", id);
00057                         ASSERT2(data, "null");
00058                 }
00059 };
00060 
00061 
00062 
00063 static void
00064 doTest
00065 (
00066 IN int borderSize
00067 )
00068 {
00069         // construct request
00070         dialog::ContainerRequest req;
00071 
00072         req.addLabel("Hi there!  This is a really long label!  Yes!  Maybe....  Yes again!");
00073         req.addLabel("This is a much smaller label");
00074         req.addButton("sub-1", "Button 1");
00075         req.addTextbox("txt-1", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26, false);
00076         req.addTextbox("txt-2", "abcdefghijklmnopqrstuvwxyz", 26, true);
00077         req.addLabel("ending label");
00078 
00079         dialog::ContainerRequest subreq(dialog::ContainerRequest::eAxis_Horizontal);
00080         subreq.addLabel("horizontal");
00081         subreq.addButton("sub-2", "Button 2");
00082         subreq.addButton("sub-3", "Button 3");
00083         subreq.addTextbox("txt-3", "foobar", 6, false);
00084 
00085         req.addContainer(subreq);
00086 
00087         std::string data = req.get();
00088         DPRINTF("This is the dialog request string:\n%s", data.c_str());
00089 
00090         // convert to hash
00091         std::istringstream iss(data.c_str());
00092         smart_ptr<Datahash> hash = readHashFromStream("root", iss);
00093         ASSERT2(hash, "null");
00094 
00095         // create dialog manager
00096 //      smart_ptr<crypto::DESKey> des = crypto::DESKey::create();
00097 //      ASSERT2(des, "null");
00098         smart_ptr<crypto::DESKey> des = NULL;
00099 
00100         smart_ptr<dialog::TextDrawer> drawer =
00101             dialog::TextDrawer::create(borderSize);
00102         ASSERT2(drawer, "null");
00103 
00104         smart_ptr<dialog::Drawer> drawer2 = drawer;     // cast
00105         smart_ptr<dialog::Manager> mgr = dialog::Manager::create(des, drawer2);
00106         ASSERT2(mgr, "null");
00107 
00108         // create dialog
00109         smart_ptr<TestHost> host = new TestHost;
00110         ASSERT2(host, "null");
00111 
00112         ASSERT2(mgr->createDialog("test-1", 5, 20, hash, host),
00113             "Failed to create dialog");
00114 
00115         mgr->display(1024, 768);
00116 
00117         std::ostringstream oss;
00118         drawer->writeToStream(oss);
00119 
00120         DPRINTF("Output dialog:\n%s", oss.str().c_str());
00121 }
00122 
00123 
00124 
00125 ////////////////////////////////////////////////////////////////////////////////
00126 //
00127 //      entry point
00128 //
00129 ////////////////////////////////////////////////////////////////////////////////
00130 
00131 int
00132 main
00133 (
00134 IN int argc,
00135 IN const char * argv[]
00136 )
00137 {
00138         ASSERT(2 == argc,
00139             "Usage: dialog-test <border-size>");
00140         int borderSize = atoi(argv[1]);
00141         int retval = 0;
00142         try {
00143                 perf::Timer timer("test -- overall timer");
00144                 doTest(borderSize);
00145 
00146         } catch (std::exception& e) {
00147                 DPRINTF("Exception: %s", e.what());
00148                 retval = 1;
00149         } catch (...) {
00150                 DPRINTF("Unknown exception!");
00151                 retval = 2;
00152         }
00153         perf::dumpTimingSummary(std::cerr);
00154 
00155         return retval;
00156 }
00157