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/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace dom_distiller
{
18 MockDistillerFactory::MockDistillerFactory() {}
19 MockDistillerFactory::~MockDistillerFactory() {}
21 FakeDistiller::FakeDistiller(bool execute_callback
)
22 : execute_callback_(execute_callback
),
23 destruction_allowed_(true) {
24 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber());
27 FakeDistiller::FakeDistiller(
28 bool execute_callback
,
29 const base::Closure
& distillation_initiated_callback
)
30 : execute_callback_(execute_callback
),
31 destruction_allowed_(true),
32 distillation_initiated_callback_(distillation_initiated_callback
) {
33 EXPECT_CALL(*this, Die()).Times(testing::AnyNumber());
36 FakeDistiller::~FakeDistiller() {
37 EXPECT_TRUE(destruction_allowed_
);
41 void FakeDistiller::DistillPage(
43 scoped_ptr
<DistillerPage
> distiller_page
,
44 const DistillationFinishedCallback
& article_callback
,
45 const DistillationUpdateCallback
& page_callback
) {
47 article_callback_
= article_callback
;
48 page_callback_
= page_callback
;
49 if (!distillation_initiated_callback_
.is_null()) {
50 base::ResetAndReturn(&distillation_initiated_callback_
).Run();
52 if (execute_callback_
) {
53 scoped_ptr
<DistilledArticleProto
> proto(new DistilledArticleProto
);
54 proto
->add_pages()->set_url(url_
.spec());
55 PostDistillerCallback(proto
.Pass());
59 void FakeDistiller::RunDistillerCallback(
60 scoped_ptr
<DistilledArticleProto
> proto
) {
61 ASSERT_FALSE(execute_callback_
) << "Cannot explicitly run the distiller "
62 "callback for a fake distiller created "
63 "with automatic callback execution.";
64 PostDistillerCallback(proto
.Pass());
67 void FakeDistiller::RunDistillerUpdateCallback(
68 const ArticleDistillationUpdate
& update
) {
69 page_callback_
.Run(update
);
73 void FakeDistiller::PostDistillerCallback(
74 scoped_ptr
<DistilledArticleProto
> proto
) {
75 base::ThreadTaskRunnerHandle::Get()->PostTask(
76 FROM_HERE
, base::Bind(&FakeDistiller::RunDistillerCallbackInternal
,
77 base::Unretained(this), 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