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 5 #include "components/dom_distiller/core/fake_distiller.h" 6 7 #include "base/auto_reset.h" 8 #include "base/bind.h" 9 #include "base/callback_helpers.h" 10 #include "base/message_loop/message_loop.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace dom_distiller { 14 namespace test { 15 16 MockDistillerFactory::MockDistillerFactory() {} 17 MockDistillerFactory::~MockDistillerFactory() {} 18 19 FakeDistiller::FakeDistiller(bool execute_callback) 20 : execute_callback_(execute_callback), 21 destruction_allowed_(true) { 22 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber()); 23 } 24 25 FakeDistiller::FakeDistiller( 26 bool execute_callback, 27 const base::Closure& distillation_initiated_callback) 28 : execute_callback_(execute_callback), 29 destruction_allowed_(true), 30 distillation_initiated_callback_(distillation_initiated_callback) { 31 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber()); 32 } 33 34 FakeDistiller::~FakeDistiller() { 35 EXPECT_TRUE(destruction_allowed_); 36 Die(); 37 } 38 39 void FakeDistiller::DistillPage( 40 const GURL& url, 41 scoped_ptr<DistillerPage> distiller_page, 42 const DistillationFinishedCallback& article_callback, 43 const DistillationUpdateCallback& page_callback) { 44 url_ = url; 45 article_callback_ = article_callback; 46 page_callback_ = page_callback; 47 if (!distillation_initiated_callback_.is_null()) { 48 base::ResetAndReturn(&distillation_initiated_callback_).Run(); 49 } 50 if (execute_callback_) { 51 scoped_ptr<DistilledArticleProto> proto(new DistilledArticleProto); 52 proto->add_pages()->set_url(url_.spec()); 53 PostDistillerCallback(proto.Pass()); 54 } 55 } 56 57 void FakeDistiller::RunDistillerCallback( 58 scoped_ptr<DistilledArticleProto> proto) { 59 ASSERT_FALSE(execute_callback_) << "Cannot explicitly run the distiller " 60 "callback for a fake distiller created " 61 "with automatic callback execution."; 62 PostDistillerCallback(proto.Pass()); 63 } 64 65 void FakeDistiller::RunDistillerUpdateCallback( 66 const ArticleDistillationUpdate& update) { 67 page_callback_.Run(update); 68 } 69 70 71 void FakeDistiller::PostDistillerCallback( 72 scoped_ptr<DistilledArticleProto> proto) { 73 base::MessageLoop::current()->PostTask( 74 FROM_HERE, 75 base::Bind(&FakeDistiller::RunDistillerCallbackInternal, 76 base::Unretained(this), 77 base::Passed(&proto))); 78 } 79 80 void FakeDistiller::RunDistillerCallbackInternal( 81 scoped_ptr<DistilledArticleProto> proto) { 82 EXPECT_FALSE(article_callback_.is_null()); 83 84 base::AutoReset<bool> dont_delete_this_in_callback(&destruction_allowed_, 85 false); 86 article_callback_.Run(proto.Pass()); 87 article_callback_.Reset(); 88 } 89 90 } // namespace test 91 } // namespace dom_distiller 92