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 virtual ~FakeViewRequestDelegate() {}
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 virtual ~TestDomDistillerService() {}
31 MOCK_CONST_METHOD0(GetSyncableService
, syncer::SyncableService
*());
32 MOCK_METHOD3(AddToList
,
33 const std::string(const GURL
&,
35 const ArticleAvailableCallback
&));
36 virtual const std::string
AddToList(
38 scoped_ptr
<DistillerPage
> distiller_page
,
39 const ArticleAvailableCallback
& article_cb
) {
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 virtual scoped_ptr
<ViewerHandle
> ViewUrl(
50 scoped_ptr
<DistillerPage
> distiller_page
,
52 return scoped_ptr
<ViewerHandle
>(ViewUrlImpl());
54 MOCK_METHOD0(ViewEntryImpl
, ViewerHandle
*());
55 virtual scoped_ptr
<ViewerHandle
> ViewEntry(
57 scoped_ptr
<DistillerPage
> distiller_page
,
59 return scoped_ptr
<ViewerHandle
>(ViewEntryImpl());
61 MOCK_METHOD0(RemoveEntryImpl
, ArticleEntry
*());
62 virtual scoped_ptr
<ArticleEntry
> RemoveEntry(const std::string
&) {
63 return scoped_ptr
<ArticleEntry
>(RemoveEntryImpl());
65 virtual scoped_ptr
<DistillerPage
> CreateDefaultDistillerPage(
66 const gfx::Size
& render_view_size
) {
67 return scoped_ptr
<DistillerPage
>();
69 virtual scoped_ptr
<DistillerPage
> CreateDefaultDistillerPageWithHandle(
70 scoped_ptr
<SourcePageHandle
> handle
) {
71 return scoped_ptr
<DistillerPage
>();
73 virtual DistilledPagePrefs
* GetDistilledPagePrefs() OVERRIDE
;
76 class DomDistillerViewerTest
: public testing::Test
{
78 virtual void SetUp() OVERRIDE
{
79 service_
.reset(new TestDomDistillerService());
83 scoped_ptr
<ViewerHandle
> CreateViewRequest(
84 const std::string
& path
,
85 ViewRequestDelegate
* view_request_delegate
) {
86 return viewer::CreateViewRequest(
87 service_
.get(), path
, view_request_delegate
, gfx::Size());
90 scoped_ptr
<TestDomDistillerService
> service_
;
93 TEST_F(DomDistillerViewerTest
, TestCreatingViewUrlRequest
) {
94 scoped_ptr
<FakeViewRequestDelegate
> view_request_delegate(
95 new FakeViewRequestDelegate());
96 ViewerHandle
* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
97 EXPECT_CALL(*service_
.get(), ViewUrlImpl())
98 .WillOnce(testing::Return(viewer_handle
));
99 EXPECT_CALL(*service_
.get(), ViewEntryImpl()).Times(0);
101 std::string("?") + kUrlKey
+ "=http%3A%2F%2Fwww.example.com%2F",
102 view_request_delegate
.get());
105 TEST_F(DomDistillerViewerTest
, TestCreatingViewEntryRequest
) {
106 scoped_ptr
<FakeViewRequestDelegate
> view_request_delegate(
107 new FakeViewRequestDelegate());
108 ViewerHandle
* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
109 EXPECT_CALL(*service_
.get(), ViewEntryImpl())
110 .WillOnce(testing::Return(viewer_handle
));
111 EXPECT_CALL(*service_
.get(), ViewUrlImpl()).Times(0);
112 CreateViewRequest(std::string("?") + kEntryIdKey
+ "=abc-def",
113 view_request_delegate
.get());
116 TEST_F(DomDistillerViewerTest
, TestCreatingInvalidViewRequest
) {
117 scoped_ptr
<FakeViewRequestDelegate
> view_request_delegate(
118 new FakeViewRequestDelegate());
119 EXPECT_CALL(*service_
.get(), ViewEntryImpl()).Times(0);
120 EXPECT_CALL(*service_
.get(), ViewUrlImpl()).Times(0);
121 // Specify none of the required query parameters.
122 CreateViewRequest("?foo=bar", view_request_delegate
.get());
123 // Specify both of the required query parameters.
124 CreateViewRequest("?" + std::string(kUrlKey
) +
125 "=http%3A%2F%2Fwww.example.com%2F&" +
126 std::string(kEntryIdKey
) + "=abc-def",
127 view_request_delegate
.get());
128 // Specify an internal Chrome page.
129 CreateViewRequest("?" + std::string(kUrlKey
) + "=chrome%3A%2F%2Fsettings%2F",
130 view_request_delegate
.get());
131 // Specify a recursive URL.
132 CreateViewRequest("?" + std::string(kUrlKey
) + "=" +
133 std::string(kTestScheme
) + "%3A%2F%2Fabc-def%2F",
134 view_request_delegate
.get());
137 DistilledPagePrefs
* TestDomDistillerService::GetDistilledPagePrefs() {
141 TEST_F(DomDistillerViewerTest
, TestGetDistilledPageThemeJsOutput
) {
142 std::string kDarkJs
= "useTheme('dark');";
143 std::string kSepiaJs
= "useTheme('sepia');";
144 std::string kLightJs
= "useTheme('light');";
145 EXPECT_EQ(kDarkJs
.compare(viewer::GetDistilledPageThemeJs(
146 DistilledPagePrefs::DARK
)),
148 EXPECT_EQ(kLightJs
.compare(viewer::GetDistilledPageThemeJs(
149 DistilledPagePrefs::LIGHT
)),
151 EXPECT_EQ(kSepiaJs
.compare(viewer::GetDistilledPageThemeJs(
152 DistilledPagePrefs::SEPIA
)),
156 TEST_F(DomDistillerViewerTest
, TestGetDistilledPageFontFamilyJsOutput
) {
157 std::string kSerifJsFontFamily
= "useFontFamily('serif');";
158 std::string kMonospaceJsFontFamily
= "useFontFamily('monospace');";
159 std::string kSansSerifJsFontFamily
= "useFontFamily('sans-serif');";
160 EXPECT_EQ(kSerifJsFontFamily
.compare(viewer::GetDistilledPageFontFamilyJs(
161 DistilledPagePrefs::SERIF
)),
163 EXPECT_EQ(kMonospaceJsFontFamily
.compare(viewer::GetDistilledPageFontFamilyJs(
164 DistilledPagePrefs::MONOSPACE
)),
166 EXPECT_EQ(kSansSerifJsFontFamily
.compare(viewer::GetDistilledPageFontFamilyJs(
167 DistilledPagePrefs::SANS_SERIF
)),
171 } // namespace dom_distiller