OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <limits.h> | 5 #include <limits.h> |
6 #include <stddef.h> | 6 #include <stddef.h> |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
| 12 #include "apps/test/app_window_waiter.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/strings/pattern.h" | 14 #include "base/strings/pattern.h" |
14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "chrome/browser/apps/app_browsertest_util.h" | 20 #include "chrome/browser/apps/app_browsertest_util.h" |
20 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/devtools/devtools_window_testing.h" | 22 #include "chrome/browser/devtools/devtools_window_testing.h" |
(...skipping 16 matching lines...) Expand all Loading... |
38 #include "components/prefs/pref_service.h" | 39 #include "components/prefs/pref_service.h" |
39 #include "content/public/browser/browser_context.h" | 40 #include "content/public/browser/browser_context.h" |
40 #include "content/public/browser/notification_service.h" | 41 #include "content/public/browser/notification_service.h" |
41 #include "content/public/browser/storage_partition.h" | 42 #include "content/public/browser/storage_partition.h" |
42 #include "content/public/common/page_zoom.h" | 43 #include "content/public/common/page_zoom.h" |
43 #include "content/public/common/url_constants.h" | 44 #include "content/public/common/url_constants.h" |
44 #include "content/public/test/browser_test_utils.h" | 45 #include "content/public/test/browser_test_utils.h" |
45 #include "extensions/browser/api_test_utils.h" | 46 #include "extensions/browser/api_test_utils.h" |
46 #include "extensions/browser/app_window/app_window.h" | 47 #include "extensions/browser/app_window/app_window.h" |
47 #include "extensions/browser/app_window/app_window_registry.h" | 48 #include "extensions/browser/app_window/app_window_registry.h" |
48 #include "extensions/browser/app_window/native_app_window.h" | |
49 #include "extensions/common/manifest_constants.h" | 49 #include "extensions/common/manifest_constants.h" |
50 #include "extensions/common/test_util.h" | 50 #include "extensions/common/test_util.h" |
51 #include "extensions/test/extension_test_message_listener.h" | 51 #include "extensions/test/extension_test_message_listener.h" |
52 #include "extensions/test/result_catcher.h" | 52 #include "extensions/test/result_catcher.h" |
53 #include "net/test/embedded_test_server/embedded_test_server.h" | 53 #include "net/test/embedded_test_server/embedded_test_server.h" |
54 #include "ui/base/window_open_disposition.h" | 54 #include "ui/base/window_open_disposition.h" |
55 #include "ui/gfx/geometry/rect.h" | 55 #include "ui/gfx/geometry/rect.h" |
56 #include "ui/views/widget/widget.h" | 56 #include "ui/views/widget/widget.h" |
57 #include "ui/views/widget/widget_observer.h" | 57 #include "ui/views/widget/widget_observer.h" |
58 | 58 |
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 | 791 |
792 EXPECT_EQ(new_width, api_test_utils::GetInteger(result.get(), "width")); | 792 EXPECT_EQ(new_width, api_test_utils::GetInteger(result.get(), "width")); |
793 EXPECT_EQ(new_height, api_test_utils::GetInteger(result.get(), "height")); | 793 EXPECT_EQ(new_height, api_test_utils::GetInteger(result.get(), "height")); |
794 | 794 |
795 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools); | 795 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools); |
796 } | 796 } |
797 | 797 |
798 // TODO(llandwerlin): Activating a browser window and waiting for the | 798 // TODO(llandwerlin): Activating a browser window and waiting for the |
799 // action to happen requires views::Widget which is not available on | 799 // action to happen requires views::Widget which is not available on |
800 // MacOSX. Deactivate for now. | 800 // MacOSX. Deactivate for now. |
| 801 // TODO(warx): Move ExtensionWindowLastFocusedTest to interactive |
| 802 // uitest as it triggers native widget activation. |
801 #if !defined(OS_MACOSX) | 803 #if !defined(OS_MACOSX) |
802 class ExtensionWindowLastFocusedTest : public ExtensionTabsTest { | 804 class ExtensionWindowLastFocusedTest : public ExtensionTabsTest { |
803 public: | 805 public: |
804 void SetUpOnMainThread() override; | 806 void SetUpOnMainThread() override; |
805 | 807 |
806 void ActivateAppWindow(AppWindow* app_window); | |
807 | |
808 void ActivateBrowserWindow(Browser* browser); | 808 void ActivateBrowserWindow(Browser* browser); |
809 | 809 |
810 Browser* CreateBrowserWithEmptyTab(bool as_popup); | 810 Browser* CreateBrowserWithEmptyTab(bool as_popup); |
811 | 811 |
812 int GetTabId(const base::DictionaryValue* value) const; | 812 int GetTabId(const base::DictionaryValue* value) const; |
813 | 813 |
814 base::Value* RunFunction(UIThreadExtensionFunction* function, | 814 base::Value* RunFunction(UIThreadExtensionFunction* function, |
815 const std::string& params); | 815 const std::string& params); |
816 | 816 |
817 private: | 817 private: |
818 // A helper class to wait for an AppWindow to become activated. On | |
819 // window system like X11, for a NativeWidget to be activated, we | |
820 // need to wait for the round trip communication with the X server. | |
821 class AppWindowActivatedWaiter : public AppWindowRegistry::Observer { | |
822 public: | |
823 AppWindowActivatedWaiter(AppWindow* app_window, | |
824 content::BrowserContext* browser_context) | |
825 : app_window_(app_window), | |
826 browser_context_(browser_context), | |
827 waiting_(false) { | |
828 AppWindowRegistry::Get(browser_context_)->AddObserver(this); | |
829 } | |
830 ~AppWindowActivatedWaiter() override { | |
831 AppWindowRegistry::Get(browser_context_)->RemoveObserver(this); | |
832 } | |
833 | |
834 void ActivateAndWait() { | |
835 app_window_->GetBaseWindow()->Activate(); | |
836 if (!app_window_->GetBaseWindow()->IsActive()) { | |
837 waiting_ = true; | |
838 content::RunMessageLoop(); | |
839 } | |
840 } | |
841 | |
842 // AppWindowRegistry::Observer: | |
843 void OnAppWindowActivated(AppWindow* app_window) override { | |
844 if (app_window_ == app_window && waiting_) { | |
845 base::MessageLoopForUI::current()->QuitWhenIdle(); | |
846 waiting_ = false; | |
847 } | |
848 } | |
849 | |
850 private: | |
851 AppWindow* app_window_; | |
852 content::BrowserContext* browser_context_; | |
853 bool waiting_; | |
854 }; | |
855 | |
856 // A helper class to wait for an views::Widget to become activated. | 818 // A helper class to wait for an views::Widget to become activated. |
857 class WidgetActivatedWaiter : public views::WidgetObserver { | 819 class WidgetActivatedWaiter : public views::WidgetObserver { |
858 public: | 820 public: |
859 explicit WidgetActivatedWaiter(views::Widget* widget) | 821 explicit WidgetActivatedWaiter(views::Widget* widget) |
860 : widget_(widget), waiting_(false) { | 822 : widget_(widget), waiting_(false) { |
861 widget_->AddObserver(this); | 823 widget_->AddObserver(this); |
862 } | 824 } |
863 ~WidgetActivatedWaiter() override { widget_->RemoveObserver(this); } | 825 ~WidgetActivatedWaiter() override { widget_->RemoveObserver(this); } |
864 | 826 |
865 void ActivateAndWait() { | 827 void ActivateAndWait() { |
(...skipping 19 matching lines...) Expand all Loading... |
885 }; | 847 }; |
886 | 848 |
887 scoped_refptr<Extension> extension_; | 849 scoped_refptr<Extension> extension_; |
888 }; | 850 }; |
889 | 851 |
890 void ExtensionWindowLastFocusedTest::SetUpOnMainThread() { | 852 void ExtensionWindowLastFocusedTest::SetUpOnMainThread() { |
891 ExtensionTabsTest::SetUpOnMainThread(); | 853 ExtensionTabsTest::SetUpOnMainThread(); |
892 extension_ = test_util::CreateEmptyExtension(); | 854 extension_ = test_util::CreateEmptyExtension(); |
893 } | 855 } |
894 | 856 |
895 void ExtensionWindowLastFocusedTest::ActivateAppWindow(AppWindow* app_window) { | |
896 AppWindowActivatedWaiter waiter(app_window, browser()->profile()); | |
897 waiter.ActivateAndWait(); | |
898 } | |
899 | |
900 void ExtensionWindowLastFocusedTest::ActivateBrowserWindow(Browser* browser) { | 857 void ExtensionWindowLastFocusedTest::ActivateBrowserWindow(Browser* browser) { |
901 BrowserView* view = BrowserView::GetBrowserViewForBrowser(browser); | 858 BrowserView* view = BrowserView::GetBrowserViewForBrowser(browser); |
902 EXPECT_NE(nullptr, view); | 859 EXPECT_NE(nullptr, view); |
903 views::Widget* widget = view->frame(); | 860 views::Widget* widget = view->frame(); |
904 EXPECT_NE(nullptr, widget); | 861 EXPECT_NE(nullptr, widget); |
905 WidgetActivatedWaiter waiter(widget); | 862 WidgetActivatedWaiter waiter(widget); |
906 waiter.ActivateAndWait(); | 863 waiter.ActivateAndWait(); |
907 } | 864 } |
908 | 865 |
909 Browser* ExtensionWindowLastFocusedTest::CreateBrowserWithEmptyTab( | 866 Browser* ExtensionWindowLastFocusedTest::CreateBrowserWithEmptyTab( |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 EXPECT_NE(devtools_window_id, | 943 EXPECT_NE(devtools_window_id, |
987 api_test_utils::GetInteger(result.get(), "id")); | 944 api_test_utils::GetInteger(result.get(), "id")); |
988 } | 945 } |
989 | 946 |
990 AppWindow* app_window = CreateTestAppWindow( | 947 AppWindow* app_window = CreateTestAppWindow( |
991 "{\"outerBounds\": " | 948 "{\"outerBounds\": " |
992 "{\"width\": 300, \"height\": 300," | 949 "{\"width\": 300, \"height\": 300," |
993 " \"minWidth\": 200, \"minHeight\": 200," | 950 " \"minWidth\": 200, \"minHeight\": 200," |
994 " \"maxWidth\": 400, \"maxHeight\": 400}}"); | 951 " \"maxWidth\": 400, \"maxHeight\": 400}}"); |
995 { | 952 { |
996 ActivateAppWindow(app_window); | 953 apps::AppWindowWaiter waiter(AppWindowRegistry::Get(browser()->profile()), |
| 954 app_window->extension_id()); |
| 955 waiter.WaitForActivated(); |
997 | 956 |
998 scoped_refptr<WindowsGetLastFocusedFunction> get_current_app_function = | 957 scoped_refptr<WindowsGetLastFocusedFunction> get_current_app_function = |
999 new WindowsGetLastFocusedFunction(); | 958 new WindowsGetLastFocusedFunction(); |
1000 std::unique_ptr<base::DictionaryValue> result(utils::ToDictionary( | 959 std::unique_ptr<base::DictionaryValue> result(utils::ToDictionary( |
1001 RunFunction(get_current_app_function.get(), "[{\"populate\": true}]"))); | 960 RunFunction(get_current_app_function.get(), "[{\"populate\": true}]"))); |
1002 int app_window_id = app_window->session_id().id(); | 961 int app_window_id = app_window->session_id().id(); |
1003 EXPECT_NE(app_window_id, api_test_utils::GetInteger(result.get(), "id")); | 962 EXPECT_NE(app_window_id, api_test_utils::GetInteger(result.get(), "id")); |
1004 } | 963 } |
1005 | 964 |
1006 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools); | 965 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 EXPECT_EQ(-1, GetTabId(result.get())); | 1017 EXPECT_EQ(-1, GetTabId(result.get())); |
1059 EXPECT_EQ("devtools", api_test_utils::GetString(result.get(), "type")); | 1018 EXPECT_EQ("devtools", api_test_utils::GetString(result.get(), "type")); |
1060 } | 1019 } |
1061 | 1020 |
1062 AppWindow* app_window = CreateTestAppWindow( | 1021 AppWindow* app_window = CreateTestAppWindow( |
1063 "{\"outerBounds\": " | 1022 "{\"outerBounds\": " |
1064 "{\"width\": 300, \"height\": 300," | 1023 "{\"width\": 300, \"height\": 300," |
1065 " \"minWidth\": 200, \"minHeight\": 200," | 1024 " \"minWidth\": 200, \"minHeight\": 200," |
1066 " \"maxWidth\": 400, \"maxHeight\": 400}}"); | 1025 " \"maxWidth\": 400, \"maxHeight\": 400}}"); |
1067 { | 1026 { |
1068 ActivateAppWindow(app_window); | 1027 apps::AppWindowWaiter waiter(AppWindowRegistry::Get(browser()->profile()), |
| 1028 app_window->extension_id()); |
| 1029 waiter.WaitForActivated(); |
1069 | 1030 |
1070 scoped_refptr<WindowsGetLastFocusedFunction> get_current_app_function = | 1031 scoped_refptr<WindowsGetLastFocusedFunction> get_current_app_function = |
1071 new WindowsGetLastFocusedFunction(); | 1032 new WindowsGetLastFocusedFunction(); |
1072 std::unique_ptr<base::DictionaryValue> result(utils::ToDictionary( | 1033 std::unique_ptr<base::DictionaryValue> result(utils::ToDictionary( |
1073 RunFunction(get_current_app_function.get(), | 1034 RunFunction(get_current_app_function.get(), |
1074 "[{\"populate\": true, \"windowTypes\": [ \"app\" ]}]"))); | 1035 "[{\"populate\": true, \"windowTypes\": [ \"app\" ]}]"))); |
1075 int app_window_id = app_window->session_id().id(); | 1036 int app_window_id = app_window->session_id().id(); |
1076 EXPECT_EQ(app_window_id, api_test_utils::GetInteger(result.get(), "id")); | 1037 EXPECT_EQ(app_window_id, api_test_utils::GetInteger(result.get(), "id")); |
1077 EXPECT_EQ(-1, GetTabId(result.get())); | 1038 EXPECT_EQ(-1, GetTabId(result.get())); |
1078 EXPECT_EQ("app", api_test_utils::GetString(result.get(), "type")); | 1039 EXPECT_EQ("app", api_test_utils::GetString(result.get(), "type")); |
(...skipping 1090 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2169 EXPECT_EQ(first_web_contents, | 2130 EXPECT_EQ(first_web_contents, |
2170 browser()->tab_strip_model()->GetActiveWebContents()); | 2131 browser()->tab_strip_model()->GetActiveWebContents()); |
2171 browser()->tab_strip_model()->ActivateTabAt(1, true); | 2132 browser()->tab_strip_model()->ActivateTabAt(1, true); |
2172 EXPECT_EQ(second_web_contents, | 2133 EXPECT_EQ(second_web_contents, |
2173 browser()->tab_strip_model()->GetActiveWebContents()); | 2134 browser()->tab_strip_model()->GetActiveWebContents()); |
2174 | 2135 |
2175 EXPECT_EQ(url, second_web_contents->GetVisibleURL()); | 2136 EXPECT_EQ(url, second_web_contents->GetVisibleURL()); |
2176 } | 2137 } |
2177 | 2138 |
2178 } // namespace extensions | 2139 } // namespace extensions |
OLD | NEW |