[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/json/json_reader.h" |
[email protected] | 0ef0196 | 2011-06-14 08:33:37 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 11 | #include "base/values.h" |
| 12 | #include "chrome/browser/extensions/extension_browsertest.h" |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_service.h" |
| 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 16 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 17 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 18 | #include "content/public/browser/web_contents.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 19 | #include "content/public/test/browser_test_utils.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 20 | #include "extensions/common/extension.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 21 | #include "extensions/common/manifest.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 22 | #include "net/dns/mock_host_resolver.h" |
nick | fa2254b | 2015-08-07 23:27:29 | [diff] [blame^] | 23 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 24 | #include "url/gurl.h" |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 25 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 26 | using extensions::Extension; |
| 27 | |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 28 | class ChromeAppAPITest : public ExtensionBrowserTest { |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame] | 29 | protected: |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 30 | bool IsAppInstalledInMainFrame() { |
| 31 | return IsAppInstalledInFrame( |
| 32 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
| 33 | } |
| 34 | bool IsAppInstalledInIFrame() { |
| 35 | return IsAppInstalledInFrame(GetIFrame()); |
| 36 | } |
| 37 | bool IsAppInstalledInFrame(content::RenderFrameHost* frame) { |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 38 | const char kGetAppIsInstalled[] = |
| 39 | "window.domAutomationController.send(window.chrome.app.isInstalled);"; |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame] | 40 | bool result; |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 41 | CHECK(content::ExecuteScriptAndExtractBool(frame, |
| 42 | kGetAppIsInstalled, |
| 43 | &result)); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 44 | return result; |
| 45 | } |
| 46 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 47 | std::string InstallStateInMainFrame() { |
| 48 | return InstallStateInFrame( |
| 49 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
| 50 | } |
| 51 | std::string InstallStateInIFrame() { |
| 52 | return InstallStateInFrame(GetIFrame()); |
| 53 | } |
| 54 | std::string InstallStateInFrame(content::RenderFrameHost* frame) { |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 55 | const char kGetAppInstallState[] = |
| 56 | "window.chrome.app.installState(" |
| 57 | " function(s) { window.domAutomationController.send(s); });"; |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 58 | std::string result; |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 59 | CHECK(content::ExecuteScriptAndExtractString(frame, |
| 60 | kGetAppInstallState, |
| 61 | &result)); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 62 | return result; |
| 63 | } |
| 64 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 65 | std::string RunningStateInMainFrame() { |
| 66 | return RunningStateInFrame( |
| 67 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
| 68 | } |
| 69 | std::string RunningStateInIFrame() { |
| 70 | return RunningStateInFrame(GetIFrame()); |
| 71 | } |
| 72 | std::string RunningStateInFrame(content::RenderFrameHost* frame) { |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 73 | const char kGetAppRunningState[] = |
| 74 | "window.domAutomationController.send(" |
| 75 | " window.chrome.app.runningState());"; |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 76 | std::string result; |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 77 | CHECK(content::ExecuteScriptAndExtractString(frame, |
| 78 | kGetAppRunningState, |
| 79 | &result)); |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame] | 80 | return result; |
| 81 | } |
| 82 | |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 83 | private: |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 84 | content::RenderFrameHost* GetIFrame() { |
| 85 | return content::FrameMatchingPredicate( |
| 86 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 87 | base::Bind(&content::FrameIsChildOfMainFrame)); |
| 88 | } |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 89 | }; |
| 90 | |
nick | fa2254b | 2015-08-07 23:27:29 | [diff] [blame^] | 91 | IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, IsInstalled) { |
| 92 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 93 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 94 | GURL app_url = |
| 95 | embedded_test_server()->GetURL("app.com", "/extensions/test_file.html"); |
| 96 | GURL non_app_url = embedded_test_server()->GetURL( |
| 97 | "nonapp.com", "/extensions/test_file.html"); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 98 | |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame] | 99 | // Before the app is installed, app.com does not think that it is installed |
| 100 | ui_test_utils::NavigateToURL(browser(), app_url); |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 101 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 102 | |
| 103 | // Load an app which includes app.com in its extent. |
| 104 | const Extension* extension = LoadExtension( |
| 105 | test_data_dir_.AppendASCII("app_dot_com_app")); |
| 106 | ASSERT_TRUE(extension); |
| 107 | |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame] | 108 | // Even after the app is installed, the existing app.com tab is not in an |
| 109 | // app process, so chrome.app.isInstalled should return false. |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 110 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 111 | |
| 112 | // Test that a non-app page has chrome.app.isInstalled = false. |
| 113 | ui_test_utils::NavigateToURL(browser(), non_app_url); |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 114 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 115 | |
| 116 | // Test that a non-app page returns null for chrome.app.getDetails(). |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 117 | const char kGetAppDetails[] = |
| 118 | "window.domAutomationController.send(" |
| 119 | " JSON.stringify(window.chrome.app.getDetails()));"; |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame] | 120 | std::string result; |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 121 | ASSERT_TRUE( |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 122 | content::ExecuteScriptAndExtractString( |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 123 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 124 | kGetAppDetails, |
| 125 | &result)); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 126 | EXPECT_EQ("null", result); |
| 127 | |
| 128 | // Check that an app page has chrome.app.isInstalled = true. |
| 129 | ui_test_utils::NavigateToURL(browser(), app_url); |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 130 | EXPECT_TRUE(IsAppInstalledInMainFrame()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 131 | |
| 132 | // Check that an app page returns the correct result for |
| 133 | // chrome.app.getDetails(). |
| 134 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 135 | ASSERT_TRUE( |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 136 | content::ExecuteScriptAndExtractString( |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 137 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 138 | kGetAppDetails, |
| 139 | &result)); |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 140 | scoped_ptr<base::DictionaryValue> app_details( |
estade | b09312b | 2015-05-22 16:30:13 | [diff] [blame] | 141 | static_cast<base::DictionaryValue*>( |
| 142 | base::JSONReader::DeprecatedRead(result))); |
[email protected] | 953620b | 2011-12-04 00:55:32 | [diff] [blame] | 143 | // extension->manifest() does not contain the id. |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 144 | app_details->Remove("id", NULL); |
| 145 | EXPECT_TRUE(app_details.get()); |
[email protected] | 953620b | 2011-12-04 00:55:32 | [diff] [blame] | 146 | EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 147 | |
[email protected] | 9b3a5c2 | 2011-05-31 08:03:13 | [diff] [blame] | 148 | // Try to change app.isInstalled. Should silently fail, so |
| 149 | // that isInstalled should have the initial value. |
| 150 | ASSERT_TRUE( |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 151 | content::ExecuteScriptAndExtractString( |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 152 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 153 | "window.domAutomationController.send(" |
| 154 | " function() {" |
| 155 | " var value = window.chrome.app.isInstalled;" |
| 156 | " window.chrome.app.isInstalled = !value;" |
| 157 | " if (window.chrome.app.isInstalled == value) {" |
| 158 | " return 'true';" |
| 159 | " } else {" |
| 160 | " return 'false';" |
| 161 | " }" |
| 162 | " }()" |
| 163 | ");", |
| 164 | &result)); |
[email protected] | 9b3a5c2 | 2011-05-31 08:03:13 | [diff] [blame] | 165 | |
| 166 | // Should not be able to alter window.chrome.app.isInstalled from javascript"; |
| 167 | EXPECT_EQ("true", result); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 170 | IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningState) { |
nick | fa2254b | 2015-08-07 23:27:29 | [diff] [blame^] | 171 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 172 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 173 | GURL app_url = embedded_test_server()->GetURL( |
| 174 | "app.com", "/extensions/get_app_details_for_frame.html"); |
| 175 | GURL non_app_url = embedded_test_server()->GetURL( |
| 176 | "nonapp.com", "/extensions/get_app_details_for_frame.html"); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 177 | |
| 178 | // Before the app is installed, app.com does not think that it is installed |
| 179 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 180 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 181 | EXPECT_EQ("not_installed", InstallStateInMainFrame()); |
| 182 | EXPECT_EQ("cannot_run", RunningStateInMainFrame()); |
| 183 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 184 | |
| 185 | const Extension* extension = LoadExtension( |
| 186 | test_data_dir_.AppendASCII("app_dot_com_app")); |
| 187 | ASSERT_TRUE(extension); |
| 188 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 189 | EXPECT_EQ("installed", InstallStateInMainFrame()); |
| 190 | EXPECT_EQ("ready_to_run", RunningStateInMainFrame()); |
| 191 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 192 | |
| 193 | // Reloading the page should put the tab in an app process. |
| 194 | ui_test_utils::NavigateToURL(browser(), app_url); |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 195 | EXPECT_EQ("installed", InstallStateInMainFrame()); |
| 196 | EXPECT_EQ("running", RunningStateInMainFrame()); |
| 197 | EXPECT_TRUE(IsAppInstalledInMainFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 198 | |
| 199 | // Disable the extension and verify the state. |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 200 | ExtensionService* service = extensions::ExtensionSystem::Get( |
| 201 | browser()->profile())->extension_service(); |
| 202 | service->DisableExtension(extension->id(), |
| 203 | Extension::DISABLE_PERMISSIONS_INCREASE); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 204 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 205 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 206 | EXPECT_EQ("disabled", InstallStateInMainFrame()); |
| 207 | EXPECT_EQ("cannot_run", RunningStateInMainFrame()); |
| 208 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 209 | |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 210 | service->EnableExtension(extension->id()); |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 211 | EXPECT_EQ("installed", InstallStateInMainFrame()); |
| 212 | EXPECT_EQ("ready_to_run", RunningStateInMainFrame()); |
| 213 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 214 | |
| 215 | // The non-app URL should still not be installed or running. |
| 216 | ui_test_utils::NavigateToURL(browser(), non_app_url); |
| 217 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 218 | EXPECT_EQ("not_installed", InstallStateInMainFrame()); |
| 219 | EXPECT_EQ("cannot_run", RunningStateInMainFrame()); |
| 220 | EXPECT_FALSE(IsAppInstalledInMainFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 221 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 222 | EXPECT_EQ("installed", InstallStateInIFrame()); |
| 223 | EXPECT_EQ("cannot_run", RunningStateInIFrame()); |
| 224 | EXPECT_FALSE(IsAppInstalledInIFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningStateFrame) { |
nick | fa2254b | 2015-08-07 23:27:29 | [diff] [blame^] | 228 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 229 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 230 | GURL app_url = embedded_test_server()->GetURL( |
| 231 | "app.com", "/extensions/get_app_details_for_frame_reversed.html"); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 232 | |
| 233 | // Check the install and running state of a non-app iframe running |
| 234 | // within an app. |
| 235 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 236 | |
[email protected] | b27c362 | 2014-03-22 00:26:55 | [diff] [blame] | 237 | EXPECT_EQ("not_installed", InstallStateInIFrame()); |
| 238 | EXPECT_EQ("cannot_run", RunningStateInIFrame()); |
| 239 | EXPECT_FALSE(IsAppInstalledInIFrame()); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 240 | } |