summaryrefslogtreecommitdiffstats
path: root/content/renderer/pepper/plugin_power_saver_helper_browsertest.cc
blob: d701c31b251e6cd438bde2768ed25fece6b9f08e (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 "base/macros.h"
#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/frame_load_waiter.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 "content/test/test_render_frame.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 "ui/gfx/geometry/size.h"
#include "url/gurl.h"

namespace content {

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

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

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

 protected:
  IPC::TestSink* sink_;

  DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest);
};

TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) {
  EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL,
            frame()->GetPeripheralContentStatus(
                url::Origin(GURL("http://same.com")),
                url::Origin(GURL("http://other.com")), gfx::Size(100, 100)));

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

  frame()->WhitelistContentOrigin(url::Origin(GURL("http://other.com")));

  EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED,
            frame()->GetPeripheralContentStatus(
                url::Origin(GURL("http://same.com")),
                url::Origin(GURL("http://other.com")), gfx::Size(100, 100)));

  // 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_TRUE(url::Origin(GURL("http://other.com"))
                  .IsSameOriginWith(base::get<0>(params)));
}

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

  std::set<url::Origin> origin_whitelist;
  origin_whitelist.insert(url::Origin(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) {
  frame()->WhitelistContentOrigin(url::Origin(GURL("http://other.com")));

  EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED,
            frame()->GetPeripheralContentStatus(
                url::Origin(GURL("http://same.com")),
                url::Origin(GURL("http://other.com")), gfx::Size(100, 100)));

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

  EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL,
            frame()->GetPeripheralContentStatus(
                url::Origin(GURL("http://same.com")),
                url::Origin(GURL("http://other.com")), gfx::Size(100, 100)));
}

}  // namespace content