| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_view.h" | 5 #include "ppapi/tests/test_view.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "ppapi/c/pp_time.h" | 9 #include "ppapi/c/pp_time.h" |
| 10 #include "ppapi/c/private/ppb_testing_private.h" | 10 #include "ppapi/c/private/ppb_testing_private.h" |
| 11 #include "ppapi/cpp/completion_callback.h" | 11 #include "ppapi/cpp/completion_callback.h" |
| 12 #include "ppapi/tests/testing_instance.h" | 12 #include "ppapi/tests/testing_instance.h" |
| 13 | 13 |
| 14 REGISTER_TEST_CASE(View); | 14 REGISTER_TEST_CASE(View); |
| 15 | 15 |
| 16 // When waiting for view changed events, wait no longer than this. | |
| 17 #if !defined(THREAD_SANITIZER) && !defined(MEMORY_SANITIZER) | |
| 18 static int kViewChangeTimeoutSec = 5; | |
| 19 #else | |
| 20 // ThreadSanitizer may slow the interaction down significantly. | |
| 21 static int kViewChangeTimeoutSec = 30; | |
| 22 #endif | |
| 23 | |
| 24 TestView::TestView(TestingInstance* instance) | 16 TestView::TestView(TestingInstance* instance) |
| 25 : TestCase(instance), | 17 : TestCase(instance), |
| 26 post_quit_on_view_changed_(false) { | 18 post_quit_on_view_changed_(false) { |
| 27 } | 19 } |
| 28 | 20 |
| 29 void TestView::DidChangeView(const pp::View& view) { | 21 void TestView::DidChangeView(const pp::View& view) { |
| 30 last_view_ = view; | 22 last_view_ = view; |
| 31 page_visibility_log_.push_back(view.IsPageVisible()); | 23 page_visibility_log_.push_back(view.IsPageVisible()); |
| 32 | 24 |
| 33 if (post_quit_on_view_changed_) { | 25 if (post_quit_on_view_changed_) { |
| 34 post_quit_on_view_changed_ = false; | 26 post_quit_on_view_changed_ = false; |
| 35 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 27 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 36 } | 28 } |
| 37 } | 29 } |
| 38 | 30 |
| 39 bool TestView::Init() { | 31 bool TestView::Init() { |
| 40 return CheckTestingInterface(); | 32 return CheckTestingInterface(); |
| 41 } | 33 } |
| 42 | 34 |
| 43 void TestView::RunTests(const std::string& filter) { | 35 void TestView::RunTests(const std::string& filter) { |
| 44 RUN_TEST(CreatedVisible, filter); | 36 RUN_TEST(CreatedVisible, filter); |
| 45 RUN_TEST(CreatedInvisible, filter); | 37 RUN_TEST(CreatedInvisible, filter); |
| 46 RUN_TEST(PageHideShow, filter); | 38 RUN_TEST(PageHideShow, filter); |
| 47 RUN_TEST(SizeChange, filter); | 39 RUN_TEST(SizeChange, filter); |
| 48 RUN_TEST(ClipChange, filter); | 40 RUN_TEST(ClipChange, filter); |
| 49 RUN_TEST(ScrollOffsetChange, filter); | 41 RUN_TEST(ScrollOffsetChange, filter); |
| 50 } | 42 } |
| 51 | 43 |
| 52 bool TestView::WaitUntilViewChanged() { | 44 bool TestView::WaitUntilViewChanged() { |
| 53 // Schedule a callback so this step times out if we don't get a ViewChanged | |
| 54 // in a reasonable amount of time. | |
| 55 pp::CompletionCallbackFactory<TestView> factory(this); | |
| 56 pp::CompletionCallback timeout = | |
| 57 factory.NewCallback(&TestView::QuitMessageLoop); | |
| 58 pp::Module::Get()->core()->CallOnMainThread( | |
| 59 kViewChangeTimeoutSec * 1000, timeout); | |
| 60 | |
| 61 size_t old_page_visibility_change_count = page_visibility_log_.size(); | 45 size_t old_page_visibility_change_count = page_visibility_log_.size(); |
| 62 | 46 |
| 63 // Run a nested message loop. It will exit either on ViewChanged or if the | 47 // Run a nested message loop. It will exit either on ViewChanged or if the |
| 64 // timeout happens. | 48 // timeout happens. |
| 65 post_quit_on_view_changed_ = true; | 49 post_quit_on_view_changed_ = true; |
| 66 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 50 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 67 post_quit_on_view_changed_ = false; | 51 post_quit_on_view_changed_ = false; |
| 68 | 52 |
| 69 // We know we got a view changed event if something was appended to the log. | 53 // We know we got a view changed event if something was appended to the log. |
| 70 return page_visibility_log_.size() > old_page_visibility_change_count; | 54 return page_visibility_log_.size() > old_page_visibility_change_count; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 97 std::string TestView::TestPageHideShow() { | 81 std::string TestView::TestPageHideShow() { |
| 98 // Initial state should be visible. | 82 // Initial state should be visible. |
| 99 ASSERT_FALSE(page_visibility_log_.empty()); | 83 ASSERT_FALSE(page_visibility_log_.empty()); |
| 100 ASSERT_TRUE(page_visibility_log_[0]); | 84 ASSERT_TRUE(page_visibility_log_[0]); |
| 101 | 85 |
| 102 // Now that we're alive, tell the test knows it can change our visibility. | 86 // Now that we're alive, tell the test knows it can change our visibility. |
| 103 instance_->ReportProgress("TestPageHideShow:Created"); | 87 instance_->ReportProgress("TestPageHideShow:Created"); |
| 104 | 88 |
| 105 // Wait until we get a hide event, being careful to handle spurious | 89 // Wait until we get a hide event, being careful to handle spurious |
| 106 // notifications of ViewChanged. | 90 // notifications of ViewChanged. |
| 107 PP_Time begin_time = pp::Module::Get()->core()->GetTime(); | |
| 108 while (WaitUntilViewChanged() && | 91 while (WaitUntilViewChanged() && |
| 109 page_visibility_log_[page_visibility_log_.size() - 1] && | 92 page_visibility_log_[page_visibility_log_.size() - 1]) { |
| 110 pp::Module::Get()->core()->GetTime() - begin_time < | |
| 111 kViewChangeTimeoutSec) { | |
| 112 } | 93 } |
| 113 if (page_visibility_log_[page_visibility_log_.size() - 1]) { | 94 if (page_visibility_log_[page_visibility_log_.size() - 1]) { |
| 114 // Didn't get a view changed event that changed visibility (though there | 95 // Didn't get a view changed event that changed visibility (though there |
| 115 // may have been some that didn't change visibility). | 96 // may have been some that didn't change visibility). |
| 116 // Add more error message since this test has some extra requirements. | 97 // Add more error message since this test has some extra requirements. |
| 117 return "Didn't receive a hide event in timeout. NOTE: " | 98 return "Didn't receive a hide event in timeout. NOTE: " |
| 118 "This test requires tab visibility to change and won't pass if you " | 99 "This test requires tab visibility to change and won't pass if you " |
| 119 "just run it in a browser. Normally the UI test should handle " | 100 "just run it in a browser. Normally the UI test should handle " |
| 120 "this. You can also run manually by waiting 2 secs, creating a new " | 101 "this. You can also run manually by waiting 2 secs, creating a new " |
| 121 "tab, waiting 2 more secs, and closing the new tab."; | 102 "tab, waiting 2 more secs, and closing the new tab."; |
| 122 } | 103 } |
| 123 | 104 |
| 124 // Tell the test so it can show us again. | 105 // Tell the test so it can show us again. |
| 125 instance_->ReportProgress("TestPageHideShow:Hidden"); | 106 instance_->ReportProgress("TestPageHideShow:Hidden"); |
| 126 | 107 |
| 127 // Wait until we get a show event. | 108 // Wait until we get a show event. |
| 128 begin_time = pp::Module::Get()->core()->GetTime(); | |
| 129 while (WaitUntilViewChanged() && | 109 while (WaitUntilViewChanged() && |
| 130 !page_visibility_log_[page_visibility_log_.size() - 1] && | 110 !page_visibility_log_[page_visibility_log_.size() - 1]) { |
| 131 pp::Module::Get()->core()->GetTime() - begin_time < | |
| 132 kViewChangeTimeoutSec) { | |
| 133 } | 111 } |
| 134 ASSERT_TRUE(page_visibility_log_[page_visibility_log_.size() - 1]); | 112 ASSERT_TRUE(page_visibility_log_[page_visibility_log_.size() - 1]); |
| 135 | 113 |
| 136 PASS(); | 114 PASS(); |
| 137 } | 115 } |
| 138 | 116 |
| 139 std::string TestView::TestSizeChange() { | 117 std::string TestView::TestSizeChange() { |
| 140 pp::Rect original_rect = last_view_.GetRect(); | 118 pp::Rect original_rect = last_view_.GetRect(); |
| 141 | 119 |
| 142 pp::Rect desired_rect = original_rect; | 120 pp::Rect desired_rect = original_rect; |
| 143 desired_rect.set_width(original_rect.width() + 10); | 121 desired_rect.set_width(original_rect.width() + 10); |
| 144 desired_rect.set_height(original_rect.height() + 12); | 122 desired_rect.set_height(original_rect.height() + 12); |
| 145 | 123 |
| 146 std::ostringstream script_stream; | 124 std::ostringstream script_stream; |
| 147 script_stream << "var plugin = document.getElementById('plugin');"; | 125 script_stream << "var plugin = document.getElementById('plugin');"; |
| 148 script_stream << "plugin.setAttribute('width', " | 126 script_stream << "plugin.setAttribute('width', " |
| 149 << desired_rect.width() << ");"; | 127 << desired_rect.width() << ");"; |
| 150 script_stream << "plugin.setAttribute('height', " | 128 script_stream << "plugin.setAttribute('height', " |
| 151 << desired_rect.height() << ");"; | 129 << desired_rect.height() << ");"; |
| 152 | 130 |
| 153 instance_->EvalScript(script_stream.str()); | 131 instance_->EvalScript(script_stream.str()); |
| 154 | 132 |
| 155 PP_Time begin_time = pp::Module::Get()->core()->GetTime(); | 133 while (WaitUntilViewChanged() && last_view_.GetRect() != desired_rect) { |
| 156 while (WaitUntilViewChanged() && last_view_.GetRect() != desired_rect && | |
| 157 pp::Module::Get()->core()->GetTime() - begin_time < | |
| 158 kViewChangeTimeoutSec) { | |
| 159 } | 134 } |
| 160 ASSERT_TRUE(last_view_.GetRect() == desired_rect); | 135 ASSERT_TRUE(last_view_.GetRect() == desired_rect); |
| 161 | 136 |
| 162 PASS(); | 137 PASS(); |
| 163 } | 138 } |
| 164 | 139 |
| 165 std::string TestView::TestClipChange() { | 140 std::string TestView::TestClipChange() { |
| 166 pp::Rect original_rect = last_view_.GetRect(); | 141 pp::Rect original_rect = last_view_.GetRect(); |
| 167 | 142 |
| 168 // Original clip should be the full frame. | 143 // Original clip should be the full frame. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 185 "top:0px; width:1px; height:5000px;');" | 160 "top:0px; width:1px; height:5000px;');" |
| 186 << "document.body.appendChild(big);" | 161 << "document.body.appendChild(big);" |
| 187 << "window.scrollBy(0, " << original_rect.y() + clip_amount << ");"; | 162 << "window.scrollBy(0, " << original_rect.y() + clip_amount << ");"; |
| 188 | 163 |
| 189 instance_->EvalScript(script_stream.str()); | 164 instance_->EvalScript(script_stream.str()); |
| 190 | 165 |
| 191 pp::Rect desired_clip = original_clip; | 166 pp::Rect desired_clip = original_clip; |
| 192 desired_clip.set_y(clip_amount); | 167 desired_clip.set_y(clip_amount); |
| 193 desired_clip.set_height(desired_clip.height() - desired_clip.y()); | 168 desired_clip.set_height(desired_clip.height() - desired_clip.y()); |
| 194 | 169 |
| 195 PP_Time begin_time = pp::Module::Get()->core()->GetTime(); | 170 while (WaitUntilViewChanged() && last_view_.GetClipRect() != desired_clip) { |
| 196 while (WaitUntilViewChanged() && last_view_.GetClipRect() != desired_clip && | |
| 197 pp::Module::Get()->core()->GetTime() - begin_time < | |
| 198 kViewChangeTimeoutSec) { | |
| 199 } | 171 } |
| 200 ASSERT_TRUE(last_view_.GetClipRect() == desired_clip); | 172 ASSERT_TRUE(last_view_.GetClipRect() == desired_clip); |
| 201 PASS(); | 173 PASS(); |
| 202 } | 174 } |
| 203 | 175 |
| 204 std::string TestView::TestScrollOffsetChange() { | 176 std::string TestView::TestScrollOffsetChange() { |
| 205 instance_->EvalScript("document.body.style.width = '5000px';" | 177 instance_->EvalScript("document.body.style.width = '5000px';" |
| 206 "document.body.style.height = '5000px';"); | 178 "document.body.style.height = '5000px';"); |
| 207 instance_->EvalScript("window.scrollTo(5, 1);"); | 179 instance_->EvalScript("window.scrollTo(5, 1);"); |
| 208 | 180 |
| 209 PP_Time begin_time = pp::Module::Get()->core()->GetTime(); | |
| 210 while (WaitUntilViewChanged() && | 181 while (WaitUntilViewChanged() && |
| 211 last_view_.GetScrollOffset() != pp::Point(5, 1) && | 182 last_view_.GetScrollOffset() != pp::Point(5, 1)) { |
| 212 pp::Module::Get()->core()->GetTime() - begin_time < | |
| 213 kViewChangeTimeoutSec) { | |
| 214 } | 183 } |
| 215 ASSERT_EQ(pp::Point(5, 1), last_view_.GetScrollOffset()); | 184 ASSERT_EQ(pp::Point(5, 1), last_view_.GetScrollOffset()); |
| 216 | 185 |
| 217 instance_->EvalScript("window.scrollTo(0, 0);"); | 186 instance_->EvalScript("window.scrollTo(0, 0);"); |
| 218 | 187 |
| 219 begin_time = pp::Module::Get()->core()->GetTime(); | |
| 220 while (WaitUntilViewChanged() && | 188 while (WaitUntilViewChanged() && |
| 221 last_view_.GetScrollOffset() != pp::Point(0, 0) && | 189 last_view_.GetScrollOffset() != pp::Point(0, 0)) { |
| 222 pp::Module::Get()->core()->GetTime() - begin_time < | |
| 223 kViewChangeTimeoutSec) { | |
| 224 } | 190 } |
| 225 ASSERT_EQ(pp::Point(0, 0), last_view_.GetScrollOffset()); | 191 ASSERT_EQ(pp::Point(0, 0), last_view_.GetScrollOffset()); |
| 226 | 192 |
| 227 PASS(); | 193 PASS(); |
| 228 } | 194 } |
| OLD | NEW |