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 "chrome/browser/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
6 #include "chrome/browser/extensions/browser_action_test_util.h" | 6 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 7 #include "chrome/browser/sessions/restore_tab_helper.h" |
7 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
8 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_action.h" |
9 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
10 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
11 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
12 | 16 |
13 using content::WebContents; | 17 using content::WebContents; |
14 | 18 |
15 class KeybindingApiTest : public ExtensionApiTest { | 19 class KeybindingApiTest : public ExtensionApiTest { |
16 public: | 20 public: |
17 KeybindingApiTest() { | 21 KeybindingApiTest() { |
18 CommandLine::ForCurrentProcess()->AppendSwitch( | 22 CommandLine::ForCurrentProcess()->AppendSwitch( |
19 switches::kEnableExperimentalExtensionApis); | 23 switches::kEnableExperimentalExtensionApis); |
20 } | 24 } |
21 virtual ~KeybindingApiTest() {} | 25 virtual ~KeybindingApiTest() {} |
22 | 26 |
23 protected: | 27 protected: |
24 BrowserActionTestUtil GetBrowserActionsBar() { | 28 BrowserActionTestUtil GetBrowserActionsBar() { |
25 return BrowserActionTestUtil(browser()); | 29 return BrowserActionTestUtil(browser()); |
26 } | 30 } |
27 | |
28 bool OpenPopup(int index) { | |
29 ResultCatcher catcher; | |
30 ui_test_utils::WindowedNotificationObserver popup_observer( | |
31 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | |
32 content::NotificationService::AllSources()); | |
33 GetBrowserActionsBar().Press(index); | |
34 popup_observer.Wait(); | |
35 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
36 return GetBrowserActionsBar().HasPopup(); | |
37 } | |
38 }; | 31 }; |
39 | 32 |
40 #if !defined(OS_MACOSX) | 33 #if !defined(OS_MACOSX) |
41 IN_PROC_BROWSER_TEST_F(KeybindingApiTest, Basic) { | 34 IN_PROC_BROWSER_TEST_F(KeybindingApiTest, Basic) { |
42 ASSERT_TRUE(test_server()->Start()); | 35 ASSERT_TRUE(test_server()->Start()); |
43 ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_; | 36 ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_; |
44 const Extension* extension = GetSingleLoadedExtension(); | 37 const Extension* extension = GetSingleLoadedExtension(); |
45 ASSERT_TRUE(extension) << message_; | 38 ASSERT_TRUE(extension) << message_; |
46 | 39 |
47 // Test that there is a browser action in the toolbar. | 40 // Test that there is a browser action in the toolbar. |
48 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); | 41 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); |
49 | 42 |
50 ui_test_utils::NavigateToURL(browser(), | 43 ui_test_utils::NavigateToURL(browser(), |
51 test_server()->GetURL("files/extensions/test_file.txt")); | 44 test_server()->GetURL("files/extensions/test_file.txt")); |
52 | 45 |
53 // Simulate the browser action being clicked (Ctrl+Shift+F). | 46 // Activate the shortcut (Ctrl+Shift+F). |
54 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 47 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
55 browser(), ui::VKEY_F, true, true, false, false)); | 48 browser(), ui::VKEY_F, true, true, false, false)); |
56 | 49 |
57 // Verify the command worked. | 50 // Verify the command worked. |
58 WebContents* tab = browser()->GetSelectedWebContents(); | 51 WebContents* tab = browser()->GetSelectedWebContents(); |
59 bool result = false; | 52 bool result = false; |
60 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 53 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
61 tab->GetRenderViewHost(), L"", | 54 tab->GetRenderViewHost(), L"", |
62 L"setInterval(function(){" | 55 L"setInterval(function(){" |
63 L" if(document.body.bgColor == 'red'){" | 56 L" if(document.body.bgColor == 'red'){" |
64 L" window.domAutomationController.send(true)}}, 100)", | 57 L" window.domAutomationController.send(true)}}, 100)", |
65 &result)); | 58 &result)); |
66 ASSERT_TRUE(result); | 59 ASSERT_TRUE(result); |
67 | 60 |
68 // Simulate the event being sent (Ctrl+Shift+Y). | 61 // Activate the shortcut (Ctrl+Shift+Y). |
69 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 62 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
70 browser(), ui::VKEY_Y, true, true, false, false)); | 63 browser(), ui::VKEY_Y, true, true, false, false)); |
71 | 64 |
72 result = false; | 65 result = false; |
73 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 66 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
74 tab->GetRenderViewHost(), L"", | 67 tab->GetRenderViewHost(), L"", |
75 L"setInterval(function(){" | 68 L"setInterval(function(){" |
76 L" if(document.body.bgColor == 'blue'){" | 69 L" if(document.body.bgColor == 'blue'){" |
77 L" window.domAutomationController.send(true)}}, 100)", | 70 L" window.domAutomationController.send(true)}}, 100)", |
78 &result)); | 71 &result)); |
79 ASSERT_TRUE(result); | 72 ASSERT_TRUE(result); |
80 } | 73 } |
81 #endif // !OS_MACOSX | 74 #endif // !OS_MACOSX |
| 75 |
| 76 #if defined(TOOLKIT_VIEWS) |
| 77 IN_PROC_BROWSER_TEST_F(KeybindingApiTest, PageAction) { |
| 78 ASSERT_TRUE(test_server()->Start()); |
| 79 ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_; |
| 80 const Extension* extension = GetSingleLoadedExtension(); |
| 81 ASSERT_TRUE(extension) << message_; |
| 82 |
| 83 { |
| 84 // Load a page, the extension will detect the navigation and request to show |
| 85 // the page action icon. |
| 86 ResultCatcher catcher; |
| 87 ui_test_utils::NavigateToURL(browser(), |
| 88 test_server()->GetURL("files/extensions/test_file.txt")); |
| 89 ASSERT_TRUE(catcher.GetNextResult()); |
| 90 } |
| 91 |
| 92 // Make sure it appears and is the right one. |
| 93 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1)); |
| 94 int tab_id = browser()->GetSelectedTabContentsWrapper()-> |
| 95 restore_tab_helper()->session_id().id(); |
| 96 ExtensionAction* action = extension->page_action(); |
| 97 ASSERT_TRUE(action); |
| 98 EXPECT_EQ("Make this page red", action->GetTitle(tab_id)); |
| 99 |
| 100 // Activate the shortcut (Ctrl+Shift+F). |
| 101 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 102 browser(), ui::VKEY_F, true, true, false, false)); |
| 103 |
| 104 // Verify the command worked (the page action turns the page red). |
| 105 WebContents* tab = browser()->GetSelectedWebContents(); |
| 106 bool result = false; |
| 107 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 108 tab->GetRenderViewHost(), L"", |
| 109 L"setInterval(function(){" |
| 110 L" if(document.body.bgColor == 'red'){" |
| 111 L" window.domAutomationController.send(true)}}, 100)", |
| 112 &result)); |
| 113 ASSERT_TRUE(result); |
| 114 } |
| 115 |
| 116 #endif // TOOLKIT_VIEWS |
OLD | NEW |