acqprogress_test.cc 5.1 KB

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