OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/ui_test_utils.h" | 5 #include "chrome/test/ui_test_utils.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "base/callback.h" |
9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
14 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/scoped_ptr.h" |
15 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
16 #include "base/values.h" | 18 #include "base/values.h" |
17 #include "chrome/browser/automation/ui_controls.h" | 19 #include "chrome/browser/automation/ui_controls.h" |
18 #include "chrome/browser/browser.h" | 20 #include "chrome/browser/browser.h" |
19 #include "chrome/browser/browser_list.h" | 21 #include "chrome/browser/browser_list.h" |
| 22 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/browser_window.h" | 23 #include "chrome/browser/browser_window.h" |
21 #include "chrome/browser/dom_operation_notification_details.h" | 24 #include "chrome/browser/dom_operation_notification_details.h" |
22 #include "chrome/browser/download/download_item.h" | 25 #include "chrome/browser/download/download_item.h" |
23 #include "chrome/browser/download/download_manager.h" | 26 #include "chrome/browser/download/download_manager.h" |
24 #include "chrome/browser/profile.h" | 27 #include "chrome/browser/profile.h" |
25 #include "chrome/browser/renderer_host/render_process_host.h" | 28 #include "chrome/browser/renderer_host/render_process_host.h" |
26 #include "chrome/browser/renderer_host/render_view_host.h" | 29 #include "chrome/browser/renderer_host/render_view_host.h" |
27 #include "chrome/browser/tab_contents/navigation_controller.h" | 30 #include "chrome/browser/tab_contents/navigation_controller.h" |
28 #include "chrome/browser/tab_contents/navigation_entry.h" | 31 #include "chrome/browser/tab_contents/navigation_entry.h" |
29 #include "chrome/browser/tab_contents/tab_contents.h" | 32 #include "chrome/browser/tab_contents/tab_contents.h" |
| 33 #include "chrome/browser/tab_contents/thumbnail_generator.h" |
30 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
31 #include "chrome/common/extensions/extension_action.h" | 35 #include "chrome/common/extensions/extension_action.h" |
32 #include "chrome/common/notification_type.h" | 36 #include "chrome/common/notification_type.h" |
33 #include "chrome/test/automation/javascript_execution_controller.h" | 37 #include "chrome/test/automation/javascript_execution_controller.h" |
34 #include "chrome/test/bookmark_load_observer.h" | 38 #include "chrome/test/bookmark_load_observer.h" |
35 #if defined(TOOLKIT_VIEWS) | 39 #if defined(TOOLKIT_VIEWS) |
36 #include "views/focus/accelerator_handler.h" | 40 #include "views/focus/accelerator_handler.h" |
37 #endif | 41 #endif |
| 42 #include "gfx/size.h" |
38 #include "googleurl/src/gurl.h" | 43 #include "googleurl/src/gurl.h" |
39 #include "net/base/net_util.h" | 44 #include "net/base/net_util.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
| 46 #include "third_party/skia/include/core/SkBitmap.h" |
| 47 #include "third_party/skia/include/core/SkColor.h" |
41 | 48 |
42 namespace ui_test_utils { | 49 namespace ui_test_utils { |
43 | 50 |
44 namespace { | 51 namespace { |
45 | 52 |
46 // Used to block until a navigation completes. | 53 // Used to block until a navigation completes. |
47 class NavigationNotificationObserver : public NotificationObserver { | 54 class NavigationNotificationObserver : public NotificationObserver { |
48 public: | 55 public: |
49 NavigationNotificationObserver(NavigationController* controller, | 56 NavigationNotificationObserver(NavigationController* controller, |
50 int number_of_navigations) | 57 int number_of_navigations) |
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 const NotificationDetails& details) { | 800 const NotificationDetails& details) { |
794 if (waiting_for_ == source) { | 801 if (waiting_for_ == source) { |
795 seen_ = true; | 802 seen_ = true; |
796 if (running_) | 803 if (running_) |
797 MessageLoopForUI::current()->Quit(); | 804 MessageLoopForUI::current()->Quit(); |
798 } else { | 805 } else { |
799 sources_seen_.insert(source.map_key()); | 806 sources_seen_.insert(source.map_key()); |
800 } | 807 } |
801 } | 808 } |
802 | 809 |
| 810 DOMMessageQueue::DOMMessageQueue() { |
| 811 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE, |
| 812 NotificationService::AllSources()); |
| 813 } |
| 814 |
| 815 void DOMMessageQueue::Observe(NotificationType type, |
| 816 const NotificationSource& source, |
| 817 const NotificationDetails& details) { |
| 818 Details<DomOperationNotificationDetails> dom_op_details(details); |
| 819 Source<RenderViewHost> sender(source); |
| 820 message_queue_.push(dom_op_details->json()); |
| 821 if (waiting_for_message_) { |
| 822 waiting_for_message_ = false; |
| 823 MessageLoopForUI::current()->Quit(); |
| 824 } |
| 825 } |
| 826 |
| 827 bool DOMMessageQueue::WaitForMessage(std::string* message) { |
| 828 if (message_queue_.empty()) { |
| 829 waiting_for_message_ = true; |
| 830 // This will be quit when a new message comes in. |
| 831 RunMessageLoop(); |
| 832 } |
| 833 // The queue should not be empty, unless we were quit because of a timeout. |
| 834 if (message_queue_.empty()) |
| 835 return false; |
| 836 if (message) |
| 837 *message = message_queue_.front(); |
| 838 return true; |
| 839 } |
| 840 |
| 841 // Coordinates taking snapshots of a |RenderWidget|. |
| 842 class SnapshotTaker { |
| 843 public: |
| 844 SnapshotTaker() : bitmap_(NULL) {} |
| 845 |
| 846 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, |
| 847 const gfx::Size& page_size, |
| 848 const gfx::Size& desired_size, |
| 849 SkBitmap* bitmap) WARN_UNUSED_RESULT { |
| 850 bitmap_ = bitmap; |
| 851 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator(); |
| 852 rwh->set_painting_observer(generator); |
| 853 snapshot_taken_ = false; |
| 854 generator->AskForSnapshot( |
| 855 rwh, |
| 856 false, // don't use backing_store |
| 857 NewCallback(this, &SnapshotTaker::OnSnapshotTaken), |
| 858 page_size, |
| 859 desired_size); |
| 860 ui_test_utils::RunMessageLoop(); |
| 861 return snapshot_taken_; |
| 862 } |
| 863 |
| 864 bool TakeEntirePageSnapshot(RenderViewHost* rvh, |
| 865 SkBitmap* bitmap) WARN_UNUSED_RESULT { |
| 866 const wchar_t* script = |
| 867 L"window.domAutomationController.send(" |
| 868 L" JSON.stringify([document.width, document.height]))"; |
| 869 std::string json; |
| 870 if (!ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 871 rvh, L"", script, &json)) |
| 872 return false; |
| 873 |
| 874 // Parse the JSON. |
| 875 std::vector<int> dimensions; |
| 876 scoped_ptr<Value> value(base::JSONReader::Read(json, true)); |
| 877 if (!value->IsType(Value::TYPE_LIST)) |
| 878 return false; |
| 879 ListValue* list = static_cast<ListValue*>(value.get()); |
| 880 int width, height; |
| 881 if (!list->GetInteger(0, &width) || !list->GetInteger(1, &height)) |
| 882 return false; |
| 883 |
| 884 // Take the snapshot. |
| 885 gfx::Size page_size(width, height); |
| 886 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap); |
| 887 } |
| 888 |
| 889 private: |
| 890 // Called when the ThumbnailGenerator has taken the snapshot. |
| 891 void OnSnapshotTaken(const SkBitmap& bitmap) { |
| 892 *bitmap_ = bitmap; |
| 893 snapshot_taken_ = true; |
| 894 MessageLoop::current()->Quit(); |
| 895 } |
| 896 |
| 897 SkBitmap* bitmap_; |
| 898 // Whether the snapshot was actually taken and received by this SnapshotTaker. |
| 899 // This will be false if the test times out. |
| 900 bool snapshot_taken_; |
| 901 |
| 902 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker); |
| 903 }; |
| 904 |
| 905 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, |
| 906 const gfx::Size& page_size, |
| 907 SkBitmap* bitmap) { |
| 908 DCHECK(bitmap); |
| 909 SnapshotTaker taker; |
| 910 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
| 911 } |
| 912 |
| 913 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
| 914 DCHECK(bitmap); |
| 915 SnapshotTaker taker; |
| 916 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
| 917 } |
| 918 |
803 } // namespace ui_test_utils | 919 } // namespace ui_test_utils |
OLD | NEW |