1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/** \file test_search_input.cc */
// Copyright (C) 2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#include <generic/apt/matching/compare_patterns.h>
#include <generic/apt/matching/pattern.h>
#include <generic/controllers/search_input.h>
#include <generic/views/mocks/search_input.h>
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace ctrls = aptitude::controllers;
namespace views = aptitude::views;
namespace mocks = aptitude::views::mocks;
using aptitude::matching::compare_patterns;
using aptitude::matching::pattern;
using boost::make_shared;
using boost::shared_ptr;
using cwidget::util::ref_ptr;
using testing::AnyNumber;
using testing::Expectation;
using testing::InSequence;
using testing::NiceMock;
using testing::Return;
using testing::StrictMock;
namespace
{
MATCHER_P(PatternsEqual,
expected,
"")
{
return compare_patterns(arg, expected) == 0;
}
// Mock class that lets us capture the controller's outgoing
// signals.
class controller_callbacks
{
public:
MOCK_METHOD2(activated, void(std::wstring, cwidget::util::ref_ptr<aptitude::matching::pattern>));
};
struct SearchInputTest
{
boost::shared_ptr<mocks::search_input> view;
boost::shared_ptr<mocks::search_input> viewNice;
boost::shared_ptr<mocks::search_input> viewStrict;
controller_callbacks callbacks;
controller_callbacks callbacksNice;
controller_callbacks callbacksStrict;
// Controllers are initialized lazily, to give us a chance to
// register expectations with the views first.
private:
boost::shared_ptr<ctrls::search_input> controller;
boost::shared_ptr<ctrls::search_input> controllerNice;
boost::shared_ptr<ctrls::search_input> controllerStrict;
public:
boost::shared_ptr<ctrls::search_input> get_controller()
{
if(controller.get() == NULL)
{
controller = ctrls::create_search_input(view);
controller->connect_activated(sigc::mem_fun(callbacks,
&controller_callbacks::activated));
}
return controller;
}
boost::shared_ptr<ctrls::search_input> get_controllerNice()
{
if(controllerNice.get() == NULL)
{
controllerNice = ctrls::create_search_input(view);
controllerNice->connect_activated(sigc::mem_fun(callbacksNice,
&controller_callbacks::activated));
}
return controllerNice;
}
boost::shared_ptr<ctrls::search_input> get_controllerStrict()
{
if(controllerStrict.get() == NULL)
{
controllerStrict = ctrls::create_search_input(view);
controllerStrict->connect_activated(sigc::mem_fun(callbacksStrict,
&controller_callbacks::activated));
}
return controllerStrict;
}
SearchInputTest()
: view(make_shared<mocks::search_input>()),
viewNice(make_shared<NiceMock<mocks::search_input> >()),
viewStrict(make_shared<StrictMock<mocks::search_input> >())
{
}
};
}
BOOST_FIXTURE_TEST_CASE(testEnteringCorrectTextSearches, SearchInputTest)
{
ref_ptr<pattern> p = pattern::make_installed();
std::wstring p_text = L"?installed";
// The controller isn't required to call set_error_message(), but it
// should set it to "" if it does.
EXPECT_CALL(*view, set_error_message(std::wstring())).Times(AnyNumber());
Expectation set_search_text;
{
InSequence dummy;
set_search_text = EXPECT_CALL(*view, set_search_text(p_text));
EXPECT_CALL(callbacks, activated(p_text, PatternsEqual(p)));
}
// After setting the search text, get_search_text() will return the
// new text.
EXPECT_CALL(*view, get_search_text())
.After(set_search_text)
.WillRepeatedly(Return(p_text));
get_controller()->enter_text(p_text);
}
|