summaryrefslogtreecommitdiff
path: root/tests/test_cmdline_download_progress_display.cc
blob: 57b23de38cb6ad13557df33aeb53848f7a8be746 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/** \file test_cmdline_download_progress_display.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_download_progress_display.h>
#include <cmdline/mocks/terminal.h>
#include <cmdline/mocks/transient_message.h>

#include <generic/views/download_progress.h>
#include <generic/views/mocks/download_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_download_progress_display;
using aptitude::views::download_progress;
using aptitude::views::download_status_display;
using boost::make_shared;
using boost::optional;
using boost::shared_ptr;
using testing::InSequence;
using testing::Mock;
using testing::Return;
using testing::StrEq;
using testing::TestWithParam;
using testing::Throw;
using testing::Values;
using testing::_;

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

namespace views = aptitude::views;

namespace
{
  // The test parameter controls whether messages are to be displayed.
  struct CmdlineDownloadProgressDisplayTest : public TestWithParam<bool>
  {
    shared_ptr<mocks::transient_message> msg;
    shared_ptr<mocks::download_status_display> status_display;
    shared_ptr<mocks::terminal_input> term_input;

    shared_ptr<views::download_progress> progress;

    typedef download_progress::file_progress file_progress;
    typedef download_progress::status status;
    typedef download_progress::status::worker_status worker_status;
    // Two arbitrary status objects; the only requirement on them is
    // that they have to be different from each other (so we can
    // verify that arguments are being passed through as expected).
    status status1, status2;

    CmdlineDownloadProgressDisplayTest()
      : msg(mocks::transient_message::create_strict()),
        status_display(mocks::download_status_display::create_strict()),
        term_input(mocks::terminal_input::create_strict()),
        status1(5, std::vector<status::worker_status>(), 0.5, 10),
        status2(1, std::vector<status::worker_status>(), 0.2, 110)
    {
    }

    void SetUp()
    {
      progress = create_download_progress_display(msg,
                                                  status_display,
                                                  term_input,
                                                  get_display_messages());
    }

    bool get_display_messages() const
    {
      return GetParam();
    }
  };
}

// Expect the given string to be passed to display_and_advance() if
// get_display_messages() is true; otherwise expect no calls to that
// routine.
#define EXPECT_MSG(__the_message_to_expect)                             \
  do                                                                    \
    {                                                                   \
      if(get_display_messages())                                        \
        EXPECT_CALL(*msg, display_and_advance(StrEq(__the_message_to_expect))); \
      else                                                              \
        EXPECT_CALL(*msg, display_and_advance(_))                       \
          .Times(0);                                                    \
    } while(0)

// Test that there are no unexpected calls just from creating the
// download progress display.
TEST_P(CmdlineDownloadProgressDisplayTest, CreateDoesNothing)
{
}

// Test that equality comparisons of file_progress objects respect the
// current_size field.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsCurrentSize)
{
  EXPECT_NE(file_progress(1, 0, true, "", optional<unsigned long>(0), ""),
            file_progress(2, 0, true, "", optional<unsigned long>(0), ""));
}

// Test that equality comparisons of file_progress objects respect the
// total_size field.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsTotalSize)
{
  EXPECT_NE(file_progress(2, 1, true, "", optional<unsigned long>(0), ""),
            file_progress(2, 0, true, "", optional<unsigned long>(0), ""));
}

// Test that equality comparisons of file_progress objects respect the
// description field.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsDescription)
{
  EXPECT_NE(file_progress(2, 0, true, "1", optional<unsigned long>(0), ""),
            file_progress(2, 0, true, "2", optional<unsigned long>(0), ""));
}

// Test that equality comparisons of file_progress objects respect the
// id field.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsId)
{
  EXPECT_NE(file_progress(2, 0, true, "1", optional<unsigned long>(0), ""),
            file_progress(2, 0, true, "1", optional<unsigned long>(5), ""));
}

// Test that equality comparisons of file_progress objects respect the
// id field when one side is unspecified.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsIdOneUnspecified)
{
  EXPECT_NE(file_progress(2, 0, true, "1", optional<unsigned long>(0), ""),
            file_progress(2, 0, true, "1", optional<unsigned long>(), ""));
}

// Test that equality comparisons of file_progress objects respect the
// mode field.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsMode)
{
  EXPECT_NE(file_progress(2, 0, true, "1", optional<unsigned long>(0), "some mode"),
            file_progress(2, 0, true, "1", optional<unsigned long>(0), "another mode"));
}

// Test that equality comparisons of file_progress objects respect the
// complete field.
TEST_P(CmdlineDownloadProgressDisplayTest, FileProgressEqualsRespectsComplete)
{
  EXPECT_NE(file_progress(2, 0, true, "1", optional<unsigned long>(0), "some mode"),
            file_progress(2, 0, false, "1", optional<unsigned long>(0), "some mode"));
}

// Ensure that a pair of equal file_progress objects compares equal.
TEST_P(CmdlineDownloadProgressDisplayTest, EqualFileProgressEqual)
{
  EXPECT_EQ(file_progress(6, 4, true, "10", optional<unsigned long>(9), "a mode"),
            file_progress(6, 4, true, "10", optional<unsigned long>(9), "a mode"));
}

// Check that comparison of status objects respects the download_rate
// field.
TEST_P(CmdlineDownloadProgressDisplayTest, StatusEqualsRespectsDownloadRate)
{
  EXPECT_NE(status(0, std::vector<worker_status>(), 0, 0),
            status(1, std::vector<worker_status>(), 0, 0));
}

// Check that comparison of status objects respects the
// active_downloads field (empty vs nonempty).
TEST_P(CmdlineDownloadProgressDisplayTest, StatusEqualsRespectsActiveDownloadsEmptyVsNonempty)
{
  std::vector<worker_status> singleton;
  singleton.push_back(file_progress(0, 1, false, "2", 3, "4"));

  EXPECT_NE(status(0, singleton, 0, 0),
            status(0, std::vector<worker_status>(), 0, 0));
}

// Check that comparison of status objects respects the
// active_downloads field (one contains a file_progress, the other
// contains a string).
TEST_P(CmdlineDownloadProgressDisplayTest, StatusEqualsRespectsActiveDownloadsContentsFileProgressAndString)
{
  std::vector<worker_status> singleton1, singleton2;
  singleton1.push_back(file_progress(0, 1, false, "2", 3, "4"));
  singleton2.push_back(std::string("2"));

  EXPECT_NE(status(0, singleton1, 0, 0),
            status(0, singleton2, 0, 0));
}

// Check that comparison of status objects respects the
// active_downloads field (one contains a file_progress, the other
// contains a different file_progress).
TEST_P(CmdlineDownloadProgressDisplayTest, StatusEqualsRespectsActiveDownloadsContentsDifferentFileProgresses)
{
  std::vector<worker_status> singleton1, singleton2;
  singleton1.push_back(file_progress(0, 1, false, "2", 3, "4"));
  singleton2.push_back(file_progress(0, 1, false, "3", 3, "4"));

  EXPECT_NE(status(0, singleton1, 0, 0),
            status(0, singleton2, 0, 0));
}

// Check that comparison of status objects respects the
// active_downloads field (one contains a string, the other
// contains a different string).
TEST_P(CmdlineDownloadProgressDisplayTest, StatusEqualsRespectsActiveDownloadsContentsDifferentStrings)
{
  std::vector<worker_status> singleton1, singleton2;
  singleton1.push_back(std::string("1"));
  singleton2.push_back(std::string("2"));

  EXPECT_NE(status(0, singleton1, 0, 0),
            status(0, singleton2, 0, 0));
}

// Test that update_progress() passes through the status object and
// returns true.
TEST_P(CmdlineDownloadProgressDisplayTest, TestStatus1NeqStatus2)
{
  EXPECT_NE(status1, status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, UpdateProgress1)
{
  EXPECT_CALL(*status_display, display_status(status1));

  EXPECT_TRUE(progress->update_progress(status1));
}

TEST_P(CmdlineDownloadProgressDisplayTest, UpdateProgress2)
{
  EXPECT_CALL(*status_display, display_status(status2));

  EXPECT_TRUE(progress->update_progress(status2));
}

// The various file_started(), file_finished(), etc routines don't
// exhaustively verify that the status is displayed correctly; the
// assumption is that it gets displayed by the same routine that backs
// update_progress().  I test a few different status values to
// sanity-check this assumption, but that's all.
//
// (I could factor the actual display code into a separate routine
//  behind another interface, exhaustively test it in its own unit
//  test, and just verify here that the status is passed through when
//  expected.  Probably not worth the trouble)

TEST_P(CmdlineDownloadProgressDisplayTest, FileStartedWithIdNoNameWithFileSize)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Get: 4 [100B]");
    EXPECT_CALL(*status_display, display_status(status1));
  }

  progress->file_started("", 4, 100, status1);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileStartedNoIdNoFileSize)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Get: somefile");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->file_started("somefile", optional<unsigned long>(), optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileStartedWithIdNoFileSize)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Get: 0 filename");
    EXPECT_CALL(*status_display, display_status(status1));
  }

  progress->file_started("filename", optional<unsigned long>(0), optional<unsigned long>(), status1);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileStartedNoIdWithFileSize)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Get: thefile [210B]");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->file_started("thefile", optional<unsigned long>(), 210, status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileStartedWithIdWithFileSize)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Get: 6 another file [210B]");
    EXPECT_CALL(*status_display, display_status(status1));
  }

  progress->file_started("another file", 6, 210, status1);
}


// Test what happens with ignored errors.  There are lots of cases
// here, but they boil down to "we ignore all the parameters except
// description" (and of course ignored).
TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredNoIdNoErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "", "", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredWithIdNoErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "", "", optional<unsigned long>(4), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredNoIdWithErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "error message", "", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredWithIdWithErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "error message", "", optional<unsigned long>(4), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredNoIdNoErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign description");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "", "description", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredWithIdNoErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign description");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "", "description", optional<unsigned long>(4), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredNoIdWithErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign description");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "error message", "description", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorIgnoredWithIdWithErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Ign description");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(true, "error message", "description", optional<unsigned long>(4), status2);
}


// Test what happens with non-ignored errors.
TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredNoIdNoErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "", "", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredWithIdNoErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "", "", optional<unsigned long>(4), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredNoIdWithErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err");
    EXPECT_MSG(L"  error message");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "error message", "", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredWithIdWithErrorNoDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err");
    EXPECT_MSG(L"  error message 2");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "error message 2", "", optional<unsigned long>(4), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredNoIdNoErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err description");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "", "description", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredWithIdNoErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err description");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "", "description", optional<unsigned long>(4), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredNoIdWithErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err description");
    EXPECT_MSG(L"  error message");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "error message", "description", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, ErrorNotIgnoredWithIdWithErrorWithDescription)
{
  {
    InSequence dummy;

    EXPECT_MSG(L"Err description");
    EXPECT_MSG(L"  error message");
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->error(false, "error message", "description", optional<unsigned long>(4), status2);
}


TEST_P(CmdlineDownloadProgressDisplayTest, FileFinishedNoDescriptionNoId)
{
  {
    InSequence dummy;

    EXPECT_CALL(*msg, display_and_advance(_)).Times(0);
    EXPECT_CALL(*status_display, display_status(status1));
  }

  progress->file_finished("", optional<unsigned long>(), status1);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileFinishedWithDescriptionNoId)
{
  {
    InSequence dummy;
    EXPECT_CALL(*msg, display_and_advance(_)).Times(0);
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->file_finished("medusa cascade", optional<unsigned long>(), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileFinishedNoDescriptionWithId)
{
  {
    InSequence dummy;
    EXPECT_CALL(*msg, display_and_advance(_)).Times(0);
    EXPECT_CALL(*status_display, display_status(status1));
  }

  progress->file_finished("", optional<unsigned long>(5), status1);
}

TEST_P(CmdlineDownloadProgressDisplayTest, FileFinishedWithDescriptionWithId)
{
  {
    InSequence dummy;
    EXPECT_CALL(*msg, display_and_advance(_)).Times(0);
    EXPECT_CALL(*status_display, display_status(status2));
  }

  progress->file_finished("medusa cascade", optional<unsigned long>(5), status2);
}

TEST_P(CmdlineDownloadProgressDisplayTest, Done)
{
  EXPECT_MSG(L"Fetched 100B in 5s (20B/s)");

  progress->done(100, 5, 20);
}

class MockMediaChangeCallback
{
public:
  MOCK_METHOD1(media_change, void(bool));
};

TEST_P(CmdlineDownloadProgressDisplayTest, MediaChange)
{
  MockMediaChangeCallback callback;

  Mock::VerifyAndClearExpectations(term_input.get());
  {
    InSequence dummy;

    // This will happen even if display_messages is false:
    EXPECT_CALL(*msg, set_text(StrEq(L""))); // We have to clear the display first.
    EXPECT_CALL(*term_input, prompt_for_input(StrEq(L"Media change: Please insert the disc labeled 'banana' into the drive 'kiwi' and press [Enter].")))
      .WillOnce(Return(L"kumquat"));
    EXPECT_CALL(callback, media_change(true));
  }

  progress->media_change("banana", "kiwi",
                         sigc::mem_fun(callback, &MockMediaChangeCallback::media_change));
}

TEST_P(CmdlineDownloadProgressDisplayTest, MediaChangeEOF)
{
  MockMediaChangeCallback callback;

  Mock::VerifyAndClearExpectations(term_input.get());
  {
    InSequence dummy;

    // This will happen even if display_messages is false:
    EXPECT_CALL(*msg, set_text(StrEq(L""))); // We have to clear the display first.
    EXPECT_CALL(*term_input, prompt_for_input(StrEq(L"Media change: Please insert the disc labeled 'dev' into the drive 'null' and press [Enter].")))
      .WillOnce(Throw(StdinEOFException()));
    EXPECT_CALL(callback, media_change(false));
  }

  progress->media_change("dev", "null",
                         sigc::mem_fun(callback, &MockMediaChangeCallback::media_change));
}

TEST_P(CmdlineDownloadProgressDisplayTest, CompleteDoesNothing)
{
  progress->complete(4, 100, 3);
}

INSTANTIATE_TEST_CASE_P(WithMessages,
                        CmdlineDownloadProgressDisplayTest,
                        Values(true));

INSTANTIATE_TEST_CASE_P(WithoutMessages,
                        CmdlineDownloadProgressDisplayTest,
                        Values(false));