blob: 73353066f640a86c0cf5cd89dd8b71892fcebb24 [file] [log] [blame]
[email protected]a964e112011-04-14 21:52:511// 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]0ef01962011-06-14 08:33:379#include "base/memory/scoped_ptr.h"
[email protected]a964e112011-04-14 21:52:5110#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"
[email protected]953620b2011-12-04 00:55:3216#include "chrome/common/extensions/manifest.h"
[email protected]af44e7fb2011-07-29 18:32:3217#include "chrome/test/base/ui_test_utils.h"
[email protected]4ca15302012-01-03 05:53:2018#include "content/public/browser/web_contents.h"
[email protected]a964e112011-04-14 21:52:5119#include "googleurl/src/gurl.h"
20#include "net/base/mock_host_resolver.h"
21
22class ChromeAppAPITest : public ExtensionBrowserTest {
[email protected]80675fc2011-06-21 02:05:4923 protected:
24 bool IsAppInstalled() {
25 std::wstring get_app_is_installed =
26 L"window.domAutomationController.send(window.chrome.app.isInstalled);";
27 bool result;
28 CHECK(
29 ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]4ca15302012-01-03 05:53:2030 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]80675fc2011-06-21 02:05:4931 L"", get_app_is_installed, &result));
32 return result;
33 }
34
[email protected]a964e112011-04-14 21:52:5135 private:
36 virtual void SetUpCommandLine(CommandLine* command_line) {
37 ExtensionBrowserTest::SetUpCommandLine(command_line);
38 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL,
39 "http://checkout.com:");
40 }
41};
42
43IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, IsInstalled) {
44 std::string app_host("app.com");
45 std::string nonapp_host("nonapp.com");
46
47 host_resolver()->AddRule(app_host, "127.0.0.1");
48 host_resolver()->AddRule(nonapp_host, "127.0.0.1");
49 ASSERT_TRUE(test_server()->Start());
50
51 GURL test_file_url(test_server()->GetURL("extensions/test_file.html"));
52 GURL::Replacements replace_host;
53
54 replace_host.SetHostStr(app_host);
55 GURL app_url(test_file_url.ReplaceComponents(replace_host));
56
57 replace_host.SetHostStr(nonapp_host);
58 GURL non_app_url(test_file_url.ReplaceComponents(replace_host));
59
[email protected]80675fc2011-06-21 02:05:4960 // Before the app is installed, app.com does not think that it is installed
61 ui_test_utils::NavigateToURL(browser(), app_url);
62 EXPECT_FALSE(IsAppInstalled());
[email protected]a964e112011-04-14 21:52:5163
64 // Load an app which includes app.com in its extent.
65 const Extension* extension = LoadExtension(
66 test_data_dir_.AppendASCII("app_dot_com_app"));
67 ASSERT_TRUE(extension);
68
[email protected]80675fc2011-06-21 02:05:4969 // Even after the app is installed, the existing app.com tab is not in an
70 // app process, so chrome.app.isInstalled should return false.
71 EXPECT_FALSE(IsAppInstalled());
[email protected]a964e112011-04-14 21:52:5172
73 // Test that a non-app page has chrome.app.isInstalled = false.
74 ui_test_utils::NavigateToURL(browser(), non_app_url);
[email protected]80675fc2011-06-21 02:05:4975 EXPECT_FALSE(IsAppInstalled());
[email protected]a964e112011-04-14 21:52:5176
77 // Test that a non-app page returns null for chrome.app.getDetails().
78 std::wstring get_app_details =
79 L"window.domAutomationController.send("
80 L" JSON.stringify(window.chrome.app.getDetails()));";
[email protected]80675fc2011-06-21 02:05:4981 std::string result;
[email protected]a964e112011-04-14 21:52:5182 ASSERT_TRUE(
83 ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:2084 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]a964e112011-04-14 21:52:5185 L"", get_app_details, &result));
86 EXPECT_EQ("null", result);
87
88 // Check that an app page has chrome.app.isInstalled = true.
89 ui_test_utils::NavigateToURL(browser(), app_url);
[email protected]80675fc2011-06-21 02:05:4990 EXPECT_TRUE(IsAppInstalled());
[email protected]a964e112011-04-14 21:52:5191
92 // Check that an app page returns the correct result for
93 // chrome.app.getDetails().
94 ui_test_utils::NavigateToURL(browser(), app_url);
95 ASSERT_TRUE(
96 ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:2097 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]a964e112011-04-14 21:52:5198 L"", get_app_details, &result));
99 scoped_ptr<DictionaryValue> app_details(
100 static_cast<DictionaryValue*>(
101 base::JSONReader::Read(result, false /* allow trailing comma */)));
[email protected]953620b2011-12-04 00:55:32102 // extension->manifest() does not contain the id.
[email protected]a964e112011-04-14 21:52:51103 app_details->Remove("id", NULL);
104 EXPECT_TRUE(app_details.get());
[email protected]953620b2011-12-04 00:55:32105 EXPECT_TRUE(app_details->Equals(extension->manifest()->value()));
[email protected]a964e112011-04-14 21:52:51106
[email protected]9b3a5c22011-05-31 08:03:13107 // Try to change app.isInstalled. Should silently fail, so
108 // that isInstalled should have the initial value.
109 ASSERT_TRUE(
110 ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20111 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]a964e112011-04-14 21:52:51112 L"",
113 L"window.domAutomationController.send("
114 L" function() {"
[email protected]9b3a5c22011-05-31 08:03:13115 L" var value = window.chrome.app.isInstalled;"
116 L" window.chrome.app.isInstalled = !value;"
117 L" if (window.chrome.app.isInstalled == value) {"
118 L" return 'true';"
119 L" } else {"
120 L" return 'false';"
121 L" }"
[email protected]a964e112011-04-14 21:52:51122 L" }()"
123 L");",
[email protected]9b3a5c22011-05-31 08:03:13124 &result));
125
126 // Should not be able to alter window.chrome.app.isInstalled from javascript";
127 EXPECT_EQ("true", result);
[email protected]a964e112011-04-14 21:52:51128}
129
130IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) {
131 std::string app_host("app.com");
132 std::string nonapp_host("nonapp.com");
133 std::string checkout_host("checkout.com");
134
135 host_resolver()->AddRule(app_host, "127.0.0.1");
136 host_resolver()->AddRule(nonapp_host, "127.0.0.1");
137 host_resolver()->AddRule(checkout_host, "127.0.0.1");
138 ASSERT_TRUE(test_server()->Start());
139
140 GURL test_file_url(test_server()->GetURL(
141 "files/extensions/get_app_details_for_frame.html"));
142 GURL::Replacements replace_host;
143
144 replace_host.SetHostStr(checkout_host);
145 GURL checkout_url(test_file_url.ReplaceComponents(replace_host));
146
147 replace_host.SetHostStr(app_host);
148 GURL app_url(test_file_url.ReplaceComponents(replace_host));
149
150 // Load an app which includes app.com in its extent.
151 const Extension* extension = LoadExtension(
152 test_data_dir_.AppendASCII("app_dot_com_app"));
153 ASSERT_TRUE(extension);
154
155 // Test that normal pages (even apps) cannot use getDetailsForFrame().
156 ui_test_utils::NavigateToURL(browser(), app_url);
157 std::wstring test_unsuccessful_access =
158 L"window.domAutomationController.send(window.testUnsuccessfulAccess())";
159 bool result = false;
160 ASSERT_TRUE(
161 ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]4ca15302012-01-03 05:53:20162 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]a964e112011-04-14 21:52:51163 L"", test_unsuccessful_access, &result));
164 EXPECT_TRUE(result);
165
166 // Test that checkout can use getDetailsForFrame() and that it works
167 // correctly.
168 ui_test_utils::NavigateToURL(browser(), checkout_url);
169 std::wstring get_details_for_frame =
170 L"window.domAutomationController.send("
171 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))";
172 std::string json;
173 ASSERT_TRUE(
174 ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20175 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]a964e112011-04-14 21:52:51176 L"", get_details_for_frame, &json));
177
178 scoped_ptr<DictionaryValue> app_details(
179 static_cast<DictionaryValue*>(
180 base::JSONReader::Read(json, false /* allow trailing comma */)));
[email protected]953620b2011-12-04 00:55:32181 // extension->manifest() does not contain the id.
[email protected]a964e112011-04-14 21:52:51182 app_details->Remove("id", NULL);
183 EXPECT_TRUE(app_details.get());
[email protected]953620b2011-12-04 00:55:32184 EXPECT_TRUE(app_details->Equals(extension->manifest()->value()));
[email protected]a964e112011-04-14 21:52:51185}