dom_distiller_service_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/dom_distiller/core/dom_distiller_service.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/run_loop.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/test/task_environment.h"
  13. #include "components/dom_distiller/core/article_entry.h"
  14. #include "components/dom_distiller/core/distilled_page_prefs.h"
  15. #include "components/dom_distiller/core/fake_distiller.h"
  16. #include "components/dom_distiller/core/fake_distiller_page.h"
  17. #include "components/dom_distiller/core/task_tracker.h"
  18. #include "testing/gmock/include/gmock/gmock.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. using testing::_;
  21. using testing::Return;
  22. namespace dom_distiller {
  23. namespace test {
  24. namespace {
  25. class FakeViewRequestDelegate : public ViewRequestDelegate {
  26. public:
  27. ~FakeViewRequestDelegate() override = default;
  28. MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto));
  29. MOCK_METHOD1(OnArticleUpdated,
  30. void(ArticleDistillationUpdate article_update));
  31. };
  32. void RunDistillerCallback(FakeDistiller* distiller,
  33. std::unique_ptr<DistilledArticleProto> proto) {
  34. distiller->RunDistillerCallback(std::move(proto));
  35. base::RunLoop().RunUntilIdle();
  36. }
  37. std::unique_ptr<DistilledArticleProto> CreateArticleWithURL(
  38. const std::string& url) {
  39. std::unique_ptr<DistilledArticleProto> proto(new DistilledArticleProto);
  40. DistilledPageProto* page = proto->add_pages();
  41. page->set_url(url);
  42. return proto;
  43. }
  44. std::unique_ptr<DistilledArticleProto> CreateDefaultArticle() {
  45. return CreateArticleWithURL("http://www.example.com/default_article_page1");
  46. }
  47. } // namespace
  48. class DomDistillerServiceTest : public testing::Test {
  49. public:
  50. void SetUp() override {
  51. distiller_factory_ = new MockDistillerFactory();
  52. distiller_page_factory_ = new MockDistillerPageFactory();
  53. service_ = std::make_unique<DomDistillerService>(
  54. std::unique_ptr<DistillerFactory>(distiller_factory_),
  55. std::unique_ptr<DistillerPageFactory>(distiller_page_factory_),
  56. /* distilled_page_prefs */ nullptr,
  57. /* distiller_ui_handle */ nullptr);
  58. }
  59. void TearDown() override {
  60. base::RunLoop().RunUntilIdle();
  61. distiller_factory_ = nullptr;
  62. service_.reset();
  63. }
  64. protected:
  65. base::test::SingleThreadTaskEnvironment task_environment_;
  66. raw_ptr<MockDistillerFactory> distiller_factory_;
  67. raw_ptr<MockDistillerPageFactory> distiller_page_factory_;
  68. std::unique_ptr<DomDistillerService> service_;
  69. };
  70. TEST_F(DomDistillerServiceTest, TestViewUrl) {
  71. FakeDistiller* distiller = new FakeDistiller(false);
  72. EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
  73. .WillOnce(Return(distiller));
  74. FakeViewRequestDelegate viewer_delegate;
  75. GURL url("http://www.example.com/p1");
  76. std::unique_ptr<ViewerHandle> handle = service_->ViewUrl(
  77. &viewer_delegate, service_->CreateDefaultDistillerPage(gfx::Size()), url);
  78. ASSERT_FALSE(distiller->GetArticleCallback().is_null());
  79. EXPECT_EQ(url, distiller->GetUrl());
  80. std::unique_ptr<DistilledArticleProto> proto = CreateDefaultArticle();
  81. EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get()));
  82. RunDistillerCallback(distiller, std::move(proto));
  83. }
  84. TEST_F(DomDistillerServiceTest, TestMultipleViewUrl) {
  85. FakeDistiller* distiller = new FakeDistiller(false);
  86. FakeDistiller* distiller2 = new FakeDistiller(false);
  87. EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
  88. .WillOnce(Return(distiller))
  89. .WillOnce(Return(distiller2));
  90. FakeViewRequestDelegate viewer_delegate;
  91. FakeViewRequestDelegate viewer_delegate2;
  92. GURL url("http://www.example.com/p1");
  93. GURL url2("http://www.example.com/a/p1");
  94. std::unique_ptr<ViewerHandle> handle = service_->ViewUrl(
  95. &viewer_delegate, service_->CreateDefaultDistillerPage(gfx::Size()), url);
  96. std::unique_ptr<ViewerHandle> handle2 = service_->ViewUrl(
  97. &viewer_delegate2, service_->CreateDefaultDistillerPage(gfx::Size()),
  98. url2);
  99. ASSERT_FALSE(distiller->GetArticleCallback().is_null());
  100. EXPECT_EQ(url, distiller->GetUrl());
  101. std::unique_ptr<DistilledArticleProto> proto = CreateDefaultArticle();
  102. EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get()));
  103. RunDistillerCallback(distiller, std::move(proto));
  104. ASSERT_FALSE(distiller2->GetArticleCallback().is_null());
  105. EXPECT_EQ(url2, distiller2->GetUrl());
  106. std::unique_ptr<DistilledArticleProto> proto2 = CreateDefaultArticle();
  107. EXPECT_CALL(viewer_delegate2, OnArticleReady(proto2.get()));
  108. RunDistillerCallback(distiller2, std::move(proto2));
  109. }
  110. TEST_F(DomDistillerServiceTest, TestViewUrlCancelled) {
  111. FakeDistiller* distiller = new FakeDistiller(false);
  112. EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
  113. .WillOnce(Return(distiller));
  114. bool distiller_destroyed = false;
  115. EXPECT_CALL(*distiller, Die())
  116. .WillOnce(testing::Assign(&distiller_destroyed, true));
  117. FakeViewRequestDelegate viewer_delegate;
  118. GURL url("http://www.example.com/p1");
  119. std::unique_ptr<ViewerHandle> handle = service_->ViewUrl(
  120. &viewer_delegate, service_->CreateDefaultDistillerPage(gfx::Size()), url);
  121. ASSERT_FALSE(distiller->GetArticleCallback().is_null());
  122. EXPECT_EQ(url, distiller->GetUrl());
  123. EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0);
  124. EXPECT_FALSE(distiller_destroyed);
  125. handle.reset();
  126. base::RunLoop().RunUntilIdle();
  127. EXPECT_TRUE(distiller_destroyed);
  128. }
  129. } // namespace test
  130. } // namespace dom_distiller