Windows precompiled header support in GN.
[chromium-blink-merge.git] / ppapi / tests / test_view.cc
blob8239b037dec79344fb80f4bfeb13b0ae8c27a0f4
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/tests/test_view.h"
7 #include <sstream>
9 #include "ppapi/c/pp_time.h"
10 #include "ppapi/c/private/ppb_testing_private.h"
11 #include "ppapi/cpp/completion_callback.h"
12 #include "ppapi/tests/testing_instance.h"
14 REGISTER_TEST_CASE(View);
16 TestView::TestView(TestingInstance* instance)
17 : TestCase(instance),
18 post_quit_on_view_changed_(false) {
21 void TestView::DidChangeView(const pp::View& view) {
22 last_view_ = view;
23 page_visibility_log_.push_back(view.IsPageVisible());
25 if (post_quit_on_view_changed_) {
26 post_quit_on_view_changed_ = false;
27 testing_interface_->QuitMessageLoop(instance_->pp_instance());
31 bool TestView::Init() {
32 return CheckTestingInterface();
35 void TestView::RunTests(const std::string& filter) {
36 RUN_TEST(CreatedVisible, filter);
37 RUN_TEST(CreatedInvisible, filter);
38 RUN_TEST(PageHideShow, filter);
39 RUN_TEST(SizeChange, filter);
40 RUN_TEST(ClipChange, filter);
41 RUN_TEST(ScrollOffsetChange, filter);
44 bool TestView::WaitUntilViewChanged() {
45 size_t old_page_visibility_change_count = page_visibility_log_.size();
47 // Run a nested message loop. It will exit either on ViewChanged or if the
48 // timeout happens.
49 post_quit_on_view_changed_ = true;
50 testing_interface_->RunMessageLoop(instance_->pp_instance());
51 post_quit_on_view_changed_ = false;
53 // We know we got a view changed event if something was appended to the log.
54 return page_visibility_log_.size() > old_page_visibility_change_count;
57 void TestView::QuitMessageLoop(int32_t result) {
58 testing_interface_->QuitMessageLoop(instance_->pp_instance());
61 std::string TestView::TestCreatedVisible() {
62 ASSERT_FALSE(page_visibility_log_.empty());
63 ASSERT_TRUE(page_visibility_log_[0]);
64 PASS();
67 std::string TestView::TestCreatedInvisible() {
68 ASSERT_FALSE(page_visibility_log_.empty());
70 if (page_visibility_log_[0]) {
71 // Add more error message since this test has some extra requirements.
72 instance_->AppendError("Initial page is set to visible. NOTE: "
73 "This test must be run in a background tab. "
74 "Either run in the UI test which does this, or you can middle-click "
75 "on the test link to run manually.");
77 ASSERT_FALSE(page_visibility_log_[0]);
78 PASS();
81 std::string TestView::TestPageHideShow() {
82 // Initial state should be visible.
83 ASSERT_FALSE(page_visibility_log_.empty());
84 ASSERT_TRUE(page_visibility_log_[0]);
86 // Now that we're alive, tell the test knows it can change our visibility.
87 instance_->ReportProgress("TestPageHideShow:Created");
89 // Wait until we get a hide event, being careful to handle spurious
90 // notifications of ViewChanged.
91 while (WaitUntilViewChanged() &&
92 page_visibility_log_[page_visibility_log_.size() - 1]) {
94 if (page_visibility_log_[page_visibility_log_.size() - 1]) {
95 // Didn't get a view changed event that changed visibility (though there
96 // may have been some that didn't change visibility).
97 // Add more error message since this test has some extra requirements.
98 return "Didn't receive a hide event in timeout. NOTE: "
99 "This test requires tab visibility to change and won't pass if you "
100 "just run it in a browser. Normally the UI test should handle "
101 "this. You can also run manually by waiting 2 secs, creating a new "
102 "tab, waiting 2 more secs, and closing the new tab.";
105 // Tell the test so it can show us again.
106 instance_->ReportProgress("TestPageHideShow:Hidden");
108 // Wait until we get a show event.
109 while (WaitUntilViewChanged() &&
110 !page_visibility_log_[page_visibility_log_.size() - 1]) {
112 ASSERT_TRUE(page_visibility_log_[page_visibility_log_.size() - 1]);
114 PASS();
117 std::string TestView::TestSizeChange() {
118 pp::Rect original_rect = last_view_.GetRect();
120 pp::Rect desired_rect = original_rect;
121 desired_rect.set_width(original_rect.width() + 10);
122 desired_rect.set_height(original_rect.height() + 12);
124 std::ostringstream script_stream;
125 script_stream << "var plugin = document.getElementById('plugin');";
126 script_stream << "plugin.setAttribute('width', "
127 << desired_rect.width() << ");";
128 script_stream << "plugin.setAttribute('height', "
129 << desired_rect.height() << ");";
131 instance_->EvalScript(script_stream.str());
133 while (WaitUntilViewChanged() && last_view_.GetRect() != desired_rect) {
135 ASSERT_TRUE(last_view_.GetRect() == desired_rect);
137 PASS();
140 std::string TestView::TestClipChange() {
141 pp::Rect original_rect = last_view_.GetRect();
143 // Original clip should be the full frame.
144 pp::Rect original_clip = last_view_.GetClipRect();
145 ASSERT_TRUE(original_clip.x() == 0);
146 ASSERT_TRUE(original_clip.y() == 0);
147 ASSERT_TRUE(original_clip.width() == original_rect.width());
148 ASSERT_TRUE(original_clip.height() == original_rect.height());
150 int clip_amount = original_rect.height() / 2;
152 // It might be nice to set the position to be absolute and set the location,
153 // but this will cause WebKit to actually tear down the plugin and recreate
154 // it. So instead we add a big div to cause the document to be scrollable,
155 // and scroll it down.
156 std::ostringstream script_stream;
157 script_stream
158 << "var big = document.createElement('div');"
159 << "big.setAttribute('style', 'position:absolute; left:100px; "
160 "top:0px; width:1px; height:5000px;');"
161 << "document.body.appendChild(big);"
162 << "window.scrollBy(0, " << original_rect.y() + clip_amount << ");";
164 instance_->EvalScript(script_stream.str());
166 pp::Rect desired_clip = original_clip;
167 desired_clip.set_y(clip_amount);
168 desired_clip.set_height(desired_clip.height() - desired_clip.y());
170 while (WaitUntilViewChanged() && last_view_.GetClipRect() != desired_clip) {
172 ASSERT_TRUE(last_view_.GetClipRect() == desired_clip);
173 PASS();
176 std::string TestView::TestScrollOffsetChange() {
177 instance_->EvalScript("document.body.style.width = '5000px';"
178 "document.body.style.height = '5000px';");
179 instance_->EvalScript("window.scrollTo(5, 1);");
181 while (WaitUntilViewChanged() &&
182 last_view_.GetScrollOffset() != pp::Point(5, 1)) {
184 ASSERT_EQ(pp::Point(5, 1), last_view_.GetScrollOffset());
186 instance_->EvalScript("window.scrollTo(0, 0);");
188 while (WaitUntilViewChanged() &&
189 last_view_.GetScrollOffset() != pp::Point(0, 0)) {
191 ASSERT_EQ(pp::Point(0, 0), last_view_.GetScrollOffset());
193 PASS();