[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 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 |
| 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] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 10 | #include "base/string_number_conversions.h" |
| 11 | #include "base/values.h" |
| 12 | #include "chrome/browser/extensions/extension_browsertest.h" |
| 13 | #include "chrome/browser/ui/browser.h" |
| 14 | #include "chrome/common/chrome_switches.h" |
| 15 | #include "chrome/common/extensions/extension.h" |
| 16 | #include "chrome/test/ui_test_utils.h" |
| 17 | #include "content/browser/tab_contents/tab_contents.h" |
| 18 | #include "googleurl/src/gurl.h" |
| 19 | #include "net/base/mock_host_resolver.h" |
| 20 | |
| 21 | class ChromeAppAPITest : public ExtensionBrowserTest { |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame^] | 22 | protected: |
| 23 | bool IsAppInstalled() { |
| 24 | std::wstring get_app_is_installed = |
| 25 | L"window.domAutomationController.send(window.chrome.app.isInstalled);"; |
| 26 | bool result; |
| 27 | CHECK( |
| 28 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 29 | browser()->GetSelectedTabContents()->render_view_host(), |
| 30 | L"", get_app_is_installed, &result)); |
| 31 | return result; |
| 32 | } |
| 33 | |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 34 | private: |
| 35 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 36 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 37 | command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, |
| 38 | "http://checkout.com:"); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, IsInstalled) { |
| 43 | std::string app_host("app.com"); |
| 44 | std::string nonapp_host("nonapp.com"); |
| 45 | |
| 46 | host_resolver()->AddRule(app_host, "127.0.0.1"); |
| 47 | host_resolver()->AddRule(nonapp_host, "127.0.0.1"); |
| 48 | ASSERT_TRUE(test_server()->Start()); |
| 49 | |
| 50 | GURL test_file_url(test_server()->GetURL("extensions/test_file.html")); |
| 51 | GURL::Replacements replace_host; |
| 52 | |
| 53 | replace_host.SetHostStr(app_host); |
| 54 | GURL app_url(test_file_url.ReplaceComponents(replace_host)); |
| 55 | |
| 56 | replace_host.SetHostStr(nonapp_host); |
| 57 | GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); |
| 58 | |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame^] | 59 | // Before the app is installed, app.com does not think that it is installed |
| 60 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 61 | EXPECT_FALSE(IsAppInstalled()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 62 | |
| 63 | // Load an app which includes app.com in its extent. |
| 64 | const Extension* extension = LoadExtension( |
| 65 | test_data_dir_.AppendASCII("app_dot_com_app")); |
| 66 | ASSERT_TRUE(extension); |
| 67 | |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame^] | 68 | // Even after the app is installed, the existing app.com tab is not in an |
| 69 | // app process, so chrome.app.isInstalled should return false. |
| 70 | EXPECT_FALSE(IsAppInstalled()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 71 | |
| 72 | // Test that a non-app page has chrome.app.isInstalled = false. |
| 73 | ui_test_utils::NavigateToURL(browser(), non_app_url); |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame^] | 74 | EXPECT_FALSE(IsAppInstalled()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 75 | |
| 76 | // Test that a non-app page returns null for chrome.app.getDetails(). |
| 77 | std::wstring get_app_details = |
| 78 | L"window.domAutomationController.send(" |
| 79 | L" JSON.stringify(window.chrome.app.getDetails()));"; |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame^] | 80 | std::string result; |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 81 | ASSERT_TRUE( |
| 82 | ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 83 | browser()->GetSelectedTabContents()->render_view_host(), |
| 84 | L"", get_app_details, &result)); |
| 85 | EXPECT_EQ("null", result); |
| 86 | |
| 87 | // Check that an app page has chrome.app.isInstalled = true. |
| 88 | ui_test_utils::NavigateToURL(browser(), app_url); |
[email protected] | 80675fc | 2011-06-21 02:05:49 | [diff] [blame^] | 89 | EXPECT_TRUE(IsAppInstalled()); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 90 | |
| 91 | // Check that an app page returns the correct result for |
| 92 | // chrome.app.getDetails(). |
| 93 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 94 | ASSERT_TRUE( |
| 95 | ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 96 | browser()->GetSelectedTabContents()->render_view_host(), |
| 97 | L"", get_app_details, &result)); |
| 98 | scoped_ptr<DictionaryValue> app_details( |
| 99 | static_cast<DictionaryValue*>( |
| 100 | base::JSONReader::Read(result, false /* allow trailing comma */))); |
| 101 | // extension->manifest_value() does not contain the id. |
| 102 | app_details->Remove("id", NULL); |
| 103 | EXPECT_TRUE(app_details.get()); |
| 104 | EXPECT_TRUE(app_details->Equals(extension->manifest_value())); |
| 105 | |
[email protected] | 9b3a5c2 | 2011-05-31 08:03:13 | [diff] [blame] | 106 | // Try to change app.isInstalled. Should silently fail, so |
| 107 | // that isInstalled should have the initial value. |
| 108 | ASSERT_TRUE( |
| 109 | ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 110 | browser()->GetSelectedTabContents()->render_view_host(), |
| 111 | L"", |
| 112 | L"window.domAutomationController.send(" |
| 113 | L" function() {" |
[email protected] | 9b3a5c2 | 2011-05-31 08:03:13 | [diff] [blame] | 114 | L" var value = window.chrome.app.isInstalled;" |
| 115 | L" window.chrome.app.isInstalled = !value;" |
| 116 | L" if (window.chrome.app.isInstalled == value) {" |
| 117 | L" return 'true';" |
| 118 | L" } else {" |
| 119 | L" return 'false';" |
| 120 | L" }" |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 121 | L" }()" |
| 122 | L");", |
[email protected] | 9b3a5c2 | 2011-05-31 08:03:13 | [diff] [blame] | 123 | &result)); |
| 124 | |
| 125 | // Should not be able to alter window.chrome.app.isInstalled from javascript"; |
| 126 | EXPECT_EQ("true", result); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) { |
| 130 | std::string app_host("app.com"); |
| 131 | std::string nonapp_host("nonapp.com"); |
| 132 | std::string checkout_host("checkout.com"); |
| 133 | |
| 134 | host_resolver()->AddRule(app_host, "127.0.0.1"); |
| 135 | host_resolver()->AddRule(nonapp_host, "127.0.0.1"); |
| 136 | host_resolver()->AddRule(checkout_host, "127.0.0.1"); |
| 137 | ASSERT_TRUE(test_server()->Start()); |
| 138 | |
| 139 | GURL test_file_url(test_server()->GetURL( |
| 140 | "files/extensions/get_app_details_for_frame.html")); |
| 141 | GURL::Replacements replace_host; |
| 142 | |
| 143 | replace_host.SetHostStr(checkout_host); |
| 144 | GURL checkout_url(test_file_url.ReplaceComponents(replace_host)); |
| 145 | |
| 146 | replace_host.SetHostStr(app_host); |
| 147 | GURL app_url(test_file_url.ReplaceComponents(replace_host)); |
| 148 | |
| 149 | // Load an app which includes app.com in its extent. |
| 150 | const Extension* extension = LoadExtension( |
| 151 | test_data_dir_.AppendASCII("app_dot_com_app")); |
| 152 | ASSERT_TRUE(extension); |
| 153 | |
| 154 | // Test that normal pages (even apps) cannot use getDetailsForFrame(). |
| 155 | ui_test_utils::NavigateToURL(browser(), app_url); |
| 156 | std::wstring test_unsuccessful_access = |
| 157 | L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; |
| 158 | bool result = false; |
| 159 | ASSERT_TRUE( |
| 160 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 161 | browser()->GetSelectedTabContents()->render_view_host(), |
| 162 | L"", test_unsuccessful_access, &result)); |
| 163 | EXPECT_TRUE(result); |
| 164 | |
| 165 | // Test that checkout can use getDetailsForFrame() and that it works |
| 166 | // correctly. |
| 167 | ui_test_utils::NavigateToURL(browser(), checkout_url); |
| 168 | std::wstring get_details_for_frame = |
| 169 | L"window.domAutomationController.send(" |
| 170 | L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; |
| 171 | std::string json; |
| 172 | ASSERT_TRUE( |
| 173 | ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 174 | browser()->GetSelectedTabContents()->render_view_host(), |
| 175 | L"", get_details_for_frame, &json)); |
| 176 | |
| 177 | scoped_ptr<DictionaryValue> app_details( |
| 178 | static_cast<DictionaryValue*>( |
| 179 | base::JSONReader::Read(json, false /* allow trailing comma */))); |
| 180 | // extension->manifest_value() does not contain the id. |
| 181 | app_details->Remove("id", NULL); |
| 182 | EXPECT_TRUE(app_details.get()); |
| 183 | EXPECT_TRUE(app_details->Equals(extension->manifest_value())); |
| 184 | } |