Always append to AM_VALAFLAGS, don't replace
[platform/upstream/folks.git] / tests / tracker / set-structured-name.vala
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
4  * This library is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk>
18  *
19  */
20
21 using Tracker.Sparql;
22 using TrackerTest;
23 using Folks;
24 using Gee;
25
26 public class SetStructuredNameTests : Folks.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private TrackerTest.Backend _tracker_backend;
30   private IndividualAggregator _aggregator;
31   private string _persona_fullname;
32   private bool _sname_found;
33   private StructuredName _sname;
34   private string _family_name;
35   private string _given_name;
36   private string _additional_names;
37   private string _prefixes;
38   private string _suffixes;
39
40   public SetStructuredNameTests ()
41     {
42       base ("SetStructuredNameTests");
43
44       this._tracker_backend = new TrackerTest.Backend ();
45
46       this.add_test ("test setting structured name ",
47           this.test_set_structured_name);
48     }
49
50   public override void set_up ()
51     {
52     }
53
54   public override void tear_down ()
55     {
56     }
57
58   public void test_set_structured_name ()
59     {
60       this._main_loop = new GLib.MainLoop (null, false);
61       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
62       this._persona_fullname = "persona #1";
63       this._family_name = "family name";
64       this._given_name = "given name";
65       this._additional_names = "additional name";
66       this._prefixes = "prefixes";
67       this._suffixes = "suffixes";
68
69       this._sname = new StructuredName (this._family_name, this._given_name,
70           this._additional_names, this._prefixes, this._suffixes);
71
72       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
73       this._tracker_backend.add_contact (c1);
74
75       this._tracker_backend.set_up ();
76
77       this._sname_found = false;
78
79       this._test_set_structured_name_async.begin ();
80
81       Timeout.add_seconds (5, () =>
82         {
83           this._main_loop.quit ();
84           assert_not_reached ();
85         });
86
87       this._main_loop.run ();
88
89       assert (this._sname_found);
90
91       this._tracker_backend.tear_down ();
92     }
93
94   private async void _test_set_structured_name_async ()
95     {
96       var store = BackendStore.dup ();
97       yield store.prepare ();
98       this._aggregator = new IndividualAggregator ();
99       this._aggregator.individuals_changed_detailed.connect
100           (this._individuals_changed_cb);
101       try
102         {
103           yield this._aggregator.prepare ();
104         }
105       catch (GLib.Error e)
106         {
107           GLib.warning ("Error when calling prepare: %s\n", e.message);
108         }
109     }
110
111   private void _individuals_changed_cb (
112        MultiMap<Individual?, Individual?> changes)
113     {
114       var added = changes.get_values ();
115       var removed = changes.get_keys ();
116
117       foreach (var i in added)
118         {
119           assert (i != null);
120
121           if (i.full_name == this._persona_fullname)
122             {
123               foreach (var p in i.personas)
124                 {
125                   p.notify["structured-name"].connect (this._notify_sname_cb);
126                   ((NameDetails) p).structured_name = this._sname;
127                 }
128             }
129         }
130
131       assert (removed.size == 1);
132
133       foreach (var i in removed)
134         {
135           assert (i == null);
136         }
137     }
138
139   private void _notify_sname_cb (Object persona, ParamSpec ps)
140     {
141       Trf.Persona p = (Trf.Persona) persona;
142       if (p.full_name == this._persona_fullname)
143         {
144           if (p.structured_name.is_empty () == false &&
145               p.structured_name.equal (this._sname) == true)
146             {
147               this._sname_found = true;
148               this._main_loop.quit ();
149             }
150         }
151     }
152 }
153
154 public int main (string[] args)
155 {
156   Test.init (ref args);
157
158   TestSuite root = TestSuite.get_root ();
159   root.add_suite (new SetStructuredNameTests ().get_suite ());
160
161   Test.run ();
162
163   return 0;
164 }