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/callback_helpers.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.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) { |
21 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber()); | 22 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber()); |
22 } | 23 } |
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 |
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 (!distillation_initiated_callback_.is_null()) { |
| 48 base::ResetAndReturn(&distillation_initiated_callback_).Run(); |
| 49 } |
37 if (execute_callback_) { | 50 if (execute_callback_) { |
38 scoped_ptr<DistilledArticleProto> proto(new DistilledArticleProto); | 51 scoped_ptr<DistilledArticleProto> proto(new DistilledArticleProto); |
39 proto->add_pages()->set_url(url_.spec()); | 52 proto->add_pages()->set_url(url_.spec()); |
40 PostDistillerCallback(proto.Pass()); | 53 PostDistillerCallback(proto.Pass()); |
41 } | 54 } |
42 } | 55 } |
43 | 56 |
44 void FakeDistiller::RunDistillerCallback( | 57 void FakeDistiller::RunDistillerCallback( |
45 scoped_ptr<DistilledArticleProto> proto) { | 58 scoped_ptr<DistilledArticleProto> proto) { |
46 ASSERT_FALSE(execute_callback_) << "Cannot explicitly run the distiller " | 59 ASSERT_FALSE(execute_callback_) << "Cannot explicitly run the distiller " |
47 "callback for a fake distiller created " | 60 "callback for a fake distiller created " |
48 "with automatic callback execution."; | 61 "with automatic callback execution."; |
49 PostDistillerCallback(proto.Pass()); | 62 PostDistillerCallback(proto.Pass()); |
50 } | 63 } |
51 | 64 |
| 65 void FakeDistiller::RunDistillerUpdateCallback( |
| 66 const ArticleDistillationUpdate& update) { |
| 67 page_callback_.Run(update); |
| 68 } |
| 69 |
| 70 |
52 void FakeDistiller::PostDistillerCallback( | 71 void FakeDistiller::PostDistillerCallback( |
53 scoped_ptr<DistilledArticleProto> proto) { | 72 scoped_ptr<DistilledArticleProto> proto) { |
54 base::MessageLoop::current()->PostTask( | 73 base::MessageLoop::current()->PostTask( |
55 FROM_HERE, | 74 FROM_HERE, |
56 base::Bind(&FakeDistiller::RunDistillerCallbackInternal, | 75 base::Bind(&FakeDistiller::RunDistillerCallbackInternal, |
57 base::Unretained(this), | 76 base::Unretained(this), |
58 base::Passed(&proto))); | 77 base::Passed(&proto))); |
59 } | 78 } |
60 | 79 |
61 void FakeDistiller::RunDistillerCallbackInternal( | 80 void FakeDistiller::RunDistillerCallbackInternal( |
62 scoped_ptr<DistilledArticleProto> proto) { | 81 scoped_ptr<DistilledArticleProto> proto) { |
63 EXPECT_FALSE(article_callback_.is_null()); | 82 EXPECT_FALSE(article_callback_.is_null()); |
64 | 83 |
65 base::AutoReset<bool> dont_delete_this_in_callback(&destruction_allowed_, | 84 base::AutoReset<bool> dont_delete_this_in_callback(&destruction_allowed_, |
66 false); | 85 false); |
67 article_callback_.Run(proto.Pass()); | 86 article_callback_.Run(proto.Pass()); |
68 article_callback_.Reset(); | 87 article_callback_.Reset(); |
69 } | 88 } |
70 | 89 |
71 } // namespace test | 90 } // namespace test |
72 } // namespace dom_distiller | 91 } // namespace dom_distiller |
OLD | NEW |