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.
4
5#include "components/dom_distiller/core/viewer.h"
6
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"
13
14namespace dom_distiller {
15
16const char kTestScheme[] = "myscheme";
17
18class FakeViewRequestDelegate : public ViewRequestDelegate {
19 public:
20  virtual ~FakeViewRequestDelegate() {}
21  MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto));
22  MOCK_METHOD1(OnArticleUpdated,
23               void(ArticleDistillationUpdate article_update));
24};
25
26class TestDomDistillerService : public DomDistillerServiceInterface {
27 public:
28  TestDomDistillerService() {}
29  virtual ~TestDomDistillerService() {}
30
31  MOCK_CONST_METHOD0(GetSyncableService, syncer::SyncableService*());
32  MOCK_METHOD3(AddToList,
33               const std::string(const GURL&,
34                                 DistillerPage*,
35                                 const ArticleAvailableCallback&));
36  virtual const std::string AddToList(
37      const GURL& url,
38      scoped_ptr<DistillerPage> distiller_page,
39      const ArticleAvailableCallback& article_cb) {
40    return AddToList(url, distiller_page.get(), article_cb);
41  }
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(
49      ViewRequestDelegate*,
50      scoped_ptr<DistillerPage> distiller_page,
51      const GURL&) {
52    return scoped_ptr<ViewerHandle>(ViewUrlImpl());
53  }
54  MOCK_METHOD0(ViewEntryImpl, ViewerHandle*());
55  virtual scoped_ptr<ViewerHandle> ViewEntry(
56      ViewRequestDelegate*,
57      scoped_ptr<DistillerPage> distiller_page,
58      const std::string&) {
59    return scoped_ptr<ViewerHandle>(ViewEntryImpl());
60  }
61  MOCK_METHOD0(RemoveEntryImpl, ArticleEntry*());
62  virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string&) {
63    return scoped_ptr<ArticleEntry>(RemoveEntryImpl());
64  }
65  virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPage(
66      const gfx::Size& render_view_size) {
67    return scoped_ptr<DistillerPage>();
68  }
69  virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPageWithHandle(
70      scoped_ptr<SourcePageHandle> handle) {
71    return scoped_ptr<DistillerPage>();
72  }
73  virtual DistilledPagePrefs* GetDistilledPagePrefs() OVERRIDE;
74};
75
76class DomDistillerViewerTest : public testing::Test {
77 public:
78  virtual void SetUp() OVERRIDE {
79    service_.reset(new TestDomDistillerService());
80  }
81
82 protected:
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());
88  }
89
90  scoped_ptr<TestDomDistillerService> service_;
91};
92
93TEST_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);
100  CreateViewRequest(
101      std::string("?") + kUrlKey + "=http%3A%2F%2Fwww.example.com%2F",
102      view_request_delegate.get());
103}
104
105TEST_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());
114}
115
116TEST_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());
135}
136
137DistilledPagePrefs* TestDomDistillerService::GetDistilledPagePrefs() {
138  return NULL;
139}
140
141TEST_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)),
147            0);
148  EXPECT_EQ(kLightJs.compare(viewer::GetDistilledPageThemeJs(
149                DistilledPagePrefs::LIGHT)),
150            0);
151  EXPECT_EQ(kSepiaJs.compare(viewer::GetDistilledPageThemeJs(
152                DistilledPagePrefs::SEPIA)),
153            0);
154}
155
156TEST_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)),
162            0);
163  EXPECT_EQ(kMonospaceJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
164                DistilledPagePrefs::MONOSPACE)),
165            0);
166  EXPECT_EQ(kSansSerifJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
167                DistilledPagePrefs::SANS_SERIF)),
168            0);
169}
170
171}  // namespace dom_distiller
172