1a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// found in the LICENSE file.
4a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
5a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "components/dom_distiller/core/viewer.h"
6a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/dom_distiller/core/distilled_page_prefs.h"
8a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "components/dom_distiller/core/dom_distiller_service.h"
9a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "components/dom_distiller/core/dom_distiller_test_util.h"
10a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "components/dom_distiller/core/task_tracker.h"
11a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "components/dom_distiller/core/url_constants.h"
12a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "testing/gtest/include/gtest/gtest.h"
13a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
14a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochnamespace dom_distiller {
15a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
16a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochconst char kTestScheme[] = "myscheme";
17a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
18a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass FakeViewRequestDelegate : public ViewRequestDelegate {
19a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public:
20a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual ~FakeViewRequestDelegate() {}
21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto));
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD1(OnArticleUpdated,
23a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch               void(ArticleDistillationUpdate article_update));
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch};
25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
26a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass TestDomDistillerService : public DomDistillerServiceInterface {
27a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public:
28a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  TestDomDistillerService() {}
29a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual ~TestDomDistillerService() {}
30a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_CONST_METHOD0(GetSyncableService, syncer::SyncableService*());
325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  MOCK_METHOD3(AddToList,
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu               const std::string(const GURL&,
345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                                 DistillerPage*,
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                                 const ArticleAvailableCallback&));
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  virtual const std::string AddToList(
375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const GURL& url,
385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      scoped_ptr<DistillerPage> distiller_page,
395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const ArticleAvailableCallback& article_cb) {
405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return AddToList(url, distiller_page.get(), article_cb);
415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  MOCK_METHOD1(HasEntry, bool(const std::string&));
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  MOCK_METHOD1(GetUrlForEntry, std::string(const std::string&));
44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_CONST_METHOD0(GetEntries, std::vector<ArticleEntry>());
45a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD1(AddObserver, void(DomDistillerObserver*));
46a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD1(RemoveObserver, void(DomDistillerObserver*));
47a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD0(ViewUrlImpl, ViewerHandle*());
485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  virtual scoped_ptr<ViewerHandle> ViewUrl(
495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      ViewRequestDelegate*,
505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      scoped_ptr<DistillerPage> distiller_page,
515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const GURL&) {
52a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return scoped_ptr<ViewerHandle>(ViewUrlImpl());
53a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD0(ViewEntryImpl, ViewerHandle*());
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  virtual scoped_ptr<ViewerHandle> ViewEntry(
565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      ViewRequestDelegate*,
575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      scoped_ptr<DistillerPage> distiller_page,
585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const std::string&) {
59a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return scoped_ptr<ViewerHandle>(ViewEntryImpl());
60a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
61a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  MOCK_METHOD0(RemoveEntryImpl, ArticleEntry*());
62a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string&) {
63a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return scoped_ptr<ArticleEntry>(RemoveEntryImpl());
64a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPage(
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const gfx::Size& render_view_size) {
675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return scoped_ptr<DistillerPage>();
685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPageWithHandle(
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<SourcePageHandle> handle) {
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return scoped_ptr<DistillerPage>();
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual DistilledPagePrefs* GetDistilledPagePrefs() OVERRIDE;
74a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch};
75a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass DomDistillerViewerTest : public testing::Test {
77a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public:
78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual void SetUp() OVERRIDE {
79a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    service_.reset(new TestDomDistillerService());
80a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
81a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
82a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch protected:
83a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<ViewerHandle> CreateViewRequest(
84a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      const std::string& path,
85a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      ViewRequestDelegate* view_request_delegate) {
86a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return viewer::CreateViewRequest(
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        service_.get(), path, view_request_delegate, gfx::Size());
88a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
89a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
90a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<TestDomDistillerService> service_;
91a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch};
92a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
93a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochTEST_F(DomDistillerViewerTest, TestCreatingViewUrlRequest) {
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<FakeViewRequestDelegate> view_request_delegate(
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new FakeViewRequestDelegate());
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ViewerHandle* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  EXPECT_CALL(*service_.get(), ViewUrlImpl())
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      .WillOnce(testing::Return(viewer_handle));
99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  EXPECT_CALL(*service_.get(), ViewEntryImpl()).Times(0);
100a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CreateViewRequest(
101a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      std::string("?") + kUrlKey + "=http%3A%2F%2Fwww.example.com%2F",
102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      view_request_delegate.get());
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
105a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochTEST_F(DomDistillerViewerTest, TestCreatingViewEntryRequest) {
106a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<FakeViewRequestDelegate> view_request_delegate(
107a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new FakeViewRequestDelegate());
108a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ViewerHandle* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
109a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  EXPECT_CALL(*service_.get(), ViewEntryImpl())
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      .WillOnce(testing::Return(viewer_handle));
111a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  EXPECT_CALL(*service_.get(), ViewUrlImpl()).Times(0);
112a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CreateViewRequest(std::string("?") + kEntryIdKey + "=abc-def",
113a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                    view_request_delegate.get());
114a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
115a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
116a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochTEST_F(DomDistillerViewerTest, TestCreatingInvalidViewRequest) {
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<FakeViewRequestDelegate> view_request_delegate(
118a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new FakeViewRequestDelegate());
119a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  EXPECT_CALL(*service_.get(), ViewEntryImpl()).Times(0);
120a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  EXPECT_CALL(*service_.get(), ViewUrlImpl()).Times(0);
121a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Specify none of the required query parameters.
122a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CreateViewRequest("?foo=bar", view_request_delegate.get());
123a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Specify both of the required query parameters.
124a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CreateViewRequest("?" + std::string(kUrlKey) +
125a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                        "=http%3A%2F%2Fwww.example.com%2F&" +
126a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                        std::string(kEntryIdKey) + "=abc-def",
127a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                    view_request_delegate.get());
128a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Specify an internal Chrome page.
129a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CreateViewRequest("?" + std::string(kUrlKey) + "=chrome%3A%2F%2Fsettings%2F",
130a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                    view_request_delegate.get());
131a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Specify a recursive URL.
132a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CreateViewRequest("?" + std::string(kUrlKey) + "=" +
133a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                        std::string(kTestScheme) + "%3A%2F%2Fabc-def%2F",
134a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                    view_request_delegate.get());
135a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
136a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
137116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDistilledPagePrefs* TestDomDistillerService::GetDistilledPagePrefs() {
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return NULL;
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
141116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(DomDistillerViewerTest, TestGetDistilledPageThemeJsOutput) {
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string kDarkJs = "useTheme('dark');";
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string kSepiaJs = "useTheme('sepia');";
144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string kLightJs = "useTheme('light');";
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(kDarkJs.compare(viewer::GetDistilledPageThemeJs(
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                DistilledPagePrefs::DARK)),
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            0);
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(kLightJs.compare(viewer::GetDistilledPageThemeJs(
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                DistilledPagePrefs::LIGHT)),
150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            0);
151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(kSepiaJs.compare(viewer::GetDistilledPageThemeJs(
152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                DistilledPagePrefs::SEPIA)),
153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            0);
154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_F(DomDistillerViewerTest, TestGetDistilledPageFontFamilyJsOutput) {
1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string kSerifJsFontFamily = "useFontFamily('serif');";
1586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string kMonospaceJsFontFamily = "useFontFamily('monospace');";
1596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string kSansSerifJsFontFamily = "useFontFamily('sans-serif');";
1606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(kSerifJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
1616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                DistilledPagePrefs::SERIF)),
1626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            0);
1636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(kMonospaceJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
1646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                DistilledPagePrefs::MONOSPACE)),
1656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            0);
1666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(kSansSerifJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
1676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                DistilledPagePrefs::SANS_SERIF)),
1686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            0);
1696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
1706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
171a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}  // namespace dom_distiller
172