Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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
49 class MyModel
50     : public DPL::Event::Model
51 {
52 public:
53     ~MyModel() {}
54
55     DPL::Event::Property<std::string>
56         Caption;
57
58     DPL::Event::Property<std::string>
59         Testproperty0;
60
61     DPL::Event::Property<std::string, DPL::Event::PropertyReadOnly>
62         Testproperty1;
63
64     DPL::Event::Property<std::string, DPL::Event::PropertyReadWrite>
65         Testproperty2;
66
67     DPL::Event::Property<std::string, DPL::Event::PropertyReadWrite,
68         DPL::Event::PropertyStorageCached> Testproperty3;
69
70     DPL::Event::Property<std::string, DPL::Event::PropertyReadWrite,
71         DPL::Event::PropertyStorageDynamic> Testproperty4;
72
73     DPL::Event::Property<int, DPL::Event::PropertyReadOnly,
74         DPL::Event::PropertyStorageDynamicCached> Testproperty5;
75
76     MyModel()
77         : Caption(this, "Foo caption"),
78           Testproperty0(this, "", &ReadSomething),
79           Testproperty1(this),
80           Testproperty2(this),
81           Testproperty3(this),
82           Testproperty4(this, "test", &ReadSomething, &WriteSomething),
83           Testproperty5(this, &ReadSomething2)
84     {
85     }
86 };
87
88 std::string g_caption;
89
90 void OnNameChanged(const DPL::Event::PropertyEvent<std::string> &event);
91 void OnNameChanged(const DPL::Event::PropertyEvent<std::string> &event)
92 {
93     g_caption = event.value;
94 }
95
96 RUNNER_TEST(Model_Test)
97 {
98     MyModel model;
99
100     g_caption = "It is a bad caption";
101
102     model.Caption.AddListener(&OnNameChanged);
103     model.Caption.Set("Test name");
104
105     RUNNER_ASSERT(model.Testproperty4.Get() == PROPERTY_VALUE_STRING);
106     RUNNER_ASSERT(PROPERTY_VALUE_INT == model.Testproperty5.Get());
107     RUNNER_ASSERT(g_caption == "Test name");
108     RUNNER_ASSERT(model.Caption.Get() == "Test name");
109
110     model.Caption.RemoveListener(&OnNameChanged);
111 }