1
2 /*********************************************************
3 * Soothsayer, an extensible predictive text entry system
4 * ------------------------------------------------------
5 *
6 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 **********(*)*/
23
24 #include <profileManagerTest.h>
25 #include <fstream>
26
27 CPPUNIT_TEST_SUITE_REGISTRATION( ProfileManagerTest );
28
setUp()29 void ProfileManagerTest::setUp()
30 {
31 profileManager = new ProfileManager();
32 profile = 0;
33
34 contextTracker = 0;
35 selector = 0;
36 predictor = 0;
37 }
38
tearDown()39 void ProfileManagerTest::tearDown()
40 {
41 delete predictor;
42 delete selector;
43 delete contextTracker;
44
45 delete profile;
46 delete profileManager;
47 }
48
testDefaultProfile()49 void ProfileManagerTest::testDefaultProfile()
50 {
51
52 std::cout << "ProfileManagerTest::testDefaultProfile()" << std::endl;
53 profileManager->buildProfile();
54 profile = profileManager->getProfile();
55 configuration = profile->get_configuration();
56
57 contextTracker = new ContextTracker(configuration);
58 selector = new Selector(configuration, contextTracker);
59 predictor = new Predictor(configuration, contextTracker);
60
61 std::cout << "ProfileManagerTest: before testProfile()" << std::endl;
62 testProfile();
63 std::cout << "Exiting ProfileManagerTest::testDefaultProfile( ...)" << std::endl;
64
65 }
66
testNonExistantProfile()67 void ProfileManagerTest::testNonExistantProfile()
68 {
69 std::cout << "ProfileManagerTest::testNonExistantProfile()" << std::endl;
70
71 // hopefully a file with the following name will not exists
72 const std::string wacky_profile("this_IS_a_wAckY_profileName.xml");
73
74 CPPUNIT_ASSERT( !profileManager->loadProfile(wacky_profile) );
75
76 /* This block of code was commented out because ProfileManager
77 * will not build a default profile when the specified profile
78 * cannot be loaded.
79
80 profile = profileManager->getProfile();
81
82 contextTracker = new ContextTracker(profile);
83 selector = new Selector(profile, contextTracker);
84 predictor = new Predictor(profile, contextTracker);
85
86 testProfile();
87 */
88 }
89
testProfile()90 void ProfileManagerTest::testProfile()
91 {
92 // test init contextTracker
93 CPPUNIT_ASSERT_EQUAL(DEFAULT_MAX_BUFFER_SIZE,
94 contextTracker->getMaxBufferSize());
95
96 // test init predictor
97 CPPUNIT_ASSERT_EQUAL(DEFAULT_PREDICT_TIME,
98 predictor->getPredictTime());
99 CPPUNIT_ASSERT_EQUAL(DEFAULT_COMBINATION_POLICY,
100 predictor->getCombinationPolicy());
101
102 // test init selector
103 CPPUNIT_ASSERT_EQUAL(DEFAULT_SUGGESTIONS,
104 selector->suggestions());
105 CPPUNIT_ASSERT_EQUAL(DEFAULT_REPEAT_SUGGESTION,
106 selector->repeat_suggestions());
107 CPPUNIT_ASSERT_EQUAL(DEFAULT_GREEDY_SUGGESTION_THRESHOLD,
108 selector->greedy_suggestion_threshold());
109 }
110
testCustomProfile()111 void ProfileManagerTest::testCustomProfile()
112 {
113 std::cout << "ProfileManagerTest::testCustomProfile()" << std::endl;
114
115 // create custom profile
116 const std::string custom_profile = "custom_profile.xml";
117 std::ofstream profile_stream(custom_profile.c_str());
118 profile_stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
119 << "<Soothsayer>\n"
120 << " <ProfileManager>\n"
121 << " <LOGGER>ERROR</LOGGER>\n"
122 << " </ProfileManager>\n"
123 << " <Custom>CUSTOM</Custom>\n"
124 << "</Soothsayer>\n";
125 profile_stream.close();
126
127 profileManager->loadProfile(custom_profile);
128 profile = profileManager->getProfile();
129 configuration = profile->get_configuration();
130
131 Variable variable("Soothsayer.Custom");
132
133 Value value = configuration->get(variable);
134
135 CPPUNIT_ASSERT_EQUAL(std::string("CUSTOM"), value);
136
137 #ifdef HAVE_UNISTD_H
138 // remove file
139 int result = unlink(custom_profile.c_str());
140 assert(result == 0);
141 #else
142 // fail test
143 std::string message = "Unable to remove custom profile file " + custom_profile;
144 CPPUNIT_FAIL(message.c_str());
145 #endif
146
147 }
148