summaryrefslogtreecommitdiffstats
path: root/content/renderer/pepper/plugin_power_saver_helper_browsertest.cc
blob: cb74cd1590e3286c90050d1cc02655b9681db8e7 (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
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
// 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 "base/run_loop.h"
#include "content/common/frame_messages.h"
#include "content/common/view_message_enums.h"
#include "content/public/common/content_constants.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/pepper/plugin_power_saver_helper.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_view_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebPluginParams.h"
#include "url/gurl.h"

namespace content {

class PluginPowerSaverHelperTest : public RenderViewTest {
 public:
  PluginPowerSaverHelperTest() : sink_(NULL) {}

  RenderFrameImpl* frame() {
    return static_cast<RenderFrameImpl*>(view_->GetMainRenderFrame());
  }

  PluginPowerSaverHelper* helper() {
    return frame()->plugin_power_saver_helper();
  }

  void SetUp() override {
    RenderViewTest::SetUp();
    sink_ = &render_thread_->sink();
  }

  blink::WebPluginParams MakeParams(const std::string& url,
                                    const std::string& poster,
                                    const std::string& width,
                                    const std::string& height) {
    const size_t size = 3;
    blink::WebVector<blink::WebString> names(size);
    blink::WebVector<blink::WebString> values(size);

    blink::WebPluginParams params;
    params.url = GURL(url);

    params.attributeNames.swap(names);
    params.attributeValues.swap(values);

    params.attributeNames[0] = "poster";
    params.attributeNames[1] = "height";
    params.attributeNames[2] = "width";
    params.attributeValues[0] = blink::WebString::fromUTF8(poster);
    params.attributeValues[1] = blink::WebString::fromUTF8(height);
    params.attributeValues[2] = blink::WebString::fromUTF8(width);

    return params;
  }

 protected:
  IPC::TestSink* sink_;

  DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest);
};

TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) {
  EXPECT_FALSE(helper()->ShouldThrottleContent(GURL(), kFlashPluginName, 100,
                                               100, nullptr));
  EXPECT_FALSE(helper()->ShouldThrottleContent(GURL(), kFlashPluginName, 1000,
                                               1000, nullptr));
}

TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) {
  bool cross_origin_main_content = false;
  EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                              kFlashPluginName, 100, 100,
                                              &cross_origin_main_content));
  EXPECT_FALSE(cross_origin_main_content);

  EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                               kFlashPluginName, 1000, 1000,
                                               &cross_origin_main_content));
  EXPECT_TRUE(cross_origin_main_content);
}

TEST_F(PluginPowerSaverHelperTest, AlwaysAllowTinyContent) {
  bool cross_origin_main_content = false;
  EXPECT_FALSE(
      helper()->ShouldThrottleContent(GURL(), kFlashPluginName, 1, 1, nullptr));
  EXPECT_FALSE(cross_origin_main_content);

  EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                               kFlashPluginName, 1, 1,
                                               &cross_origin_main_content));
  EXPECT_FALSE(cross_origin_main_content);

  EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                               kFlashPluginName, 5, 5,
                                               &cross_origin_main_content));
  EXPECT_FALSE(cross_origin_main_content);

  EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                              kFlashPluginName, 10, 10,
                                              &cross_origin_main_content));
  EXPECT_FALSE(cross_origin_main_content);
}

TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) {
  bool cross_origin_main_content = false;
  EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                              kFlashPluginName, 100, 100,
                                              &cross_origin_main_content));
  EXPECT_FALSE(cross_origin_main_content);

  // Clear out other messages so we find just the plugin power saver IPCs.
  sink_->ClearMessages();

  helper()->WhitelistContentOrigin(GURL("http://b.com"));
  EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
                                               kFlashPluginName, 100, 100,
                                               &cross_origin_main_content));
  EXPECT_FALSE(cross_origin_main_content);

  // Test that we've sent an IPC to the browser.
  ASSERT_EQ(1u, sink_->message_count());
  const IPC::Message* msg = sink_->GetMessageAt(0);
  EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type());
  FrameHostMsg_PluginContentOriginAllowed::Param params;
  FrameHostMsg_PluginContentOriginAllowed::Read(msg, &params);
  EXPECT_EQ(GURL("http://b.com"), base::get<0>(params));
}

TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) {
  base::RunLoop loop;
  frame()->RegisterPeripheralPlugin(GURL("http://other.com"),
                                    loop.QuitClosure());

  std::set<GURL> origin_whitelist;
  origin_whitelist.insert(GURL("http://other.com"));
  frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist(
      frame()->GetRoutingID(), origin_whitelist));

  // Runs until the unthrottle closure is run.
  loop.Run();
}

TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) {
  helper()->WhitelistContentOrigin(GURL("http://b.com"));

  EXPECT_FALSE(helper()->ShouldThrottleContent(
      GURL("http://b.com"), kFlashPluginName, 100, 100, nullptr));

  LoadHTML("<html></html>");

  EXPECT_TRUE(helper()->ShouldThrottleContent(
      GURL("http://b.com"), kFlashPluginName, 100, 100, nullptr));
}

}  // namespace content