1 // Copyright 2014 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/viewer.h"
7 #include "components/dom_distiller/core/distilled_page_prefs.h"
8 #include "components/dom_distiller/core/dom_distiller_service.h"
9 #include "components/dom_distiller/core/dom_distiller_test_util.h"
10 #include "components/dom_distiller/core/task_tracker.h"
11 #include "components/dom_distiller/core/url_constants.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace dom_distiller
{
16 const char kTestScheme
[] = "myscheme";
18 class FakeViewRequestDelegate
: public ViewRequestDelegate
{
20 ~FakeViewRequestDelegate() override
{}
21 MOCK_METHOD1(OnArticleReady
, void(const DistilledArticleProto
* proto
));
22 MOCK_METHOD1(OnArticleUpdated
,
23 void(ArticleDistillationUpdate article_update
));
26 class TestDomDistillerService
: public DomDistillerServiceInterface
{
28 TestDomDistillerService() {}
29 ~TestDomDistillerService() override
{}
31 MOCK_CONST_METHOD0(GetSyncableService
, syncer::SyncableService
*());
32 MOCK_METHOD3(AddToList
,
33 const std::string(const GURL
&,
35 const ArticleAvailableCallback
&));
36 const std::string
AddToList(
38 scoped_ptr
<DistillerPage
> distiller_page
,
39 const ArticleAvailableCallback
& article_cb
) override
{
40 return AddToList(url
, distiller_page
.get(), article_cb
);
42 MOCK_METHOD1(HasEntry
, bool(const std::string
&));
43 MOCK_METHOD1(GetUrlForEntry
, std::string(const std::string
&));
44 MOCK_CONST_METHOD0(GetEntries
, std::vector
<ArticleEntry
>());
45 MOCK_METHOD1(AddObserver
, void(DomDistillerObserver
*));
46 MOCK_METHOD1(RemoveObserver
, void(DomDistillerObserver
*));
47 MOCK_METHOD0(ViewUrlImpl
, ViewerHandle
*());
48 scoped_ptr
<ViewerHandle
> ViewUrl(
50 scoped_ptr
<DistillerPage
> distiller_page
,
51 const GURL
&) override
{
52 return scoped_ptr
<ViewerHandle
>(ViewUrlImpl());
54 MOCK_METHOD0(ViewEntryImpl
, ViewerHandle
*());
55 scoped_ptr
<ViewerHandle
> ViewEntry(
57 scoped_ptr
<DistillerPage
> distiller_page
,
58 const std::string
&) override
{
59 return scoped_ptr
<ViewerHandle
>(ViewEntryImpl());
61 MOCK_METHOD0(RemoveEntryImpl
, ArticleEntry
*());
62 scoped_ptr
<ArticleEntry
> RemoveEntry(const std::string
&) override
{
63 return scoped_ptr
<ArticleEntry
>(RemoveEntryImpl());
65 scoped_ptr
<DistillerPage
> CreateDefaultDistillerPage(
66 const gfx::Size
& render_view_size
) override
{
67 return scoped_ptr
<DistillerPage
>();
69 scoped_ptr
<DistillerPage
> CreateDefaultDistillerPageWithHandle(
70 scoped_ptr
<SourcePageHandle
> handle
) override
{
71 return scoped_ptr
<DistillerPage
>();
73 DistilledPagePrefs
* GetDistilledPagePrefs() override
;
76 class DomDistillerViewerTest
: public testing::Test
{
78 void SetUp() override
{ service_
.reset(new TestDomDistillerService()); }
81 scoped_ptr
<ViewerHandle
> CreateViewRequest(
82 const std::string
& path
,
83 ViewRequestDelegate
* view_request_delegate
) {
84 return viewer::CreateViewRequest(
85 service_
.get(), path
, view_request_delegate
, gfx::Size());
88 scoped_ptr
<TestDomDistillerService
> service_
;
91 TEST_F(DomDistillerViewerTest
, TestCreatingViewUrlRequest
) {
92 scoped_ptr
<FakeViewRequestDelegate
> view_request_delegate(
93 new FakeViewRequestDelegate());
94 ViewerHandle
* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
95 EXPECT_CALL(*service_
.get(), ViewUrlImpl())
96 .WillOnce(testing::Return(viewer_handle
));
97 EXPECT_CALL(*service_
.get(), ViewEntryImpl()).Times(0);
99 std::string("?") + kUrlKey
+ "=http%3A%2F%2Fwww.example.com%2F",
100 view_request_delegate
.get());
103 TEST_F(DomDistillerViewerTest
, TestCreatingViewEntryRequest
) {
104 scoped_ptr
<FakeViewRequestDelegate
> view_request_delegate(
105 new FakeViewRequestDelegate());
106 ViewerHandle
* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
107 EXPECT_CALL(*service_
.get(), ViewEntryImpl())
108 .WillOnce(testing::Return(viewer_handle
));
109 EXPECT_CALL(*service_
.get(), ViewUrlImpl()).Times(0);
110 CreateViewRequest(std::string("?") + kEntryIdKey
+ "=abc-def",
111 view_request_delegate
.get());
114 TEST_F(DomDistillerViewerTest
, TestCreatingInvalidViewRequest
) {
115 scoped_ptr
<FakeViewRequestDelegate
> view_request_delegate(
116 new FakeViewRequestDelegate());
117 EXPECT_CALL(*service_
.get(), ViewEntryImpl()).Times(0);
118 EXPECT_CALL(*service_
.get(), ViewUrlImpl()).Times(0);
119 // Specify none of the required query parameters.
120 CreateViewRequest("?foo=bar", view_request_delegate
.get());
121 // Specify both of the required query parameters.
122 CreateViewRequest("?" + std::string(kUrlKey
) +
123 "=http%3A%2F%2Fwww.example.com%2F&" +
124 std::string(kEntryIdKey
) + "=abc-def",
125 view_request_delegate
.get());
126 // Specify an internal Chrome page.
127 CreateViewRequest("?" + std::string(kUrlKey
) + "=chrome%3A%2F%2Fsettings%2F",
128 view_request_delegate
.get());
129 // Specify a recursive URL.
130 CreateViewRequest("?" + std::string(kUrlKey
) + "=" +
131 std::string(kTestScheme
) + "%3A%2F%2Fabc-def%2F",
132 view_request_delegate
.get());
135 DistilledPagePrefs
* TestDomDistillerService::GetDistilledPagePrefs() {
139 TEST_F(DomDistillerViewerTest
, TestGetDistilledPageThemeJsOutput
) {
140 std::string kDarkJs
= "useTheme('dark');";
141 std::string kSepiaJs
= "useTheme('sepia');";
142 std::string kLightJs
= "useTheme('light');";
143 EXPECT_EQ(kDarkJs
.compare(viewer::GetDistilledPageThemeJs(
144 DistilledPagePrefs::DARK
)),
146 EXPECT_EQ(kLightJs
.compare(viewer::GetDistilledPageThemeJs(
147 DistilledPagePrefs::LIGHT
)),
149 EXPECT_EQ(kSepiaJs
.compare(viewer::GetDistilledPageThemeJs(
150 DistilledPagePrefs::SEPIA
)),
154 TEST_F(DomDistillerViewerTest
, TestGetDistilledPageFontFamilyJsOutput
) {
155 std::string kSerifJsFontFamily
= "useFontFamily('serif');";
156 std::string kMonospaceJsFontFamily
= "useFontFamily('monospace');";
157 std::string kSansSerifJsFontFamily
= "useFontFamily('sans-serif');";
158 EXPECT_EQ(kSerifJsFontFamily
.compare(viewer::GetDistilledPageFontFamilyJs(
159 DistilledPagePrefs::SERIF
)),
161 EXPECT_EQ(kMonospaceJsFontFamily
.compare(viewer::GetDistilledPageFontFamilyJs(
162 DistilledPagePrefs::MONOSPACE
)),
164 EXPECT_EQ(kSansSerifJsFontFamily
.compare(viewer::GetDistilledPageFontFamilyJs(
165 DistilledPagePrefs::SANS_SERIF
)),
169 } // namespace dom_distiller