File: viewer_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 6,108 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dom_distiller/core/viewer.h"

#include <memory>

#include "components/dom_distiller/core/distilled_page_prefs.h"
#include "components/dom_distiller/core/distiller_ui_handle.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
#include "components/dom_distiller/core/task_tracker.h"
#include "components/dom_distiller/core/url_constants.h"
#include "components/dom_distiller/core/url_utils.h"
#include "net/base/url_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/url_util.h"

namespace dom_distiller {

namespace {

const GURL GetDistillerViewUrlFromUrl(const std::string& url) {
  return url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url),
                                               "Title");
}

const GURL GetDistillerViewUrlFromEntryId(const std::string& id) {
  return url_utils::GetDistillerViewUrlFromEntryId(kDomDistillerScheme, id);
}

}  // namespace

class FakeViewRequestDelegate : public ViewRequestDelegate {
 public:
  ~FakeViewRequestDelegate() override = default;
  MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto));
  MOCK_METHOD1(OnArticleUpdated,
               void(ArticleDistillationUpdate article_update));
};

class TestDomDistillerService : public DomDistillerServiceInterface {
 public:
  TestDomDistillerService() = default;
  ~TestDomDistillerService() override = default;

  MOCK_METHOD0(ViewUrlImpl, ViewerHandle*());
  std::unique_ptr<ViewerHandle> ViewUrl(
      ViewRequestDelegate*,
      std::unique_ptr<DistillerPage> distiller_page,
      const GURL&) override {
    return std::unique_ptr<ViewerHandle>(ViewUrlImpl());
  }
  std::unique_ptr<DistillerPage> CreateDefaultDistillerPage(
      const gfx::Size& render_view_size) override {
    return nullptr;
  }
  std::unique_ptr<DistillerPage> CreateDefaultDistillerPageWithHandle(
      std::unique_ptr<SourcePageHandle> handle) override {
    return nullptr;
  }
  DistilledPagePrefs* GetDistilledPagePrefs() override;
  DistillerUIHandle* GetDistillerUIHandle() override;
};

class DomDistillerViewerTest : public testing::Test {
 public:
  void SetUp() override {
    service_ = std::make_unique<TestDomDistillerService>();
  }

 protected:
  std::unique_ptr<ViewerHandle> CreateViewRequest(
      const GURL& url,
      ViewRequestDelegate* view_request_delegate) {
    return viewer::CreateViewRequest(service_.get(), url, view_request_delegate,
                                     gfx::Size());
  }

  std::unique_ptr<TestDomDistillerService> service_;
};

TEST_F(DomDistillerViewerTest, TestCreatingViewUrlRequest) {
  std::unique_ptr<FakeViewRequestDelegate> view_request_delegate(
      new FakeViewRequestDelegate());
  ViewerHandle* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback()));
  EXPECT_CALL(*service_, ViewUrlImpl())
      .WillOnce(testing::Return(viewer_handle));
  CreateViewRequest(GetDistillerViewUrlFromUrl("http://www.example.com/"),
                    view_request_delegate.get());
}

TEST_F(DomDistillerViewerTest, TestCreatingInvalidViewRequest) {
  std::unique_ptr<FakeViewRequestDelegate> view_request_delegate(
      new FakeViewRequestDelegate());
  EXPECT_CALL(*service_, ViewUrlImpl()).Times(0);
  // Specify none of the required query parameters.
  CreateViewRequest(GURL(std::string(kDomDistillerScheme) + "://host?foo=bar"),
                    view_request_delegate.get());
  // Specify both of the required query parameters.
  CreateViewRequest(net::AppendOrReplaceQueryParameter(
                        GetDistillerViewUrlFromUrl("http://www.example.com/"),
                        kEntryIdKey, "abc-def"),
                    view_request_delegate.get());
  // Specify an internal Chrome page.
  CreateViewRequest(GetDistillerViewUrlFromUrl("chrome://settings/"),
                    view_request_delegate.get());
  // Specify a recursive URL.
  CreateViewRequest(GetDistillerViewUrlFromUrl(
                        GetDistillerViewUrlFromEntryId("abc-def").spec()),
                    view_request_delegate.get());
  // Specify a non-distilled URL.
  CreateViewRequest(GURL("https://example.com"), view_request_delegate.get());
  // Specify an empty URL.
  CreateViewRequest(GURL(), view_request_delegate.get());
}

DistilledPagePrefs* TestDomDistillerService::GetDistilledPagePrefs() {
  return nullptr;
}

DistillerUIHandle* TestDomDistillerService::GetDistillerUIHandle() {
  return nullptr;
}

TEST_F(DomDistillerViewerTest, TestGetDistilledPageThemeJsOutput) {
  std::string kDarkJs = "useTheme('dark');";
  std::string kSepiaJs = "useTheme('sepia');";
  std::string kLightJs = "useTheme('light');";
  EXPECT_EQ(
      kDarkJs.compare(viewer::GetDistilledPageThemeJs(mojom::Theme::kDark)), 0);
  EXPECT_EQ(
      kLightJs.compare(viewer::GetDistilledPageThemeJs(mojom::Theme::kLight)),
      0);
  EXPECT_EQ(
      kSepiaJs.compare(viewer::GetDistilledPageThemeJs(mojom::Theme::kSepia)),
      0);
}

TEST_F(DomDistillerViewerTest, TestGetDistilledPageFontFamilyJsOutput) {
  std::string kSerifJsFontFamily = "useFontFamily('serif');";
  std::string kMonospaceJsFontFamily = "useFontFamily('monospace');";
  std::string kSansSerifJsFontFamily = "useFontFamily('sans-serif');";
  EXPECT_EQ(kSerifJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
                mojom::FontFamily::kSerif)),
            0);
  EXPECT_EQ(kMonospaceJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
                mojom::FontFamily::kMonospace)),
            0);
  EXPECT_EQ(kSansSerifJsFontFamily.compare(viewer::GetDistilledPageFontFamilyJs(
                mojom::FontFamily::kSansSerif)),
            0);
}

TEST_F(DomDistillerViewerTest, TestGetDistilledPageFontScalingJsOutput) {
  std::string kJsFontScaling = "useFontScaling(5);";
  EXPECT_EQ(kJsFontScaling.compare(viewer::GetDistilledPageFontScalingJs(5)),
            0);
}

}  // namespace dom_distiller