]> git.saurik.com Git - apt.git/blame_incremental - test/libapt/acqprogress_test.cc
allow ratelimiting progress reporting for testcases
[apt.git] / test / libapt / acqprogress_test.cc
... / ...
CommitLineData
1#include <config.h>
2#include <apt-pkg/hashes.h>
3#include <apt-pkg/acquire.h>
4#include <apt-pkg/acquire-item.h>
5#include <apt-pkg/configuration.h>
6#include <apt-private/acqprogress.h>
7#include <string>
8#include <sstream>
9#include <gtest/gtest.h>
10
11class TestItem: public pkgAcquire::Item
12{
13public:
14 TestItem(pkgAcquire * const Acq) : pkgAcquire::Item(Acq) {}
15
16 virtual std::string DescURI() const { return ""; }
17 virtual HashStringList GetExpectedHashes() const { return HashStringList(); }
18
19};
20
21TEST(AcqProgress, IMSHit)
22{
23 std::ostringstream out;
24 unsigned int width = 80;
25 AcqTextStatus Stat(out, width, 0);
26 Stat.Start();
27
28 pkgAcquire::ItemDesc hit;
29 hit.URI = "http://example.org/file";
30 hit.Description = "Example File from example.org";
31 hit.ShortDesc = "Example File";
32 hit.Owner = NULL;
33
34 EXPECT_EQ("", out.str());
35 Stat.IMSHit(hit);
36 EXPECT_EQ("Hit Example File from example.org\n", out.str());
37 Stat.IMSHit(hit);
38 EXPECT_EQ("Hit Example File from example.org\n"
39 "Hit Example File from example.org\n", out.str());
40 Stat.Stop();
41 EXPECT_EQ("Hit Example File from example.org\n"
42 "Hit Example File from example.org\n", out.str());
43}
44TEST(AcqProgress, FetchNoFileSize)
45{
46 std::ostringstream out;
47 unsigned int width = 80;
48 AcqTextStatus Stat(out, width, 0);
49 Stat.Start();
50
51 pkgAcquire Acq(&Stat);
52 pkgAcquire::ItemDesc fetch;
53 fetch.URI = "http://example.org/file";
54 fetch.Description = "Example File from example.org";
55 fetch.ShortDesc = "Example File";
56 TestItem fetchO(&Acq);
57 fetch.Owner = &fetchO;
58
59 EXPECT_EQ("", out.str());
60 Stat.Fetch(fetch);
61 EXPECT_EQ("Get:1 Example File from example.org\n", out.str());
62 Stat.Fetch(fetch);
63 EXPECT_EQ("Get:1 Example File from example.org\n"
64 "Get:2 Example File from example.org\n", out.str());
65 Stat.Stop();
66 EXPECT_EQ("Get:1 Example File from example.org\n"
67 "Get:2 Example File from example.org\n", out.str());
68}
69TEST(AcqProgress, FetchFileSize)
70{
71 std::ostringstream out;
72 unsigned int width = 80;
73 AcqTextStatus Stat(out, width, 0);
74 Stat.Start();
75
76 pkgAcquire Acq(&Stat);
77 pkgAcquire::ItemDesc fetch;
78 fetch.URI = "http://example.org/file";
79 fetch.Description = "Example File from example.org";
80 fetch.ShortDesc = "Example File";
81 TestItem fetchO(&Acq);
82 fetchO.FileSize = 100;
83 fetch.Owner = &fetchO;
84
85 EXPECT_EQ("", out.str());
86 Stat.Fetch(fetch);
87 EXPECT_EQ("Get:1 Example File from example.org [100 B]\n", out.str());
88 fetchO.FileSize = 42;
89 Stat.Fetch(fetch);
90 EXPECT_EQ("Get:1 Example File from example.org [100 B]\n"
91 "Get:2 Example File from example.org [42 B]\n", out.str());
92 Stat.Stop();
93 EXPECT_EQ("Get:1 Example File from example.org [100 B]\n"
94 "Get:2 Example File from example.org [42 B]\n", out.str());
95}
96TEST(AcqProgress, Fail)
97{
98 std::ostringstream out;
99 unsigned int width = 80;
100 AcqTextStatus Stat(out, width, 0);
101 Stat.Start();
102
103 pkgAcquire Acq(&Stat);
104 pkgAcquire::ItemDesc fetch;
105 fetch.URI = "http://example.org/file";
106 fetch.Description = "Example File from example.org";
107 fetch.ShortDesc = "Example File";
108 TestItem fetchO(&Acq);
109 fetchO.FileSize = 100;
110 fetchO.Status = pkgAcquire::Item::StatIdle;
111 fetch.Owner = &fetchO;
112
113 EXPECT_EQ("", out.str());
114 Stat.Fail(fetch);
115 EXPECT_EQ("", out.str());
116 fetchO.Status = pkgAcquire::Item::StatDone;
117 Stat.Fail(fetch);
118 EXPECT_EQ("Ign Example File from example.org\n", out.str());
119 fetchO.Status = pkgAcquire::Item::StatError;
120 fetchO.ErrorText = "An error test!";
121 Stat.Fail(fetch);
122 EXPECT_EQ("Ign Example File from example.org\n"
123 "Err Example File from example.org\n"
124 " An error test!\n", out.str());
125 _config->Set("Acquire::Progress::Ignore::ShowErrorText", true);
126 fetchO.Status = pkgAcquire::Item::StatDone;
127 Stat.Fail(fetch);
128 EXPECT_EQ("Ign Example File from example.org\n"
129 "Err Example File from example.org\n"
130 " An error test!\n"
131 "Ign Example File from example.org\n"
132 " An error test!\n", out.str());
133 _config->Set("Acquire::Progress::Ignore::ShowErrorText", true);
134 Stat.Stop();
135 EXPECT_EQ("Ign Example File from example.org\n"
136 "Err Example File from example.org\n"
137 " An error test!\n"
138 "Ign Example File from example.org\n"
139 " An error test!\n", out.str());
140}
141TEST(AcqProgress, Pulse)
142{
143 std::ostringstream out;
144 unsigned int width = 80;
145 AcqTextStatus Stat(out, width, 0);
146 _config->Set("APT::Sandbox::User", ""); // ensure we aren't sandboxing
147
148 pkgAcquire Acq(&Stat);
149 pkgAcquire::ItemDesc fetch;
150 fetch.URI = "http://example.org/file";
151 fetch.Description = "Example File from example.org";
152 fetch.ShortDesc = "Example File";
153 TestItem fetchO(&Acq);
154 fetchO.FileSize = 100;
155 fetchO.Status = pkgAcquire::Item::StatFetching;
156 fetch.Owner = &fetchO;
157
158 // make screen smaller and bigger again while running
159 EXPECT_TRUE(Stat.Pulse(&Acq));
160 EXPECT_EQ("\r0% [Working]", out.str());
161 width = 8;
162 EXPECT_TRUE(Stat.Pulse(&Acq));
163 EXPECT_EQ("\r0% [Working]"
164 "\r "
165 "\r0% [Work", out.str());
166 width = 80;
167 EXPECT_TRUE(Stat.Pulse(&Acq));
168 EXPECT_EQ("\r0% [Working]"
169 "\r "
170 "\r0% [Work"
171 "\r0% [Working]", out.str());
172}