summaryrefslogtreecommitdiff
path: root/tests/test_cmdline_search_progress.cc
blob: e245e690efbc59a16669ca5c238b229d586ae64b (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/** \file test_cmdline_search_progress.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.



// Local includes:
#include <cmdline/cmdline_search_progress.h>

#include <generic/util/progress_info.h>
#include <generic/util/mocks/throttle.h>
#include <generic/views/mocks/progress.h>

// System includes:
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

using aptitude::cmdline::create_search_progress;
using aptitude::util::progress_info;
using boost::make_shared;
using boost::shared_ptr;
using testing::AnyNumber;
using testing::Expectation;
using testing::Mock;
using testing::Return;
using testing::Sequence;
using testing::Test;
using testing::_;

namespace mocks
{
  using namespace aptitude::util::mocks;
  using namespace aptitude::views::mocks;
}

namespace views = aptitude::views;

namespace
{
  struct CmdlineSearchProgressTest : public Test
  {
    const shared_ptr<mocks::progress> progress;
    const shared_ptr<mocks::throttle> throttle;

    /** \brief The search pattern string used to create the search
     *  progress object.
     */
    const std::string search_pattern;
    const shared_ptr<views::progress> search_progress;

    CmdlineSearchProgressTest()
      : progress(make_shared<mocks::progress>()),
        throttle(make_shared<mocks::throttle>()),
        search_pattern("?name(aptitude)"),
        search_progress(create_search_progress(search_pattern,
                                               progress,
                                               throttle))
    {
    }

    // Define shorter names for the progress_info constructors.
    progress_info none()
    {
      return progress_info::none();
    }

    progress_info pulse(const std::string &msg)
    {
      return progress_info::pulse(msg);
    }

    progress_info bar(double fraction, const std::string &msg)
    {
      return progress_info::bar(fraction, msg);
    }

    void never_throttle()
    {
      EXPECT_CALL(*throttle, update_required())
        .WillRepeatedly(Return(true));
      EXPECT_CALL(*throttle, reset_timer())
        .Times(AnyNumber());
    }

    void always_throttle()
    {
      EXPECT_CALL(*throttle, update_required())
        .WillRepeatedly(Return(false));
      // No call to reset_timer() expected since it's throttled --
      // calling it would, in fact, be wrong.
      EXPECT_CALL(*throttle, reset_timer())
        .Times(0);
    }
  };
}

TEST_F(CmdlineSearchProgressTest, SetProgressNone)
{
  never_throttle();

  EXPECT_CALL(*progress, set_progress(none()));

  search_progress->set_progress(none());
}

TEST_F(CmdlineSearchProgressTest, SetProgressPulse)
{
  never_throttle();

  const std::string msg = "Blip";
  const progress_info info = pulse(search_pattern + ": " + msg);

  EXPECT_CALL(*progress, set_progress(info));

  search_progress->set_progress(pulse(msg));
}

TEST_F(CmdlineSearchProgressTest, SetProgressBar)
{
  never_throttle();

  const double fraction = 0.3;
  const std::string msg = "Blorp";
  const progress_info info = bar(fraction, search_pattern + ": " + msg);

  EXPECT_CALL(*progress, set_progress(info));

  search_progress->set_progress(bar(fraction, msg));
}

TEST_F(CmdlineSearchProgressTest, ThrottledProgressNone)
{
  always_throttle();

  EXPECT_CALL(*progress, set_progress(_))
    .Times(0);

  search_progress->set_progress(none());


  // Check that a second set_progress() call goes through, since
  // throttling is no longer enabled:
  Mock::VerifyAndClearExpectations(throttle.get());
  Mock::VerifyAndClearExpectations(progress.get());

  never_throttle();

  EXPECT_CALL(*progress, set_progress(none()));

  search_progress->set_progress(none());
}

TEST_F(CmdlineSearchProgressTest, ThrottledProgressPulse)
{
  const std::string msg = "The caged whale knows not the mighty deeps";
  const progress_info info = pulse(msg);

  always_throttle();

  EXPECT_CALL(*progress, set_progress(_))
    .Times(0);

  search_progress->set_progress(info);


  // Check that a second set_progress() call goes through, since
  // throttling is no longer enabled:
  Mock::VerifyAndClearExpectations(throttle.get());
  Mock::VerifyAndClearExpectations(progress.get());

  never_throttle();

  EXPECT_CALL(*progress,
              set_progress(pulse(search_pattern + ": " + msg)));

  search_progress->set_progress(info);
}

TEST_F(CmdlineSearchProgressTest, ThrottledProgressBar)
{
  const std::string msg = "Reversing polarity";
  const double fraction = 0.8;
  const progress_info info = bar(fraction, msg);

  always_throttle();

  EXPECT_CALL(*progress, set_progress(_))
    .Times(0);

  search_progress->set_progress(info);


  // Check that a second set_progress() call goes through, since
  // throttling is no longer enabled:
  Mock::VerifyAndClearExpectations(throttle.get());
  Mock::VerifyAndClearExpectations(progress.get());

  never_throttle();

  EXPECT_CALL(*progress,
              set_progress(bar(fraction, search_pattern + ": " + msg)));

  search_progress->set_progress(info);
}

TEST_F(CmdlineSearchProgressTest, Done)
{
  never_throttle();
  EXPECT_CALL(*progress, done());

  search_progress->done();
}

// Test that Done ignores throttling.
TEST_F(CmdlineSearchProgressTest, DoneThrottled)
{
  always_throttle();
  EXPECT_CALL(*progress, done());

  search_progress->done();
}