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.
5 #include "components/dom_distiller/core/fake_distiller.h"
7 #include "base/auto_reset.h"
9 #include "base/callback_helpers.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace dom_distiller
{
16 MockDistillerFactory::MockDistillerFactory() {}
17 MockDistillerFactory::~MockDistillerFactory() {}
19 FakeDistiller::FakeDistiller(bool execute_callback
)
20 : execute_callback_(execute_callback
),
21 destruction_allowed_(true) {
22 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber());
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());
34 FakeDistiller::~FakeDistiller() {
35 EXPECT_TRUE(destruction_allowed_
);
39 void FakeDistiller::DistillPage(
41 scoped_ptr
<DistillerPage
> distiller_page
,
42 const DistillationFinishedCallback
& article_callback
,
43 const DistillationUpdateCallback
& page_callback
) {
45 article_callback_
= article_callback
;
46 page_callback_
= page_callback
;
47 if (!distillation_initiated_callback_
.is_null()) {
48 base::ResetAndReturn(&distillation_initiated_callback_
).Run();
50 if (execute_callback_
) {
51 scoped_ptr
<DistilledArticleProto
> proto(new DistilledArticleProto
);
52 proto
->add_pages()->set_url(url_
.spec());
53 PostDistillerCallback(proto
.Pass());
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());
65 void FakeDistiller::RunDistillerUpdateCallback(
66 const ArticleDistillationUpdate
& update
) {
67 page_callback_
.Run(update
);
71 void FakeDistiller::PostDistillerCallback(
72 scoped_ptr
<DistilledArticleProto
> proto
) {
73 base::MessageLoop::current()->PostTask(
75 base::Bind(&FakeDistiller::RunDistillerCallbackInternal
,
76 base::Unretained(this),
77 base::Passed(&proto
)));
80 void FakeDistiller::RunDistillerCallbackInternal(
81 scoped_ptr
<DistilledArticleProto
> proto
) {
82 EXPECT_FALSE(article_callback_
.is_null());
84 base::AutoReset
<bool> dont_delete_this_in_callback(&destruction_allowed_
,
86 article_callback_
.Run(proto
.Pass());
87 article_callback_
.Reset();
91 } // namespace dom_distiller