acqprogress_test.cc 5.2 KB

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