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