desktop: fixed bug in GroupCfgDlg that gave mistaken "not configured" error
[barry/progweb.git] / desktop / src / ostest.cc
blob999ee6a4b04f605778765b0f6a409caec3587741
1 ///
2 /// \file ostest.cc
3 /// Test application for the OpenSync API
4 ///
6 /*
7 Copyright (C) 2009-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include <iostream>
23 #include <stdexcept>
24 #include <memory>
25 #include <barry/barry.h>
26 #include "os22.h"
27 #include "os40.h"
28 #include "deviceset.h"
30 using namespace std;
31 using namespace OpenSync;
33 void DeviceSetTest(Barry::GlobalConfigFile &config, APISet &set)
35 DeviceSet dset(config, set);
36 cout << "========================================================\n";
37 cout << " Device Set results:\n";
38 cout << "========================================================\n";
39 cout << dset << endl;
42 void Test(API &os)
44 cout << "=======================================================\n";
45 cout << " Begin test run: " << os.GetVersion() << "\n";
46 cout << "=======================================================\n";
48 format_list_type flist;
49 os.GetFormats(flist);
50 cout << "Formats:\n" << flist << endl;
52 string_list_type slist;
53 os.GetPluginNames(slist);
54 cout << "Plugins:\n" << slist << endl;
56 os.GetGroupNames(slist);
57 cout << "Groups:\n" << slist << endl;
59 for( string_list_type::iterator b = slist.begin(); b != slist.end(); ++ b) {
60 member_list_type mlist;
61 os.GetMembers(*b, mlist);
62 cout << "Members for group: " << *b << endl;
63 cout << "---------------------------------------\n";
64 cout << mlist << endl;
68 // Test Group / Members
71 const std::string group_name = "ostest_trial_group";
73 cout << "Testing with group_name: " << group_name << endl;
75 // start fresh
76 try { os.DeleteGroup(group_name); }
77 catch( std::runtime_error &re ) {
78 cout << "DeleteGroup: " << re.what() << endl;
81 // add group twice, to confirm behaviour
82 os.AddGroup(group_name);
83 cout << "Added: " << group_name << endl;
85 try {
86 os.AddGroup(group_name);
87 throw std::logic_error("AddGroup() succeeded incorrectly!");
89 catch( std::runtime_error &re ) {
90 cout << "AddGroup: " << re.what() << endl;
93 if( OpenSync40 *os40 = dynamic_cast<OpenSync40*>(&os) ) {
94 try { os40->DeleteMember(group_name, 1); }
95 catch( std::runtime_error &re ) {
96 cout << "DeleteMember: " << re.what() << endl;
100 os.AddMember(group_name, "barry-sync", "Barry sync member");
101 os.AddMember(group_name, "evo2-sync", "Evolution sync member");
103 // test deleting the member twice, to confirm behaviour
104 // only os40 has DeleteMember()
105 if( OpenSync40 *os40 = dynamic_cast<OpenSync40*>(&os) ) {
106 os.AddMember(group_name, "file-sync", "File sync member");
107 os40->DeleteMember(group_name, "file-sync");
108 try { os40->DeleteMember(group_name, "file-sync"); }
109 catch( std::runtime_error &re ) {
110 cout << "DeleteMember: " << re.what() << endl;
114 // display our test group
115 member_list_type mlist;
116 os.GetMembers(group_name, mlist);
117 cout << "Members for group: " << group_name << endl;
118 cout << "---------------------------------------\n";
119 cout << mlist << endl;
121 // dump configurations
122 cout << group_name << "(1): " << (os.IsConfigurable(group_name, 1) ?
123 "configurable" : "not configurable") << endl;
124 cout << group_name << "(2): " << (os.IsConfigurable(group_name, 2) ?
125 "configurable" : "not configurable") << endl;
127 cout << "Member 1's configuration:\n";
128 cout << os.GetConfiguration(group_name, 1) << endl;
129 cout << "Member 2's configuration:\n";
130 cout << os.GetConfiguration(group_name, 2) << endl;
132 // add comment to bottom of barry-sync config
133 long id = mlist.FindId("barry-sync");
134 string barry_config = os.GetConfiguration(group_name, id);
135 if( dynamic_cast<OpenSync22*>(&os) )
136 barry_config += "\n# This is a test\n";
137 else
138 barry_config += "<!-- This is a test -->\n";
139 os.SetConfiguration(group_name, id, barry_config);
140 cout << "New config for member " << id << ":\n";
141 cout << os.GetConfiguration(group_name, id);
143 // discover
144 os.Discover(group_name);
146 // try a sync
147 SyncStatus status_callbacks;
148 os.Sync(group_name, status_callbacks);
149 os.Sync(group_name, status_callbacks);
151 // loop
152 string command;
153 while( (cout << "Enter 'q' to quit: "),
154 getline(cin, command),
155 command[0] != 'q' )
157 os.Sync(group_name, status_callbacks);
160 // delete group twice, to confirm behaviour
161 os.DeleteGroup(group_name);
162 cout << "Deleted: " << group_name << endl;
164 try {
165 os.DeleteGroup(group_name);
166 throw std::logic_error("DeleteGroup() succeeded incorrectly!");
168 catch( std::runtime_error &re ) {
169 cout << "DeleteGroup failed as expected" << endl;
172 cout << "=======================================================\n";
173 cout << " End test run: " << os.GetVersion() << "\n";
174 cout << "=======================================================\n";
177 void ShowAdvanced(OS40PluginConfig &cfg, const char *name)
179 cout << name << ": " << cfg.GetAdvanced(name) << endl;
182 void TestConfig(OpenSync40 &os, const char *name, long member_id)
184 OS40PluginConfig cfg = os.GetConfigurationObj(name, member_id);
185 ShowAdvanced(cfg, "PinCode");
186 ShowAdvanced(cfg, "Debug");
187 cfg.SetAdvanced("TestName", "Test Display Name", "Whippoorwill");
189 OS40PluginConfig::OS40ConfigResourcePtr res =
190 cfg.GetResource("contact");
191 if( res->IsExistingResource() ) {
192 cout << "Resource: " << res->GetName() << ": "
193 << (res->IsEnabled() ? "enabled" : "disabled")
194 << endl;
195 cout << " pref format: " << res->GetPreferredFormat() << endl;
196 cout << " mime: " << res->GetMime() << endl;
197 cout << " objtype: " << res->GetObjType() << endl;
198 cout << " path: " << res->GetPath() << endl;
199 cout << " url: " << res->GetUrl() << endl;
201 string config;
202 if( res->FindObjFormat("vcard30", config) ) {
203 cout << " objformat: vcard30: " << config << endl;
205 else {
206 cout << " no vcard30 found" << endl;
209 else {
210 cout << "No contact resource found" << endl;
213 // add one / set one
214 res = cfg.GetResource("testresource");
215 if( res->IsExistingResource() )
216 cout << "testresource exists" << endl;
217 res->SetObjFormat("vcard30").
218 SetUrl("http://netdirect.ca/").
219 SetName("ResourceName").
220 Enable().
221 AddResource();
222 cfg.Save();
225 void TestConfig(API &os)
227 OpenSync40 &os40 = dynamic_cast<OpenSync40&> (os);
228 TestConfig(os40, "test", 1);
229 TestConfig(os40, "test", 2);
232 int main()
234 Barry::Init(true);
236 try {
237 Barry::GlobalConfigFile config("ostest");
239 APISet set;
240 set.OpenAvailable();
242 DeviceSetTest(config, set);
244 if( set.os40() ) {
245 TestConfig(*set.os40());
246 Test(*set.os40());
249 if( set.os22() ) {
250 Test(*set.os22());
254 catch( std::exception &e ) {
255 cout << "TEST FAILED: " << e.what() << endl;
258 return 0;