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_utils.h" | 5 #include "ppapi/tests/test_utils.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | |
8 #if defined(_MSC_VER) | 9 #if defined(_MSC_VER) |
9 #include <windows.h> | 10 #include <windows.h> |
10 #else | 11 #else |
11 #include <unistd.h> | 12 #include <unistd.h> |
12 #endif | 13 #endif |
13 | 14 |
14 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/cpp/module.h" | 16 #include "ppapi/cpp/module.h" |
17 #include "ppapi/cpp/var.h" | |
16 | 18 |
17 const int kActionTimeoutMs = 10000; | 19 const int kActionTimeoutMs = 10000; |
18 | 20 |
19 const PPB_Testing_Dev* GetTestingInterface() { | 21 const PPB_Testing_Dev* GetTestingInterface() { |
20 static const PPB_Testing_Dev* g_testing_interface = | 22 static const PPB_Testing_Dev* g_testing_interface = |
21 static_cast<const PPB_Testing_Dev*>( | 23 static_cast<const PPB_Testing_Dev*>( |
22 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); | 24 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); |
23 return g_testing_interface; | 25 return g_testing_interface; |
24 } | 26 } |
25 | 27 |
26 std::string ReportError(const char* method, int32_t error) { | 28 std::string ReportError(const char* method, int32_t error) { |
27 char error_as_string[12]; | 29 char error_as_string[12]; |
28 sprintf(error_as_string, "%d", static_cast<int>(error)); | 30 sprintf(error_as_string, "%d", static_cast<int>(error)); |
29 std::string result = method + std::string(" failed with error: ") + | 31 std::string result = method + std::string(" failed with error: ") + |
30 error_as_string; | 32 error_as_string; |
31 return result; | 33 return result; |
32 } | 34 } |
33 | 35 |
34 void PlatformSleep(int duration_ms) { | 36 void PlatformSleep(int duration_ms) { |
35 #if defined(_MSC_VER) | 37 #if defined(_MSC_VER) |
36 ::Sleep(duration_ms); | 38 ::Sleep(duration_ms); |
37 #else | 39 #else |
38 usleep(duration_ms * 1000); | 40 usleep(duration_ms * 1000); |
39 #endif | 41 #endif |
40 } | 42 } |
41 | 43 |
44 bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port) { | |
45 if (!host || !port) | |
46 return false; | |
47 | |
48 const PPB_Testing_Dev* testing = GetTestingInterface(); | |
49 if (!testing) | |
50 return false; | |
51 | |
52 PP_URLComponents_Dev components; | |
53 pp::Var pp_url(pp::Var::PassRef(), | |
54 testing->GetDocumentURL(instance, &components)); | |
55 if (!pp_url.is_string()) | |
56 return false; | |
57 std::string url = pp_url.AsString(); | |
58 | |
59 if (components.host.len < 0) | |
60 return false; | |
61 host->assign(url.substr(components.host.begin, components.host.len)); | |
62 | |
63 if (components.port.len <= 0) | |
64 return false; | |
65 else { | |
yzshen1
2011/12/14 19:16:54
nit: no need to use 'else'.
if (...)
return fal
brettw
2011/12/14 22:36:47
Yeah, we generally frown on using else after retur
ygorshenin
2011/12/15 11:48:22
Done.
ygorshenin
2011/12/15 11:48:22
Done.
| |
66 int i = atoi(url.substr(components.port.begin, | |
67 components.port.len).c_str()); | |
68 if (i < 0 || i > 65535) | |
69 return false; | |
70 *port = static_cast<uint16_t>(i); | |
71 } | |
72 | |
73 return true; | |
74 } | |
75 | |
42 TestCompletionCallback::TestCompletionCallback(PP_Instance instance) | 76 TestCompletionCallback::TestCompletionCallback(PP_Instance instance) |
43 : have_result_(false), | 77 : have_result_(false), |
44 result_(PP_OK_COMPLETIONPENDING), | 78 result_(PP_OK_COMPLETIONPENDING), |
45 force_async_(false), | 79 force_async_(false), |
46 post_quit_task_(false), | 80 post_quit_task_(false), |
47 run_count_(0), | 81 run_count_(0), |
48 instance_(instance) { | 82 instance_(instance) { |
49 } | 83 } |
50 | 84 |
51 TestCompletionCallback::TestCompletionCallback(PP_Instance instance, | 85 TestCompletionCallback::TestCompletionCallback(PP_Instance instance, |
(...skipping 28 matching lines...) Expand all Loading... | |
80 TestCompletionCallback* callback = | 114 TestCompletionCallback* callback = |
81 static_cast<TestCompletionCallback*>(user_data); | 115 static_cast<TestCompletionCallback*>(user_data); |
82 callback->result_ = result; | 116 callback->result_ = result; |
83 callback->have_result_ = true; | 117 callback->have_result_ = true; |
84 callback->run_count_++; | 118 callback->run_count_++; |
85 if (callback->post_quit_task_) { | 119 if (callback->post_quit_task_) { |
86 callback->post_quit_task_ = false; | 120 callback->post_quit_task_ = false; |
87 GetTestingInterface()->QuitMessageLoop(callback->instance_); | 121 GetTestingInterface()->QuitMessageLoop(callback->instance_); |
88 } | 122 } |
89 } | 123 } |
OLD | NEW |