| 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/testing_instance.h" | 5 #include "ppapi/tests/testing_instance.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
| 12 #include "ppapi/cpp/var.h" | 12 #include "ppapi/cpp/var.h" |
| 13 #include "ppapi/tests/test_case.h" | 13 #include "ppapi/tests/test_case.h" |
| 14 | 14 |
| 15 TestCaseFactory* TestCaseFactory::head_ = NULL; | 15 TestCaseFactory* TestCaseFactory::head_ = NULL; |
| 16 | 16 |
| 17 // Returns a new heap-allocated test case for the given test, or NULL on | 17 // Returns a new heap-allocated test case for the given test, or NULL on |
| 18 // failure. | 18 // failure. |
| 19 TestingInstance::TestingInstance(PP_Instance instance) | 19 TestingInstance::TestingInstance(PP_Instance instance) |
| 20 #if (defined __native_client__) | |
| 21 : pp::Instance(instance), | 20 : pp::Instance(instance), |
| 22 #else | |
| 23 : pp::InstancePrivate(instance), | |
| 24 #endif | |
| 25 current_case_(NULL), | 21 current_case_(NULL), |
| 26 executed_tests_(false), | 22 executed_tests_(false), |
| 27 nacl_mode_(false) { | 23 nacl_mode_(false) { |
| 28 callback_factory_.Initialize(this); | 24 callback_factory_.Initialize(this); |
| 29 } | 25 } |
| 30 | 26 |
| 31 TestingInstance::~TestingInstance() { | 27 TestingInstance::~TestingInstance() { |
| 32 if (current_case_) | 28 if (current_case_) |
| 33 delete current_case_; | 29 delete current_case_; |
| 34 } | 30 } |
| 35 | 31 |
| 36 bool TestingInstance::Init(uint32_t argc, | 32 bool TestingInstance::Init(uint32_t argc, |
| 37 const char* argn[], | 33 const char* argn[], |
| 38 const char* argv[]) { | 34 const char* argv[]) { |
| 39 for (uint32_t i = 0; i < argc; i++) { | 35 for (uint32_t i = 0; i < argc; i++) { |
| 40 if (std::strcmp(argn[i], "mode") == 0) { | 36 if (std::strcmp(argn[i], "mode") == 0) { |
| 41 if (std::strcmp(argv[i], "nacl") == 0) | 37 if (std::strcmp(argv[i], "nacl") == 0) |
| 42 nacl_mode_ = true; | 38 nacl_mode_ = true; |
| 39 break; |
| 43 } | 40 } |
| 44 else if (std::strcmp(argn[i], "protocol") == 0) | |
| 45 protocol_ = argv[i]; | |
| 46 } | 41 } |
| 47 // Create the proper test case from the argument. | 42 // Create the proper test case from the argument. |
| 48 for (uint32_t i = 0; i < argc; i++) { | 43 for (uint32_t i = 0; i < argc; i++) { |
| 49 if (std::strcmp(argn[i], "testcase") == 0) { | 44 if (std::strcmp(argn[i], "testcase") == 0) { |
| 50 if (argv[i][0] == '\0') | 45 if (argv[i][0] == '\0') |
| 51 break; | 46 break; |
| 52 current_case_ = CaseForTestName(argv[i]); | 47 current_case_ = CaseForTestName(argv[i]); |
| 53 if (!current_case_) | 48 if (!current_case_) |
| 54 errors_.append(std::string("Unknown test case ") + argv[i]); | 49 errors_.append(std::string("Unknown test case ") + argv[i]); |
| 55 else if (!current_case_->Init()) | 50 else if (!current_case_->Init()) |
| 56 errors_.append(" Test case could not initialize."); | 51 errors_.append(" Test case could not initialize."); |
| 57 return true; | 52 return true; |
| 58 } | 53 } |
| 59 } | 54 } |
| 60 | 55 |
| 61 // In DidChangeView, we'll dump out a list of all available tests. | 56 // In DidChangeView, we'll dump out a list of all available tests. |
| 62 return true; | 57 return true; |
| 63 } | 58 } |
| 64 | 59 |
| 65 #if !(defined __native_client__) | |
| 66 pp::Var TestingInstance::GetInstanceObject() { | 60 pp::Var TestingInstance::GetInstanceObject() { |
| 67 if (current_case_) | 61 if (current_case_) |
| 68 return current_case_->GetTestObject(); | 62 return current_case_->GetTestObject(); |
| 69 | 63 |
| 70 return pp::VarPrivate(); | 64 return pp::Var(this, NULL); |
| 71 } | 65 } |
| 72 #endif | |
| 73 | 66 |
| 74 void TestingInstance::HandleMessage(const pp::Var& message_data) { | 67 void TestingInstance::HandleMessage(const pp::Var& message_data) { |
| 75 current_case_->HandleMessage(message_data); | 68 current_case_->HandleMessage(message_data); |
| 76 } | 69 } |
| 77 | 70 |
| 78 void TestingInstance::DidChangeView(const pp::Rect& position, | 71 void TestingInstance::DidChangeView(const pp::Rect& position, |
| 79 const pp::Rect& clip) { | 72 const pp::Rect& clip) { |
| 80 if (!executed_tests_) { | 73 if (!executed_tests_) { |
| 81 executed_tests_ = true; | 74 executed_tests_ = true; |
| 82 pp::Module::Get()->core()->CallOnMainThread( | 75 pp::Module::Get()->core()->CallOnMainThread( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 109 void TestingInstance::AppendError(const std::string& message) { | 102 void TestingInstance::AppendError(const std::string& message) { |
| 110 if (!errors_.empty()) | 103 if (!errors_.empty()) |
| 111 errors_.append(", "); | 104 errors_.append(", "); |
| 112 errors_.append(message); | 105 errors_.append(message); |
| 113 } | 106 } |
| 114 | 107 |
| 115 void TestingInstance::ExecuteTests(int32_t unused) { | 108 void TestingInstance::ExecuteTests(int32_t unused) { |
| 116 SetCookie("STARTUP_COOKIE", "STARTED"); | 109 SetCookie("STARTUP_COOKIE", "STARTED"); |
| 117 | 110 |
| 118 // Clear the console. | 111 // Clear the console. |
| 119 PostMessage(pp::Var("TESTING_MESSAGE:ClearConsole")); | 112 // This does: window.document.getElementById("console").innerHTML = ""; |
| 113 pp::Var window = GetWindowObject(); |
| 114 window.GetProperty("document"). |
| 115 Call("getElementById", "console").SetProperty("innerHTML", ""); |
| 120 | 116 |
| 121 if (!errors_.empty()) { | 117 if (!errors_.empty()) { |
| 122 // Catch initialization errors and output the current error string to | 118 // Catch initialization errors and output the current error string to |
| 123 // the console. | 119 // the console. |
| 124 LogError("Plugin initialization failed: " + errors_); | 120 LogError("Plugin initialization failed: " + errors_); |
| 125 } else if (!current_case_) { | 121 } else if (!current_case_) { |
| 126 LogAvailableTests(); | 122 LogAvailableTests(); |
| 127 errors_.append("FAIL: Only listed tests"); | 123 errors_.append("FAIL: Only listed tests"); |
| 128 } else { | 124 } else { |
| 129 current_case_->RunTest(); | 125 current_case_->RunTest(); |
| 130 // Automated PyAuto tests rely on finding the exact strings below. | 126 // Automated PyAuto tests rely on finding the exact strings below. |
| 131 LogHTML(errors_.empty() ? | 127 LogHTML(errors_.empty() ? |
| 132 "<span class=\"pass\">[SHUTDOWN]</span> All tests passed." : | 128 "<span class=\"pass\">[SHUTDOWN]</span> All tests passed." : |
| 133 "<span class=\"fail\">[SHUTDOWN]</span> Some tests failed."); | 129 "<span class=\"fail\">[SHUTDOWN]</span> Some tests failed."); |
| 134 } | 130 } |
| 135 | 131 |
| 136 // Declare we're done by setting a cookie to either "PASS" or the errors. | 132 // Declare we're done by setting a cookie to either "PASS" or the errors. |
| 137 SetCookie("COMPLETION_COOKIE", errors_.empty() ? "PASS" : errors_); | 133 SetCookie("COMPLETION_COOKIE", errors_.empty() ? "PASS" : errors_); |
| 138 PostMessage(pp::Var("TESTING_MESSAGE:DidExecuteTests")); | 134 |
| 135 window.Call("DidExecuteTests"); |
| 139 } | 136 } |
| 140 | 137 |
| 141 TestCase* TestingInstance::CaseForTestName(const char* name) { | 138 TestCase* TestingInstance::CaseForTestName(const char* name) { |
| 142 TestCaseFactory* iter = TestCaseFactory::head_; | 139 TestCaseFactory* iter = TestCaseFactory::head_; |
| 143 while (iter != NULL) { | 140 while (iter != NULL) { |
| 144 if (std::strcmp(name, iter->name_) == 0) | 141 if (std::strcmp(name, iter->name_) == 0) |
| 145 return iter->method_(this); | 142 return iter->method_(this); |
| 146 iter = iter->next_; | 143 iter = iter->next_; |
| 147 } | 144 } |
| 148 return NULL; | 145 return NULL; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 164 html.append("<dd><a href='?testcase="); | 161 html.append("<dd><a href='?testcase="); |
| 165 html.append(test_cases[i]); | 162 html.append(test_cases[i]); |
| 166 if (nacl_mode_) | 163 if (nacl_mode_) |
| 167 html.append("&mode=nacl"); | 164 html.append("&mode=nacl"); |
| 168 html.append("'>"); | 165 html.append("'>"); |
| 169 html.append(test_cases[i]); | 166 html.append(test_cases[i]); |
| 170 html.append("</a></dd>"); | 167 html.append("</a></dd>"); |
| 171 } | 168 } |
| 172 html.append("</dl>"); | 169 html.append("</dl>"); |
| 173 html.append("<button onclick='RunAll()'>Run All Tests</button>"); | 170 html.append("<button onclick='RunAll()'>Run All Tests</button>"); |
| 174 | |
| 175 LogHTML(html); | 171 LogHTML(html); |
| 176 } | 172 } |
| 177 | 173 |
| 178 void TestingInstance::LogError(const std::string& text) { | 174 void TestingInstance::LogError(const std::string& text) { |
| 179 std::string html; | 175 std::string html; |
| 180 html.append("<span class=\"fail\">FAIL</span>: <span class=\"err_msg\">"); | 176 html.append("<span class=\"fail\">FAIL</span>: <span class=\"err_msg\">"); |
| 181 html.append(text); | 177 html.append(text); |
| 182 html.append("</span>"); | 178 html.append("</span>"); |
| 183 LogHTML(html); | 179 LogHTML(html); |
| 184 } | 180 } |
| 185 | 181 |
| 186 void TestingInstance::LogHTML(const std::string& html) { | 182 void TestingInstance::LogHTML(const std::string& html) { |
| 187 std::string message("TESTING_MESSAGE:LogHTML:"); | 183 // This does: window.document.getElementById("console").innerHTML += html |
| 188 message.append(html); | 184 pp::Var console = GetWindowObject().GetProperty("document"). |
| 189 PostMessage(pp::Var(message)); | 185 Call("getElementById", "console"); |
| 186 pp::Var inner_html = console.GetProperty("innerHTML"); |
| 187 console.SetProperty("innerHTML", inner_html.AsString() + html); |
| 190 } | 188 } |
| 191 | 189 |
| 192 void TestingInstance::SetCookie(const std::string& name, | 190 void TestingInstance::SetCookie(const std::string& name, |
| 193 const std::string& value) { | 191 const std::string& value) { |
| 194 std::string message("TESTING_MESSAGE:SetCookie:"); | 192 // window.document.cookie = "<name>=<value>; path=/" |
| 195 message.append(name); | 193 std::string cookie_string = name + "=" + value + "; path=/"; |
| 196 message.append("="); | 194 pp::Var document = GetWindowObject().GetProperty("document"); |
| 197 message.append(value); | 195 document.SetProperty("cookie", cookie_string); |
| 198 PostMessage(pp::Var(message)); | |
| 199 } | 196 } |
| 200 | 197 |
| 201 class Module : public pp::Module { | 198 class Module : public pp::Module { |
| 202 public: | 199 public: |
| 203 Module() : pp::Module() {} | 200 Module() : pp::Module() {} |
| 204 virtual ~Module() {} | 201 virtual ~Module() {} |
| 205 | 202 |
| 206 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 203 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 207 return new TestingInstance(instance); | 204 return new TestingInstance(instance); |
| 208 } | 205 } |
| 209 }; | 206 }; |
| 210 | 207 |
| 211 namespace pp { | 208 namespace pp { |
| 212 | 209 |
| 213 Module* CreateModule() { | 210 Module* CreateModule() { |
| 214 return new ::Module(); | 211 return new ::Module(); |
| 215 } | 212 } |
| 216 | 213 |
| 217 } // namespace pp | 214 } // namespace pp |
| OLD | NEW |