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 "chrome/test/base/ui_test_utils.h" | 5 #include "chrome/test/base/ui_test_utils.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #if defined(OS_WIN) | |
Paweł Hajdan Jr.
2011/11/22 16:38:37
nit: I think we include C headers before C++ ones.
Takashi Toyoshima
2011/11/22 20:13:42
Oh, sorry and thanks.
| |
10 #include <windows.h> | |
11 #endif | |
12 | |
9 #include "base/bind.h" | 13 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 15 #include "base/callback.h" |
12 #include "base/command_line.h" | 16 #include "base/command_line.h" |
13 #include "base/file_path.h" | 17 #include "base/file_path.h" |
14 #include "base/json/json_reader.h" | 18 #include "base/json/json_reader.h" |
15 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
17 #include "base/path_service.h" | 21 #include "base/path_service.h" |
18 #include "base/process_util.h" | 22 #include "base/process_util.h" |
23 #include "base/string_number_conversions.h" | |
19 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
20 #include "base/values.h" | 25 #include "base/values.h" |
21 #include "chrome/browser/automation/ui_controls.h" | 26 #include "chrome/browser/automation/ui_controls.h" |
22 #include "chrome/browser/bookmarks/bookmark_model.h" | 27 #include "chrome/browser/bookmarks/bookmark_model.h" |
23 #include "chrome/browser/browser_process.h" | 28 #include "chrome/browser/browser_process.h" |
24 #include "chrome/browser/dom_operation_notification_details.h" | 29 #include "chrome/browser/dom_operation_notification_details.h" |
25 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/search_engines/template_url_service.h" | 31 #include "chrome/browser/search_engines/template_url_service.h" |
27 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 32 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
28 #include "chrome/browser/tab_contents/thumbnail_generator.h" | 33 #include "chrome/browser/tab_contents/thumbnail_generator.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
55 #include "ui/gfx/size.h" | 60 #include "ui/gfx/size.h" |
56 | 61 |
57 #if defined(TOOLKIT_VIEWS) | 62 #if defined(TOOLKIT_VIEWS) |
58 #include "ui/views/focus/accelerator_handler.h" | 63 #include "ui/views/focus/accelerator_handler.h" |
59 #endif | 64 #endif |
60 | 65 |
61 #if defined(USE_AURA) | 66 #if defined(USE_AURA) |
62 #include "ui/aura/desktop.h" | 67 #include "ui/aura/desktop.h" |
63 #endif | 68 #endif |
64 | 69 |
70 static const int kDefaultWsPort = 8880; | |
71 | |
65 namespace ui_test_utils { | 72 namespace ui_test_utils { |
66 | 73 |
67 namespace { | 74 namespace { |
68 | 75 |
69 class DOMOperationObserver : public content::NotificationObserver { | 76 class DOMOperationObserver : public content::NotificationObserver { |
70 public: | 77 public: |
71 explicit DOMOperationObserver(RenderViewHost* render_view_host) | 78 explicit DOMOperationObserver(RenderViewHost* render_view_host) |
72 : did_respond_(false) { | 79 : did_respond_(false) { |
73 registrar_.Add(this, chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, | 80 registrar_.Add(this, chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, |
74 content::Source<RenderViewHost>(render_view_host)); | 81 content::Source<RenderViewHost>(render_view_host)); |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
690 std::string newpath(oldpath); | 697 std::string newpath(oldpath); |
691 newpath.append(":"); | 698 newpath.append(":"); |
692 newpath.append(dir.value()); | 699 newpath.append(dir.value()); |
693 setenv(kPythonPath, newpath.c_str(), 1); | 700 setenv(kPythonPath, newpath.c_str(), 1); |
694 } | 701 } |
695 #endif | 702 #endif |
696 } | 703 } |
697 | 704 |
698 } // anonymous namespace | 705 } // anonymous namespace |
699 | 706 |
707 #if defined(OS_POSIX) | |
708 class OrphanedWebSocketServerFilter : public base::ProcessFilter { | |
709 public: | |
710 OrphanedWebSocketServerFilter(std::string script_name, std::string port) | |
711 : script_name_(script_name), | |
712 port_(port) {} | |
713 | |
714 virtual bool Includes(const base::ProcessEntry& entry) const { | |
715 // The parent process of orphaned WebSocket servers must be root process. | |
716 if (entry.parent_pid() != 1) | |
717 return false; | |
718 | |
719 // Python script name must be ended with specified script name. | |
720 const std::string &script_name = entry.cmd_line_args()[2]; | |
721 if (script_name.find_last_of(script_name_) != script_name.size() - 1) | |
722 return false; | |
723 | |
724 // Check if an argument sequence like "--port 8880" exist. | |
725 bool found = false; | |
726 for (std::vector<std::string>::const_iterator it = | |
727 entry.cmd_line_args().begin(); | |
728 it != entry.cmd_line_args().end(); | |
729 ++it) { | |
730 if (*it == "--port") { | |
731 ++it; | |
732 if (it == entry.cmd_line_args().end() && *it == port_) { | |
733 found = true; | |
734 break; | |
735 } | |
736 } | |
737 } | |
738 return found; | |
739 } | |
740 | |
741 private: | |
742 std::string script_name_; | |
743 std::string port_; | |
744 DISALLOW_COPY_AND_ASSIGN(OrphanedWebSocketServerFilter); | |
745 }; | |
746 #endif | |
747 | |
700 TestWebSocketServer::TestWebSocketServer() : started_(false) { | 748 TestWebSocketServer::TestWebSocketServer() : started_(false) { |
701 } | 749 } |
702 | 750 |
703 bool TestWebSocketServer::Start(const FilePath& root_directory) { | 751 bool TestWebSocketServer::Start(const FilePath& root_directory) { |
704 if (started_) | 752 if (started_) |
705 return true; | 753 return true; |
706 // Append CommandLine arguments after the server script, switches won't work. | 754 // Append CommandLine arguments after the server script, switches won't work. |
707 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); | 755 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); |
708 cmd_line->AppendArg("--server=start"); | 756 cmd_line->AppendArg("--server=start"); |
709 cmd_line->AppendArg("--chromium"); | 757 cmd_line->AppendArg("--chromium"); |
710 cmd_line->AppendArg("--register_cygwin"); | 758 cmd_line->AppendArg("--register_cygwin"); |
711 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") + | 759 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") + |
712 root_directory.value()); | 760 root_directory.value()); |
761 cmd_line->AppendArg("--port=" + base::IntToString(kDefaultWsPort)); | |
713 if (!temp_dir_.CreateUniqueTempDir()) { | 762 if (!temp_dir_.CreateUniqueTempDir()) { |
714 LOG(ERROR) << "Unable to create a temporary directory."; | 763 LOG(ERROR) << "Unable to create a temporary directory."; |
715 return false; | 764 return false; |
716 } | 765 } |
717 websocket_pid_file_ = temp_dir_.path().AppendASCII("websocket.pid"); | 766 websocket_pid_file_ = temp_dir_.path().AppendASCII("websocket.pid"); |
718 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") + | 767 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") + |
719 websocket_pid_file_.value()); | 768 websocket_pid_file_.value()); |
720 SetPythonPath(); | 769 SetPythonPath(); |
770 | |
721 base::LaunchOptions options; | 771 base::LaunchOptions options; |
772 | |
773 #if defined(OS_POSIX) | |
774 // Try to kill any orphaned WebSocket server processes that may be running. | |
775 OrphanedWebSocketServerFilter filter("standalone.py", | |
776 base::IntToString(kDefaultWsPort)); | |
777 if (!base::KillProcesses("python", -1, &filter)) | |
Paweł Hajdan Jr.
2011/11/22 16:38:37
I think you could use process groups on POSIX in a
Takashi Toyoshima
2011/11/22 20:13:42
Done.
| |
778 LOG(WARNING) << | |
779 "Failed to clean up older orphaned WebSocket server instance."; | |
780 #elif defined(OS_WIN) | |
781 job_handle_.Set(CreateJobObject(NULL, NULL)); | |
782 if (!job_handle_.IsValid()) { | |
783 LOG(ERROR) << "Could not create JobObject."; | |
784 return false; | |
785 } | |
786 | |
787 JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info = {0}; | |
788 limit_info.BasicLimitInformation.LimitFlags = | |
789 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; | |
790 if (0 == SetInformationJobObject(job_handle_.Get(), | |
Paweł Hajdan Jr.
2011/11/22 16:38:37
I think this is the third occurrence of this code
Takashi Toyoshima
2011/11/22 20:13:42
OK, I found the same code sequence in five source
| |
791 JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info))) { | |
792 LOG(ERROR) << "Could not SetInformationJobObject."; | |
793 return false; | |
794 } | |
795 | |
796 options.inherit_handles = true; | |
797 options.job_handle = job_handle_.Get(); | |
798 #endif | |
799 | |
800 // Launch a new WebSocket server process. | |
722 options.wait = true; | 801 options.wait = true; |
723 if (!base::LaunchProcess(*cmd_line.get(), options, NULL)) { | 802 if (!base::LaunchProcess(*cmd_line.get(), options, NULL)) { |
724 LOG(ERROR) << "Unable to launch websocket server."; | 803 LOG(ERROR) << "Unable to launch websocket server."; |
725 return false; | 804 return false; |
726 } | 805 } |
727 started_ = true; | 806 started_ = true; |
728 return true; | 807 return true; |
729 } | 808 } |
730 | 809 |
731 CommandLine* TestWebSocketServer::CreatePythonCommandLine() { | 810 CommandLine* TestWebSocketServer::CreatePythonCommandLine() { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
995 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); | 1074 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
996 } | 1075 } |
997 | 1076 |
998 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { | 1077 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
999 DCHECK(bitmap); | 1078 DCHECK(bitmap); |
1000 SnapshotTaker taker; | 1079 SnapshotTaker taker; |
1001 return taker.TakeEntirePageSnapshot(rvh, bitmap); | 1080 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
1002 } | 1081 } |
1003 | 1082 |
1004 } // namespace ui_test_utils | 1083 } // namespace ui_test_utils |
OLD | NEW |