Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: ppapi/tests/test_utils.cc

Issue 8840007: GetDocumentURL is added to PPB_Testing_Dev. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #if defined(_MSC_VER) 8 #if defined(_MSC_VER)
9 #include <windows.h> 9 #include <windows.h>
10 #else 10 #else
11 #include <unistd.h> 11 #include <unistd.h>
12 #endif 12 #endif
13 13
14 #include "ppapi/c/pp_errors.h" 14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/cpp/module.h" 15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/var.h"
16 17
17 const int kActionTimeoutMs = 10000; 18 const int kActionTimeoutMs = 10000;
18 19
19 const PPB_Testing_Dev* GetTestingInterface() { 20 const PPB_Testing_Dev* GetTestingInterface() {
20 static const PPB_Testing_Dev* g_testing_interface = 21 static const PPB_Testing_Dev* g_testing_interface =
21 reinterpret_cast<PPB_Testing_Dev const*>( 22 reinterpret_cast<PPB_Testing_Dev const*>(
22 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); 23 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
23 return g_testing_interface; 24 return g_testing_interface;
24 } 25 }
25 26
26 std::string ReportError(const char* method, int32_t error) { 27 std::string ReportError(const char* method, int32_t error) {
27 char error_as_string[12]; 28 char error_as_string[12];
28 sprintf(error_as_string, "%d", static_cast<int>(error)); 29 sprintf(error_as_string, "%d", static_cast<int>(error));
29 std::string result = method + std::string(" failed with error: ") + 30 std::string result = method + std::string(" failed with error: ") +
30 error_as_string; 31 error_as_string;
31 return result; 32 return result;
32 } 33 }
33 34
34 void PlatformSleep(int duration_ms) { 35 void PlatformSleep(int duration_ms) {
35 #if defined(_MSC_VER) 36 #if defined(_MSC_VER)
36 ::Sleep(duration_ms); 37 ::Sleep(duration_ms);
37 #else 38 #else
38 usleep(duration_ms * 1000); 39 usleep(duration_ms * 1000);
39 #endif 40 #endif
40 } 41 }
41 42
43 bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port) {
44 if (!host || !port)
45 return false;
46
47 const PPB_Testing_Dev* testing = GetTestingInterface();
48 if (!testing)
49 return false;
50
51 PP_URLComponents_Dev components;
52 pp::Var pp_url(pp::Var::PassRef(),
53 testing->GetDocumentURL(instance, &components));
54 if (!pp_url.is_string())
55 return false;
56 std::string url = pp_url.AsString();
57
58 if (components.host.len < 0)
59 return false;
60 host->assign(url.substr(components.host.begin, components.host.len));
61
62 if (components.port.len > 0) {
yzshen1 2011/12/13 18:49:54 You could reverse the condition and return early o
ygorshenin 2011/12/14 17:41:10 Done.
63 int i = atoi(url.substr(components.port.begin,
yzshen1 2011/12/13 18:49:54 Please add include for atoi.
ygorshenin 2011/12/14 17:41:10 Done.
64 components.port.len).c_str());
65 if (i < 0 || i > 65535)
66 return false;
67 *port = static_cast<uint16_t>(i);
68 } else
69 return false;
70
71 return true;
72 }
73
42 TestCompletionCallback::TestCompletionCallback(PP_Instance instance) 74 TestCompletionCallback::TestCompletionCallback(PP_Instance instance)
43 : have_result_(false), 75 : have_result_(false),
44 result_(PP_OK_COMPLETIONPENDING), 76 result_(PP_OK_COMPLETIONPENDING),
45 force_async_(false), 77 force_async_(false),
46 post_quit_task_(false), 78 post_quit_task_(false),
47 run_count_(0), 79 run_count_(0),
48 instance_(instance) { 80 instance_(instance) {
49 } 81 }
50 82
51 TestCompletionCallback::TestCompletionCallback(PP_Instance instance, 83 TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
(...skipping 28 matching lines...) Expand all
80 TestCompletionCallback* callback = 112 TestCompletionCallback* callback =
81 static_cast<TestCompletionCallback*>(user_data); 113 static_cast<TestCompletionCallback*>(user_data);
82 callback->result_ = result; 114 callback->result_ = result;
83 callback->have_result_ = true; 115 callback->have_result_ = true;
84 callback->run_count_++; 116 callback->run_count_++;
85 if (callback->post_quit_task_) { 117 if (callback->post_quit_task_) {
86 callback->post_quit_task_ = false; 118 callback->post_quit_task_ = false;
87 GetTestingInterface()->QuitMessageLoop(callback->instance_); 119 GetTestingInterface()->QuitMessageLoop(callback->instance_);
88 } 120 }
89 } 121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698