acqprogress_test.cc 5.5 KB

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