~smspillaz/nux/nux.3.0.fix_1036521

« back to all changes in this revision

Viewing changes to tests/gtest-nux-area.cpp

WindowCompostior: use ObjectWeakPtr to handle focus and mouse areas

So they get automatically nullified when the handled objects are destroyed,
also Make Area to unset the key-focus paths on destroy.. Fixes: https://bugs.launchpad.net/bugs/1045059. Approved by Marco Trevisan (Treviño).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <string>
2
 
#include <fstream>
 
1
/*
 
2
 * Copyright 2010 Inalogic® Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 
19
 *              Marco Trevisan <marco.trevisan@canonical.com>
 
20
 */
3
21
 
4
 
#include <iostream>
5
22
#include <gmock/gmock.h>
6
 
#include <boost/filesystem.hpp>
7
 
#include <glib.h>
8
 
 
 
23
#include <boost/shared_ptr.hpp>
9
24
#include "Nux/Nux.h"
10
25
#include "Nux/HLayout.h"
11
26
#include "Nux/StaticText.h"
16
31
 
17
32
namespace {
18
33
 
19
 
TEST(TestArea, TestAreaSize)
20
 
{
21
 
  nux::NuxInitialize(0);
22
 
  nux::WindowThread *wnd_thread = nux::CreateNuxWindow("Area Test", 300, 200,
23
 
    nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
24
 
 
25
 
  nux::TestView* test_view = new nux::TestView("");
26
 
 
 
34
struct MockTestView : nux::TestView
 
35
{
 
36
  MockTestView()
 
37
    : nux::TestView("MockTestView")
 
38
  {}
 
39
 
 
40
  void SetNextObjectToKeyFocusArea(nux::Area* area)
 
41
  {
 
42
    TestView::SetNextObjectToKeyFocusArea(area);
 
43
  }
 
44
 
 
45
  nux::Area* GetNextObjectToKeyFocusArea()
 
46
  {
 
47
    return TestView::GetNextObjectToKeyFocusArea();
 
48
  }
 
49
};
 
50
 
 
51
struct TestArea : public testing::Test
 
52
{
 
53
  void SetUp()
 
54
  {
 
55
    nux::NuxInitialize(0);
 
56
    wnd_thread.reset(nux::CreateNuxWindow("Area Test", 300, 200, nux::WINDOWSTYLE_NORMAL,
 
57
                                          NULL, false, NULL, NULL));
 
58
    test_view = new MockTestView();
 
59
  }
 
60
 
 
61
  boost::shared_ptr<nux::WindowThread> wnd_thread;
 
62
  nux::ObjectPtr<MockTestView> test_view;
 
63
};
 
64
 
 
65
TEST_F(TestArea, TestAreaSize)
 
66
{
27
67
  EXPECT_EQ(test_view->GetMinimumWidth(), nux::AREA_MIN_WIDTH);
28
68
  EXPECT_EQ(test_view->GetMaximumWidth(), nux::AREA_MAX_WIDTH);
29
69
  EXPECT_EQ(test_view->GetMinimumHeight(), nux::AREA_MIN_HEIGHT);
48
88
  EXPECT_EQ(test_view->GetMaximumWidth(), 4321);
49
89
  EXPECT_EQ(test_view->GetMinimumHeight(), 432);
50
90
  EXPECT_EQ(test_view->GetMaximumHeight(), 1234);
51
 
 
52
 
  test_view->UnReference();
53
 
  delete wnd_thread;
54
91
}
55
92
 
56
 
TEST(TestArea, TestAreaGeometry)
 
93
TEST_F(TestArea, TestAreaGeometry)
57
94
{
58
 
  nux::NuxInitialize(0);
59
 
  nux::WindowThread *wnd_thread = nux::CreateNuxWindow("Area Test", 300, 200,
60
 
    nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
61
 
 
62
 
  nux::TestView* test_view = new nux::TestView("");
63
 
 
64
 
  static_cast<nux::Area*>(test_view)->SetGeometry(0, 0, 100, 100);
 
95
  static_cast<nux::Area*>(test_view.GetPointer())->SetGeometry(0, 0, 100, 100);
65
96
 
66
97
  EXPECT_EQ(test_view->GetBaseX(), 0);
67
98
  EXPECT_EQ(test_view->GetBaseX(), 0);
68
99
  EXPECT_EQ(test_view->GetBaseWidth(), 100);
69
100
  EXPECT_EQ(test_view->GetBaseHeight(), 100);
70
 
 
71
 
 
72
 
  test_view->UnReference();
73
 
  delete wnd_thread;
74
101
}
75
102
 
76
103
static bool object_destroyed = false;
79
106
  object_destroyed = true;
80
107
}
81
108
 
82
 
TEST(TestArea, TestUnParentKeyFocus)
 
109
TEST_F(TestArea, TestUnParentKeyFocus)
83
110
{
84
 
  nux::NuxInitialize(0);
85
 
  nux::WindowThread *wnd_thread = nux::CreateNuxWindow("Area Test", 300, 200,
86
 
    nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
87
 
 
 
111
  nux::TestView* test_view1 = new nux::TestView("");
88
112
  nux::HLayout* layout = new nux::HLayout();
89
 
  nux::TestView* test_view = new nux::TestView("");
90
 
  test_view->object_destroyed.connect(sigc::ptr_fun(&OnObjectDestroyed));
 
113
  test_view1->object_destroyed.connect(sigc::ptr_fun(&OnObjectDestroyed));
91
114
 
92
 
  test_view->Reference();
93
 
  
94
 
  layout->AddView(test_view, 1);
 
115
  test_view1->Reference();
 
116
  layout->AddView(test_view1, 1);
95
117
 
96
118
  wnd_thread->SetLayout(layout);
97
119
 
98
 
  EXPECT_EQ(test_view->HasKeyFocus(), false);
99
 
  nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(test_view);
100
 
  EXPECT_EQ(test_view->HasKeyFocus(), true);
101
 
  layout->RemoveChildObject(test_view);
102
 
  EXPECT_EQ(test_view->HasKeyFocus(), false);
103
 
  test_view->UnReference();
 
120
  EXPECT_EQ(test_view1->HasKeyFocus(), false);
 
121
  nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(test_view1);
 
122
  EXPECT_EQ(test_view1->HasKeyFocus(), true);
 
123
  layout->RemoveChildObject(test_view1);
 
124
  EXPECT_EQ(test_view1->HasKeyFocus(), false);
 
125
  test_view1->UnReference();
104
126
  EXPECT_EQ(object_destroyed, true);
105
 
 
106
 
  delete wnd_thread;
 
127
}
 
128
 
 
129
TEST_F(TestArea, NextObjectToKeyFocusSetReset)
 
130
{
 
131
  MockTestView* parent = new MockTestView();
 
132
  MockTestView* brother = new MockTestView();
 
133
  MockTestView* child1 = new MockTestView();
 
134
  MockTestView* child2 = new MockTestView();
 
135
  MockTestView* child3 = new MockTestView();
 
136
 
 
137
  parent->SetNextObjectToKeyFocusArea(brother);
 
138
  ASSERT_EQ(parent->GetNextObjectToKeyFocusArea(), brother);
 
139
 
 
140
  child1->SetParentObject(parent);
 
141
  child1->SetNextObjectToKeyFocusArea(child2);
 
142
  ASSERT_EQ(child1->GetNextObjectToKeyFocusArea(), child2);
 
143
 
 
144
  child2->SetParentObject(parent);
 
145
  child2->SetNextObjectToKeyFocusArea(child3);
 
146
  ASSERT_EQ(child2->GetNextObjectToKeyFocusArea(), child3);
 
147
 
 
148
  child3->SetParentObject(parent);
 
149
  ASSERT_EQ(child3->GetNextObjectToKeyFocusArea(), nullptr);
 
150
 
 
151
  child1->UnReference();
 
152
  EXPECT_EQ(child2->GetNextObjectToKeyFocusArea(), nullptr);
 
153
  EXPECT_EQ(parent->GetNextObjectToKeyFocusArea(), nullptr);
 
154
 
 
155
  parent->UnReference();
107
156
}
108
157
 
109
158
}