summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/peripheral_content_heuristic_unittest.cc
blob: 56d006c815eee30f697a0492844f5e2018336cb8 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/peripheral_content_heuristic.h"

#include "base/test/scoped_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace content {

namespace {

const char kSameOrigin[] = "http://same.com";
const char kOtherOrigin[] = "http://other.com";

}  // namespaces

TEST(PeripheralContentHeuristic, AllowSameOrigin) {
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_SAME_ORIGIN,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kSameOrigin)), gfx::Size(100, 100)));
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_SAME_ORIGIN,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kSameOrigin)), gfx::Size(1000, 1000)));
}

TEST(PeripheralContentHeuristic, DisallowCrossOriginUnlessLarge) {
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(100, 100)));
  EXPECT_EQ(
      RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG,
      PeripheralContentHeuristic::GetPeripheralStatus(
          std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
          url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(1000, 1000)));
}

TEST(PeripheralContentHeuristic, TinyContent) {
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_TINY,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kSameOrigin)), gfx::Size(1, 1)));
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_TINY,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(1, 1)));
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_TINY,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(5, 5)));
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(10, 10)));
}

TEST(PeripheralContentHeuristic, TemporaryOriginWhitelist) {
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(100, 100)));

  std::set<url::Origin> whitelist;
  whitelist.insert(url::Origin::Create(GURL(kOtherOrigin)));

  EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED,
            PeripheralContentHeuristic::GetPeripheralStatus(
                whitelist, url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size(100, 100)));
}

TEST(PeripheralContentHeuristic, UndefinedSize) {
  // Undefined size plugins from any origin (including same-origin and
  // whitelisted origins) are marked tiny until proven otherwise.
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_TINY,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kSameOrigin)), gfx::Size()));

  std::set<url::Origin> whitelist;
  whitelist.insert(url::Origin::Create(GURL(kOtherOrigin)));
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_TINY,
            PeripheralContentHeuristic::GetPeripheralStatus(
                whitelist, url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size()));

  EXPECT_EQ(RenderFrame::CONTENT_STATUS_TINY,
            PeripheralContentHeuristic::GetPeripheralStatus(
                std::set<url::Origin>(), url::Origin::Create(GURL(kSameOrigin)),
                url::Origin::Create(GURL(kOtherOrigin)), gfx::Size()));
}

}  // namespace content