summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_plugin/test_browser_plugin_guest.cc
blob: 35f119e56229dcf17b6f2f963e351d612fde96e9 (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
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2012 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/browser/browser_plugin/test_browser_plugin_guest.h"

#include "base/test/test_timeouts.h"
#include "content/browser/browser_plugin/browser_plugin_guest.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/browser_plugin_messages.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/test_utils.h"
#include "ui/gfx/size.h"

namespace content {

class BrowserPluginGuest;

TestBrowserPluginGuest::TestBrowserPluginGuest(
    int instance_id,
    WebContentsImpl* web_contents,
    RenderViewHost* render_view_host)
    : BrowserPluginGuest(instance_id, web_contents, render_view_host),
      update_rect_count_(0),
      crash_observed_(false),
      focus_observed_(false),
      advance_focus_observed_(false),
      was_hidden_observed_(false),
      waiting_for_update_rect_msg_with_size_(false),
      last_update_rect_width_(-1),
      last_update_rect_height_(-1) {
  // Listen to visibility changes so that a test can wait for these changes.
  registrar_.Add(this,
                 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED,
                 Source<WebContents>(web_contents));
}

TestBrowserPluginGuest::~TestBrowserPluginGuest() {
}

void TestBrowserPluginGuest::Observe(int type,
                                     const NotificationSource& source,
                                     const NotificationDetails& details) {
  switch (type) {
    case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: {
      bool visible = *Details<bool>(details).ptr();
      if (!visible) {
        was_hidden_observed_ = true;
        if (was_hidden_message_loop_runner_)
          was_hidden_message_loop_runner_->Quit();
      }
      break;
    }
    default:
      NOTREACHED() << "Unexpected notification type: " << type;
  }
}

void TestBrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) {
  if (msg->type() == BrowserPluginMsg_UpdateRect::ID) {
    PickleIterator iter(*msg);

    int instance_id;
    int message_id;
    BrowserPluginMsg_UpdateRect_Params update_rect_params;

    if (!IPC::ReadParam(msg, &iter, &instance_id) ||
        !IPC::ReadParam(msg, &iter, &message_id) ||
        !IPC::ReadParam(msg, &iter, &update_rect_params)) {
      NOTREACHED() <<
          "Cannot read BrowserPluginMsg_UpdateRect params from ipc message";
    }
    last_update_rect_width_ = update_rect_params.view_size.width();
    last_update_rect_height_ = update_rect_params.view_size.height();
    update_rect_count_++;
    if (waiting_for_update_rect_msg_with_size_ &&
        expected_width_ == last_update_rect_width_ &&
        expected_height_ == last_update_rect_height_) {
      waiting_for_update_rect_msg_with_size_ = false;
      if (send_message_loop_runner_)
        send_message_loop_runner_->Quit();
    } else if (!waiting_for_update_rect_msg_with_size_) {
      if (send_message_loop_runner_)
        send_message_loop_runner_->Quit();
    }
  }
  BrowserPluginGuest::SendMessageToEmbedder(msg);
}

void TestBrowserPluginGuest::WaitForUpdateRectMsg() {
  // Check if we already got any UpdateRect message.
  if (update_rect_count_ > 0)
    return;
  send_message_loop_runner_ = new MessageLoopRunner();
  send_message_loop_runner_->Run();
}

void TestBrowserPluginGuest::WaitForUpdateRectMsgWithSize(int width,
                                                          int height) {
  if (update_rect_count_ > 0 &&
      last_update_rect_width_ == width &&
      last_update_rect_height_ == height) {
    // We already saw this message.
    return;
  }
  waiting_for_update_rect_msg_with_size_ = true;
  expected_width_ = width;
  expected_height_ = height;

  send_message_loop_runner_ = new MessageLoopRunner();
  send_message_loop_runner_->Run();
}

void TestBrowserPluginGuest::RenderViewGone(base::TerminationStatus status) {
  crash_observed_ = true;
  LOG(INFO) << "Guest crashed";
  if (crash_message_loop_runner_)
    crash_message_loop_runner_->Quit();
  BrowserPluginGuest::RenderViewGone(status);
}

void TestBrowserPluginGuest::WaitForCrashed() {
  // Check if we already observed a guest crash, return immediately if so.
  if (crash_observed_)
    return;

  crash_message_loop_runner_ = new MessageLoopRunner();
  crash_message_loop_runner_->Run();
}

void TestBrowserPluginGuest::WaitForFocus() {
  if (focus_observed_)
    return;
  focus_message_loop_runner_ = new MessageLoopRunner();
  focus_message_loop_runner_->Run();
}

void TestBrowserPluginGuest::WaitForAdvanceFocus() {
  if (advance_focus_observed_)
    return;
  advance_focus_message_loop_runner_ = new MessageLoopRunner();
  advance_focus_message_loop_runner_->Run();
}

void TestBrowserPluginGuest::WaitUntilHidden() {
  if (was_hidden_observed_) {
    was_hidden_observed_ = false;
    return;
  }
  was_hidden_message_loop_runner_ = new MessageLoopRunner();
  was_hidden_message_loop_runner_->Run();
  was_hidden_observed_ = false;
}

void TestBrowserPluginGuest::SetFocus(bool focused) {
  focus_observed_ = true;
  if (focus_message_loop_runner_)
    focus_message_loop_runner_->Quit();
  BrowserPluginGuest::SetFocus(focused);
}

bool TestBrowserPluginGuest::ViewTakeFocus(bool reverse) {
  advance_focus_observed_ = true;
  if (advance_focus_message_loop_runner_)
    advance_focus_message_loop_runner_->Quit();
  return BrowserPluginGuest::ViewTakeFocus(reverse);
}

}  // namespace content