tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / event / test_property.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_property.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of test property
21  */
22 #include <dpl/test/test_runner.h>
23 #include <dpl/event/property.h>
24 #include <dpl/event/model.h>
25 #include <string>
26
27 namespace {
28 const int PROPERTY_VALUE_INT = 2;
29 const std::string PROPERTY_VALUE_STRING = "aaa";
30 }
31
32 int ReadSomething2(DPL::Event::Model */*model*/);
33 int ReadSomething2(DPL::Event::Model */*model*/)
34 {
35     return PROPERTY_VALUE_INT;
36 }
37
38 std::string ReadSomething(DPL::Event::Model */*model*/);
39 std::string ReadSomething(DPL::Event::Model */*model*/)
40 {
41     return PROPERTY_VALUE_STRING;
42 }
43
44 void WriteSomething(const std::string & /*value*/, DPL::Event::Model */*model*/);
45 void WriteSomething(const std::string & /*value*/, DPL::Event::Model */*model*/)
46 {}
47
48 class MyModel :
49     public DPL::Event::Model
50 {
51   public:
52     ~MyModel() {}
53
54     DPL::Event::Property<std::string>
55     Caption;
56
57     DPL::Event::Property<std::string>
58     Testproperty0;
59
60     DPL::Event::Property<std::string, DPL::Event::PropertyReadOnly>
61     Testproperty1;
62
63     DPL::Event::Property<std::string, DPL::Event::PropertyReadWrite>
64     Testproperty2;
65
66     DPL::Event::Property<std::string, DPL::Event::PropertyReadWrite,
67                          DPL::Event::PropertyStorageCached> Testproperty3;
68
69     DPL::Event::Property<std::string, DPL::Event::PropertyReadWrite,
70                          DPL::Event::PropertyStorageDynamic> Testproperty4;
71
72     DPL::Event::Property<int, DPL::Event::PropertyReadOnly,
73                          DPL::Event::PropertyStorageDynamicCached>
74     Testproperty5;
75
76     MyModel() :
77         Caption(this, std::string("Foo caption")),
78         Testproperty0(this, std::string(""), &ReadSomething),
79         Testproperty1(this),
80         Testproperty2(this),
81         Testproperty3(this),
82         Testproperty4(this, std::string("test"), &ReadSomething, &WriteSomething),
83         Testproperty5(this, &ReadSomething2)
84     {}
85 };
86
87 std::string g_caption;
88
89 void OnNameChanged(const DPL::Event::PropertyEvent<std::string> &event);
90 void OnNameChanged(const DPL::Event::PropertyEvent<std::string> &event)
91 {
92     g_caption = event.value;
93 }
94
95 /*
96 Name: Model_Test
97 Description: tests accessing and changing models properties
98 Expected: listener should get changed value
99 */
100 RUNNER_TEST(Model_Test)
101 {
102     MyModel model;
103
104     g_caption = "It is a bad caption";
105
106     model.Caption.AddListener(&OnNameChanged);
107     model.Caption.Set("Test name");
108
109     RUNNER_ASSERT(model.Testproperty4.Get() == PROPERTY_VALUE_STRING);
110     RUNNER_ASSERT(PROPERTY_VALUE_INT == model.Testproperty5.Get());
111     RUNNER_ASSERT(g_caption == "Test name");
112     RUNNER_ASSERT(model.Caption.Get() == "Test name");
113
114     model.Caption.RemoveListener(&OnNameChanged);
115 }