| 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 "base/command_line.h" |    5 #include "base/command_line.h" | 
|    6 #include "base/stringprintf.h" |    6 #include "base/stringprintf.h" | 
|    7 #include "base/utf_string_conversions.h" |    7 #include "base/utf_string_conversions.h" | 
|    8 #include "chrome/browser/extensions/extension_apitest.h" |    8 #include "chrome/browser/extensions/extension_apitest.h" | 
|    9 #include "chrome/browser/extensions/extension_browsertest.h" |    9 #include "chrome/browser/extensions/extension_browsertest.h" | 
|   10 #include "chrome/browser/extensions/extension_host.h" |  | 
|   11 #include "chrome/browser/extensions/extension_service.h" |   10 #include "chrome/browser/extensions/extension_service.h" | 
|   12 #include "chrome/browser/extensions/extension_test_message_listener.h" |   11 #include "chrome/browser/extensions/extension_test_message_listener.h" | 
 |   12 #include "chrome/browser/extensions/platform_app_registry.h" | 
|   13 #include "chrome/browser/profiles/profile.h" |   13 #include "chrome/browser/profiles/profile.h" | 
|   14 #include "chrome/browser/tab_contents/render_view_context_menu.h" |   14 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 
|   15 #include "chrome/browser/ui/browser.h" |   15 #include "chrome/browser/ui/browser.h" | 
|   16 #include "chrome/browser/ui/browser_list.h" |   16 #include "chrome/browser/ui/browser_list.h" | 
 |   17 #include "chrome/browser/ui/extensions/shell_window.h" | 
|   17 #include "chrome/common/chrome_switches.h" |   18 #include "chrome/common/chrome_switches.h" | 
|   18 #include "chrome/common/chrome_view_type.h" |  | 
|   19 #include "chrome/common/extensions/extension_constants.h" |   19 #include "chrome/common/extensions/extension_constants.h" | 
|   20 #include "chrome/test/base/ui_test_utils.h" |   20 #include "chrome/test/base/ui_test_utils.h" | 
|   21 #include "content/public/browser/web_contents.h" |   21 #include "content/public/browser/web_contents.h" | 
|   22 #include "content/public/common/context_menu_params.h" |   22 #include "content/public/common/context_menu_params.h" | 
|   23 #include "testing/gtest/include/gtest/gtest.h" |   23 #include "testing/gtest/include/gtest/gtest.h" | 
|   24 #include "ui/base/models/menu_model.h" |   24 #include "ui/base/models/menu_model.h" | 
|   25  |   25  | 
|   26 using content::WebContents; |   26 using content::WebContents; | 
|   27  |   27  | 
|   28 namespace { |   28 namespace { | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|   48 class PlatformAppBrowserTest : public ExtensionApiTest { |   48 class PlatformAppBrowserTest : public ExtensionApiTest { | 
|   49  public: |   49  public: | 
|   50   virtual void SetUpCommandLine(CommandLine* command_line) { |   50   virtual void SetUpCommandLine(CommandLine* command_line) { | 
|   51     ExtensionBrowserTest::SetUpCommandLine(command_line); |   51     ExtensionBrowserTest::SetUpCommandLine(command_line); | 
|   52     command_line->AppendSwitch(switches::kEnablePlatformApps); |   52     command_line->AppendSwitch(switches::kEnablePlatformApps); | 
|   53     command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |   53     command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 
|   54   } |   54   } | 
|   55  |   55  | 
|   56  protected: |   56  protected: | 
|   57   void LoadAndLaunchPlatformApp(const char* name) { |   57   void LoadAndLaunchPlatformApp(const char* name) { | 
|   58     size_t platform_app_count = GetPlatformAppCount(); |  | 
|   59     ui_test_utils::WindowedNotificationObserver app_loaded_observer( |   58     ui_test_utils::WindowedNotificationObserver app_loaded_observer( | 
|   60         content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |   59         content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 
|   61         content::NotificationService::AllSources()); |   60         content::NotificationService::AllSources()); | 
|   62  |   61  | 
|   63     EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |   62     EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). | 
|   64         AppendASCII(name))); |   63         AppendASCII(name))); | 
|   65  |   64  | 
|   66     ExtensionService* service = browser()->profile()->GetExtensionService(); |   65     ExtensionService* service = browser()->profile()->GetExtensionService(); | 
|   67     const Extension* extension = service->GetExtensionById( |   66     const Extension* extension = service->GetExtensionById( | 
|   68         last_loaded_extension_id_, false); |   67         last_loaded_extension_id_, false); | 
|   69     EXPECT_TRUE(extension); |   68     EXPECT_TRUE(extension); | 
|   70  |   69  | 
|   71     Browser::OpenApplication( |   70     Browser::OpenApplication( | 
|   72         browser()->profile(), |   71         browser()->profile(), | 
|   73         extension, |   72         extension, | 
|   74         extension_misc::LAUNCH_NONE, |   73         extension_misc::LAUNCH_NONE, | 
|   75         GURL(), |   74         GURL(), | 
|   76         NEW_WINDOW); |   75         NEW_WINDOW); | 
|   77  |   76  | 
|   78     app_loaded_observer.Wait(); |   77     app_loaded_observer.Wait(); | 
|   79  |  | 
|   80     // Now we have a new platform app running. |  | 
|   81     EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount()); |  | 
|   82   } |   78   } | 
|   83  |   79  | 
|   84   // Gets the number of platform apps extension hosts that are running. |   80   // Gets the WebContents associated with the first shell window that is found | 
|   85   size_t GetPlatformAppCount() { |   81   // (most tests only deal with one platform app window, so this is good | 
|   86     int count = 0; |   82   // enough). | 
|   87     ExtensionProcessManager* process_manager = |   83   WebContents* GetFirstShellWindowWebContents() { | 
|   88         browser()->profile()->GetExtensionProcessManager(); |   84     PlatformAppRegistry* app_registry = | 
|   89     ExtensionProcessManager::const_iterator iter; |   85         PlatformAppRegistry::Get(browser()->profile()); | 
|   90     ExtensionProcessManager::ExtensionHostSet platform_app_hosts = |   86     PlatformAppRegistry::const_iterator iter; | 
|   91         process_manager->platform_app_hosts(); |   87     PlatformAppRegistry::ShellWindowSet shell_windows = | 
|   92     for (iter = platform_app_hosts.begin(); iter != platform_app_hosts.end(); |   88         app_registry->shell_windows(); | 
|   93          ++iter) { |   89     for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) { | 
|   94       if ((*iter)->extension()) |   90       return (*iter)->web_contents(); | 
|   95         count++; |  | 
|   96     } |  | 
|   97  |  | 
|   98     return count; |  | 
|   99   } |  | 
|  100  |  | 
|  101   // Gets the WebContents associated with the ExtensionHost of the first |  | 
|  102   // platform app shell window that is found (most tests only deal with one |  | 
|  103   // platform app window, so this is good enough). |  | 
|  104   WebContents* GetFirstPlatformAppShellWindowWebContents() { |  | 
|  105     ExtensionProcessManager* process_manager = |  | 
|  106         browser()->profile()->GetExtensionProcessManager(); |  | 
|  107     ExtensionProcessManager::const_iterator iter; |  | 
|  108     ExtensionProcessManager::ExtensionHostSet platform_app_hosts = |  | 
|  109         process_manager->platform_app_hosts(); |  | 
|  110     for (iter = platform_app_hosts.begin(); iter != platform_app_hosts.end(); |  | 
|  111          ++iter) { |  | 
|  112       return (*iter)->host_contents(); |  | 
|  113     } |   91     } | 
|  114  |   92  | 
|  115     return NULL; |   93     return NULL; | 
|  116   } |   94   } | 
|  117 }; |   95 }; | 
|  118  |   96  | 
|  119 // Tests that platform apps received the "launch" event when launched. |   97 // Tests that platform apps received the "launch" event when launched. | 
|  120 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |   98 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { | 
|  121   ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |   99   ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; | 
|  122 } |  100 } | 
|  123  |  101  | 
|  124 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |  102 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { | 
|  125   ExtensionTestMessageListener launched_listener("Launched", false); |  103   ExtensionTestMessageListener launched_listener("Launched", false); | 
|  126   LoadAndLaunchPlatformApp("empty_context_menu"); |  104   LoadAndLaunchPlatformApp("empty_context_menu"); | 
|  127  |  105  | 
|  128   ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |  106   ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); | 
|  129  |  107  | 
|  130   // The empty app doesn't add any context menu items, so its menu should |  108   // The empty app doesn't add any context menu items, so its menu should | 
|  131   // only include the developer tools. |  109   // only include the developer tools. | 
|  132   WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents(); |  110   WebContents* web_contents = GetFirstShellWindowWebContents(); | 
|  133   ASSERT_TRUE(web_contents); |  111   ASSERT_TRUE(web_contents); | 
|  134   WebKit::WebContextMenuData data; |  112   WebKit::WebContextMenuData data; | 
|  135   content::ContextMenuParams params(data); |  113   content::ContextMenuParams params(data); | 
|  136   PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |  114   PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, | 
|  137       params); |  115       params); | 
|  138   menu->Init(); |  116   menu->Init(); | 
 |  117   // TODO(benwells): Remove the constant below. Instead of checking the | 
 |  118   // number of menu items check certain item's absense and presence. | 
|  139   // 3 including separator |  119   // 3 including separator | 
|  140   ASSERT_EQ(3, menu->menu_model().GetItemCount()); |  120   ASSERT_EQ(3, menu->menu_model().GetItemCount()); | 
|  141 } |  121 } | 
|  142  |  122  | 
|  143 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |  123 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { | 
|  144   ExtensionTestMessageListener launched_listener("Launched", false); |  124   ExtensionTestMessageListener launched_listener("Launched", false); | 
|  145   LoadAndLaunchPlatformApp("context_menu"); |  125   LoadAndLaunchPlatformApp("context_menu"); | 
|  146  |  126  | 
|  147   // Wait for the extension to tell us it's initialized its context menus and |  127   // Wait for the extension to tell us it's initialized its context menus and | 
|  148   // launched a window. |  128   // launched a window. | 
|  149   ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |  129   ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); | 
|  150  |  130  | 
|  151   // The context_menu app has one context menu item. This, along with a |  131   // The context_menu app has one context menu item. This, along with a | 
|  152   // separator and the developer tools, is all that should be in the menu. |  132   // separator and the developer tools, is all that should be in the menu. | 
|  153   WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents(); |  133   WebContents* web_contents = GetFirstShellWindowWebContents(); | 
|  154   ASSERT_TRUE(web_contents); |  134   ASSERT_TRUE(web_contents); | 
|  155   WebKit::WebContextMenuData data; |  135   WebKit::WebContextMenuData data; | 
|  156   content::ContextMenuParams params(data); |  136   content::ContextMenuParams params(data); | 
|  157   PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |  137   PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, | 
|  158       params); |  138       params); | 
|  159   menu->Init(); |  139   menu->Init(); | 
 |  140   // TODO(benwells): Remove the constant below. Instead of checking the | 
 |  141   // number of menu items check certain item's absense and presence. | 
|  160   ASSERT_EQ(4, menu->menu_model().GetItemCount()); |  142   ASSERT_EQ(4, menu->menu_model().GetItemCount()); | 
|  161 } |  143 } | 
|  162  |  144  | 
|  163 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |  145 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { | 
|  164   ASSERT_TRUE(StartTestServer()); |  146   ASSERT_TRUE(StartTestServer()); | 
|  165   ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |  147   ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; | 
|  166 } |  148 } | 
|  167  |  149  | 
|  168 // Tests that localStorage and WebSQL are disabled for platform apps. |  150 // Tests that localStorage and WebSQL are disabled for platform apps. | 
|  169 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |  151 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { | 
|  170   ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |  152   ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; | 
|  171 } |  153 } | 
|  172  |  154  | 
|  173 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |  155 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { | 
|  174   ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |  156   ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; | 
|  175 } |  157 } | 
|  176  |  158  | 
|  177 // Tests that platform apps can use the chrome.windows.* API. |  159 // Tests that platform apps can use the chrome.windows.* API. | 
|  178 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |  160 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { | 
|  179   ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |  161   ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; | 
|  180 } |  162 } | 
| OLD | NEW |