OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_flash_message_loop.h" | 5 #include "ppapi/tests/test_flash_message_loop.h" |
6 | 6 |
7 #include "ppapi/c/pp_macros.h" | 7 #include "ppapi/c/pp_macros.h" |
| 8 #include "ppapi/c/ppb_var.h" |
8 #include "ppapi/cpp/core.h" | 9 #include "ppapi/cpp/core.h" |
| 10 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" |
9 #include "ppapi/cpp/logging.h" | 11 #include "ppapi/cpp/logging.h" |
10 #include "ppapi/cpp/module.h" | 12 #include "ppapi/cpp/module.h" |
11 #include "ppapi/cpp/private/flash_message_loop.h" | 13 #include "ppapi/cpp/private/flash_message_loop.h" |
12 #include "ppapi/tests/testing_instance.h" | 14 #include "ppapi/tests/testing_instance.h" |
13 | 15 |
| 16 namespace { |
| 17 |
| 18 const char kDidRunScriptCallback[] = "DidRunScriptCallback"; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 class TestFlashMessageLoop::InstanceSO |
| 23 : public pp::deprecated::ScriptableObject { |
| 24 public: |
| 25 explicit InstanceSO(TestFlashMessageLoop* owner) : owner_(owner) {} |
| 26 |
| 27 ~InstanceSO() override { |
| 28 if (owner_) |
| 29 owner_->clear_instance_so(); |
| 30 } |
| 31 |
| 32 // pp::deprecated::ScriptableObject overrides. |
| 33 bool HasMethod(const pp::Var& name, pp::Var* exception) override { |
| 34 if (!name.is_string()) |
| 35 return false; |
| 36 return name.AsString() == kDidRunScriptCallback; |
| 37 } |
| 38 |
| 39 pp::Var Call(const pp::Var& method_name, |
| 40 const std::vector<pp::Var>& args, |
| 41 pp::Var* exception) override { |
| 42 if (!method_name.is_string()) |
| 43 return false; |
| 44 std::string name = method_name.AsString(); |
| 45 |
| 46 if (name == kDidRunScriptCallback) { |
| 47 if (args.size() != 0) { |
| 48 *exception = pp::Var("Bad argument to DidRunScriptCallback()"); |
| 49 } else if (owner_) { |
| 50 owner_->DidRunScriptCallback(); |
| 51 } |
| 52 } else { |
| 53 *exception = pp::Var("Bad function call"); |
| 54 } |
| 55 |
| 56 return pp::Var(); |
| 57 } |
| 58 |
| 59 void clear_owner() { owner_ = nullptr; } |
| 60 |
| 61 private: |
| 62 TestFlashMessageLoop* owner_; |
| 63 }; |
| 64 |
14 REGISTER_TEST_CASE(FlashMessageLoop); | 65 REGISTER_TEST_CASE(FlashMessageLoop); |
15 | 66 |
16 TestFlashMessageLoop::TestFlashMessageLoop(TestingInstance* instance) | 67 TestFlashMessageLoop::TestFlashMessageLoop(TestingInstance* instance) |
17 : TestCase(instance), | 68 : TestCase(instance), |
18 message_loop_(NULL), | 69 message_loop_(nullptr), |
19 callback_factory_(this) { | 70 instance_so_(nullptr), |
20 } | 71 suspend_script_callback_result_(false), |
| 72 callback_factory_(this) {} |
21 | 73 |
22 TestFlashMessageLoop::~TestFlashMessageLoop() { | 74 TestFlashMessageLoop::~TestFlashMessageLoop() { |
23 PP_DCHECK(!message_loop_); | 75 PP_DCHECK(!message_loop_); |
| 76 |
| 77 ResetTestObject(); |
| 78 if (instance_so_) |
| 79 instance_so_->clear_owner(); |
24 } | 80 } |
25 | 81 |
26 void TestFlashMessageLoop::RunTests(const std::string& filter) { | 82 void TestFlashMessageLoop::RunTests(const std::string& filter) { |
27 RUN_TEST(Basics, filter); | 83 RUN_TEST(Basics, filter); |
28 RUN_TEST(RunWithoutQuit, filter); | 84 RUN_TEST(RunWithoutQuit, filter); |
| 85 RUN_TEST(SuspendScriptCallbackWhileRunning, filter); |
| 86 } |
| 87 |
| 88 void TestFlashMessageLoop::DidRunScriptCallback() { |
| 89 // Script callbacks are not supposed to run while the Flash message loop is |
| 90 // running. |
| 91 if (message_loop_) |
| 92 suspend_script_callback_result_ = false; |
| 93 } |
| 94 |
| 95 pp::deprecated::ScriptableObject* TestFlashMessageLoop::CreateTestObject() { |
| 96 if (!instance_so_) |
| 97 instance_so_ = new InstanceSO(this); |
| 98 return instance_so_; |
29 } | 99 } |
30 | 100 |
31 std::string TestFlashMessageLoop::TestBasics() { | 101 std::string TestFlashMessageLoop::TestBasics() { |
32 message_loop_ = new pp::flash::MessageLoop(instance_); | 102 message_loop_ = new pp::flash::MessageLoop(instance_); |
33 | 103 |
34 pp::CompletionCallback callback = callback_factory_.NewCallback( | 104 pp::CompletionCallback callback = callback_factory_.NewCallback( |
35 &TestFlashMessageLoop::QuitMessageLoopTask); | 105 &TestFlashMessageLoop::QuitMessageLoopTask); |
36 pp::Module::Get()->core()->CallOnMainThread(0, callback); | 106 pp::Module::Get()->core()->CallOnMainThread(0, callback); |
37 int32_t result = message_loop_->Run(); | 107 int32_t result = message_loop_->Run(); |
38 | 108 |
39 ASSERT_TRUE(message_loop_); | 109 ASSERT_TRUE(message_loop_); |
40 delete message_loop_; | 110 delete message_loop_; |
41 message_loop_ = NULL; | 111 message_loop_ = nullptr; |
42 | 112 |
43 ASSERT_EQ(PP_OK, result); | 113 ASSERT_EQ(PP_OK, result); |
44 PASS(); | 114 PASS(); |
45 } | 115 } |
46 | 116 |
47 std::string TestFlashMessageLoop::TestRunWithoutQuit() { | 117 std::string TestFlashMessageLoop::TestRunWithoutQuit() { |
48 message_loop_ = new pp::flash::MessageLoop(instance_); | 118 message_loop_ = new pp::flash::MessageLoop(instance_); |
49 | 119 |
50 pp::CompletionCallback callback = callback_factory_.NewCallback( | 120 pp::CompletionCallback callback = callback_factory_.NewCallback( |
51 &TestFlashMessageLoop::DestroyMessageLoopResourceTask); | 121 &TestFlashMessageLoop::DestroyMessageLoopResourceTask); |
52 pp::Module::Get()->core()->CallOnMainThread(0, callback); | 122 pp::Module::Get()->core()->CallOnMainThread(0, callback); |
53 int32_t result = message_loop_->Run(); | 123 int32_t result = message_loop_->Run(); |
54 | 124 |
55 if (message_loop_) { | 125 if (message_loop_) { |
56 delete message_loop_; | 126 delete message_loop_; |
57 message_loop_ = NULL; | 127 message_loop_ = nullptr; |
58 ASSERT_TRUE(false); | 128 ASSERT_TRUE(false); |
59 } | 129 } |
60 | 130 |
61 ASSERT_EQ(PP_ERROR_ABORTED, result); | 131 ASSERT_EQ(PP_ERROR_ABORTED, result); |
62 PASS(); | 132 PASS(); |
63 } | 133 } |
64 | 134 |
| 135 std::string TestFlashMessageLoop::TestSuspendScriptCallbackWhileRunning() { |
| 136 suspend_script_callback_result_ = true; |
| 137 message_loop_ = new pp::flash::MessageLoop(instance_); |
| 138 |
| 139 pp::CompletionCallback callback = callback_factory_.NewCallback( |
| 140 &TestFlashMessageLoop::TestSuspendScriptCallbackTask); |
| 141 pp::Module::Get()->core()->CallOnMainThread(0, callback); |
| 142 message_loop_->Run(); |
| 143 |
| 144 ASSERT_TRUE(message_loop_); |
| 145 delete message_loop_; |
| 146 message_loop_ = nullptr; |
| 147 |
| 148 ASSERT_TRUE(suspend_script_callback_result_); |
| 149 PASS(); |
| 150 } |
| 151 |
| 152 void TestFlashMessageLoop::TestSuspendScriptCallbackTask(int32_t unused) { |
| 153 pp::Var exception; |
| 154 pp::Var rev = instance_->ExecuteScript( |
| 155 "(function() {" |
| 156 " function delayedHandler() {" |
| 157 " document.getElementById('plugin').DidRunScriptCallback();" |
| 158 " }" |
| 159 " setTimeout(delayedHandler, 1);" |
| 160 "})()", |
| 161 &exception); |
| 162 if (!exception.is_undefined()) |
| 163 suspend_script_callback_result_ = false; |
| 164 |
| 165 pp::CompletionCallback callback = |
| 166 callback_factory_.NewCallback(&TestFlashMessageLoop::QuitMessageLoopTask); |
| 167 pp::Module::Get()->core()->CallOnMainThread(500, callback); |
| 168 } |
| 169 |
65 void TestFlashMessageLoop::QuitMessageLoopTask(int32_t unused) { | 170 void TestFlashMessageLoop::QuitMessageLoopTask(int32_t unused) { |
66 if (message_loop_) | 171 if (message_loop_) |
67 message_loop_->Quit(); | 172 message_loop_->Quit(); |
68 else | 173 else |
69 PP_NOTREACHED(); | 174 PP_NOTREACHED(); |
70 } | 175 } |
71 | 176 |
72 void TestFlashMessageLoop::DestroyMessageLoopResourceTask(int32_t unused) { | 177 void TestFlashMessageLoop::DestroyMessageLoopResourceTask(int32_t unused) { |
73 if (message_loop_) { | 178 if (message_loop_) { |
74 delete message_loop_; | 179 delete message_loop_; |
75 message_loop_ = NULL; | 180 message_loop_ = nullptr; |
76 } else { | 181 } else { |
77 PP_NOTREACHED(); | 182 PP_NOTREACHED(); |
78 } | 183 } |
79 } | 184 } |
OLD | NEW |