| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "chrome/browser/extensions/extension_browsertest.h" | 6 #include "chrome/browser/extensions/extension_browsertest.h" |
| 7 #include "chrome/browser/extensions/extension_host.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/extensions/extension_test_message_listener.h" | 9 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_list.h" | 13 #include "chrome/browser/ui/browser_list.h" |
| 13 #include "chrome/browser/web_applications/web_app.h" | 14 #include "chrome/browser/web_applications/web_app.h" |
| 14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 38 | 39 |
| 39 } // namespace | 40 } // namespace |
| 40 | 41 |
| 41 class PlatformAppBrowserTest : public ExtensionBrowserTest { | 42 class PlatformAppBrowserTest : public ExtensionBrowserTest { |
| 42 public: | 43 public: |
| 43 virtual void SetUpCommandLine(CommandLine* command_line) { | 44 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 44 ExtensionBrowserTest::SetUpCommandLine(command_line); | 45 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 45 command_line->AppendSwitch(switches::kEnablePlatformApps); | 46 command_line->AppendSwitch(switches::kEnablePlatformApps); |
| 46 } | 47 } |
| 47 | 48 |
| 49 protected: |
| 48 void LoadAndLaunchPlatformApp(const char* name) { | 50 void LoadAndLaunchPlatformApp(const char* name) { |
| 49 web_app::SetDisableShortcutCreationForTests(true); | 51 web_app::SetDisableShortcutCreationForTests(true); |
| 50 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). | 52 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |
| 51 AppendASCII(name))); | 53 AppendASCII(name))); |
| 52 | 54 |
| 53 ExtensionService* service = browser()->profile()->GetExtensionService(); | 55 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 54 const Extension* extension = service->GetExtensionById( | 56 const Extension* extension = service->GetExtensionById( |
| 55 last_loaded_extension_id_, false); | 57 last_loaded_extension_id_, false); |
| 56 EXPECT_TRUE(extension); | 58 EXPECT_TRUE(extension); |
| 57 | 59 |
| 58 size_t browser_count = BrowserList::size(); | 60 size_t platform_app_count = GetPlatformAppCount(); |
| 59 | 61 |
| 60 Browser::OpenApplication( | 62 Browser::OpenApplication( |
| 61 browser()->profile(), | 63 browser()->profile(), |
| 62 extension, | 64 extension, |
| 63 extension_misc::LAUNCH_SHELL, | 65 extension_misc::LAUNCH_SHELL, |
| 64 GURL(), | 66 GURL(), |
| 65 NEW_WINDOW); | 67 NEW_WINDOW); |
| 66 | 68 |
| 67 // Now we have a new browser instance. | 69 // Now we have a new platform app running. |
| 68 EXPECT_EQ(browser_count + 1, BrowserList::size()); | 70 EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount()); |
| 71 } |
| 72 |
| 73 // Gets the number of platform apps that are running. |
| 74 size_t GetPlatformAppCount() { |
| 75 int count = 0; |
| 76 ExtensionProcessManager* process_manager = |
| 77 browser()->profile()->GetExtensionProcessManager(); |
| 78 ExtensionProcessManager::const_iterator iter; |
| 79 for (iter = process_manager->begin(); iter != process_manager->end(); |
| 80 ++iter) { |
| 81 ExtensionHost* host = *iter; |
| 82 if (host->extension() && host->extension()->is_platform_app()) |
| 83 count++; |
| 84 } |
| 85 |
| 86 return count; |
| 87 } |
| 88 |
| 89 // Gets the TabContents associated with the ExtensionHost of the first |
| 90 // platform app that is found (most tests only deal with one platform |
| 91 // app, so this is good enough). |
| 92 TabContents* GetFirstPlatformAppTabContents() { |
| 93 ExtensionProcessManager* process_manager = |
| 94 browser()->profile()->GetExtensionProcessManager(); |
| 95 ExtensionProcessManager::const_iterator iter; |
| 96 for (iter = process_manager->begin(); iter != process_manager->end(); |
| 97 ++iter) { |
| 98 ExtensionHost* host = *iter; |
| 99 if (host->extension() && host->extension()->is_platform_app()) |
| 100 return host->host_contents(); |
| 101 } |
| 102 |
| 103 return NULL; |
| 69 } | 104 } |
| 70 }; | 105 }; |
| 71 | 106 |
| 72 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenAppInShellContainer) { | 107 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenAppInShellContainer) { |
| 73 // Start with one browser, new platform app will create another. | 108 ASSERT_EQ(0u, GetPlatformAppCount()); |
| 74 ASSERT_EQ(1u, BrowserList::size()); | |
| 75 | |
| 76 LoadAndLaunchPlatformApp("empty"); | 109 LoadAndLaunchPlatformApp("empty"); |
| 77 | 110 ASSERT_EQ(1u, GetPlatformAppCount()); |
| 78 // The launch should have created a new browser, so there should be 2 now. | |
| 79 ASSERT_EQ(2u, BrowserList::size()); | |
| 80 | |
| 81 // The new browser is the last one. | |
| 82 BrowserList::const_reverse_iterator reverse_iterator(BrowserList::end()); | |
| 83 Browser* new_browser = *(reverse_iterator++); | |
| 84 | |
| 85 ASSERT_TRUE(new_browser); | |
| 86 ASSERT_TRUE(new_browser != browser()); | |
| 87 | |
| 88 // Expect an app in a shell window. | |
| 89 EXPECT_TRUE(new_browser->is_app()); | |
| 90 EXPECT_TRUE(new_browser->is_type_shell()); | |
| 91 } | 111 } |
| 92 | 112 |
| 93 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { | 113 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
| 94 // Start with one browser, new platform app will create another. | |
| 95 ASSERT_EQ(1u, BrowserList::size()); | |
| 96 | |
| 97 LoadAndLaunchPlatformApp("empty"); | 114 LoadAndLaunchPlatformApp("empty"); |
| 98 | 115 |
| 99 // The launch should have created a new browser, so there should be 2 now. | |
| 100 ASSERT_EQ(2u, BrowserList::size()); | |
| 101 | |
| 102 // The new browser is the last one. | |
| 103 BrowserList::const_reverse_iterator reverse_iterator(BrowserList::end()); | |
| 104 Browser* new_browser = *(reverse_iterator++); | |
| 105 | |
| 106 ASSERT_TRUE(new_browser); | |
| 107 ASSERT_TRUE(new_browser != browser()); | |
| 108 | |
| 109 // The empty app doesn't add any context menu items, so its menu should | 116 // The empty app doesn't add any context menu items, so its menu should |
| 110 // be empty. | 117 // be empty. |
| 111 TabContents* tab_contents = new_browser->GetSelectedTabContents(); | 118 TabContents* tab_contents = GetFirstPlatformAppTabContents(); |
| 119 ASSERT_TRUE(tab_contents); |
| 112 WebKit::WebContextMenuData data; | 120 WebKit::WebContextMenuData data; |
| 113 ContextMenuParams params(data); | 121 ContextMenuParams params(data); |
| 114 PlatformAppContextMenu* menu = new PlatformAppContextMenu(tab_contents, | 122 PlatformAppContextMenu* menu = new PlatformAppContextMenu(tab_contents, |
| 115 params); | 123 params); |
| 116 menu->Init(); | 124 menu->Init(); |
| 117 ASSERT_FALSE(menu->menu_model().GetItemCount()); | 125 ASSERT_FALSE(menu->menu_model().GetItemCount()); |
| 118 } | 126 } |
| 119 | 127 |
| 120 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { | 128 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
| 121 // Start with one browser, new platform app will create another. | |
| 122 ASSERT_EQ(1u, BrowserList::size()); | |
| 123 | |
| 124 ExtensionTestMessageListener listener1("created item", false); | 129 ExtensionTestMessageListener listener1("created item", false); |
| 125 LoadAndLaunchPlatformApp("context_menu"); | 130 LoadAndLaunchPlatformApp("context_menu"); |
| 126 | 131 |
| 127 // Wait for the extension to tell us it's created an item. | 132 // Wait for the extension to tell us it's created an item. |
| 128 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 133 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
| 129 | 134 |
| 130 // The launch should have created a new browser, so there should be 2 now. | |
| 131 ASSERT_EQ(2u, BrowserList::size()); | |
| 132 | |
| 133 // The new browser is the last one. | |
| 134 BrowserList::const_reverse_iterator reverse_iterator(BrowserList::end()); | |
| 135 Browser* new_browser = *(reverse_iterator++); | |
| 136 | |
| 137 ASSERT_TRUE(new_browser); | |
| 138 ASSERT_TRUE(new_browser != browser()); | |
| 139 | |
| 140 // The context_menu app has one context menu item. This is all that should | 135 // The context_menu app has one context menu item. This is all that should |
| 141 // be in the menu, there should be no seperator. | 136 // be in the menu, there should be no seperator. |
| 142 TabContents* tab_contents = new_browser->GetSelectedTabContents(); | 137 TabContents* tab_contents = GetFirstPlatformAppTabContents(); |
| 138 ASSERT_TRUE(tab_contents); |
| 143 WebKit::WebContextMenuData data; | 139 WebKit::WebContextMenuData data; |
| 144 ContextMenuParams params(data); | 140 ContextMenuParams params(data); |
| 145 PlatformAppContextMenu* menu = new PlatformAppContextMenu(tab_contents, | 141 PlatformAppContextMenu* menu = new PlatformAppContextMenu(tab_contents, |
| 146 params); | 142 params); |
| 147 menu->Init(); | 143 menu->Init(); |
| 148 ASSERT_EQ(1, menu->menu_model().GetItemCount()); | 144 ASSERT_EQ(1, menu->menu_model().GetItemCount()); |
| 149 } | 145 } |
| OLD | NEW |