blob: a9ca9d1ce7211142ee6a44e238060b8a9193bfe2 [file] [log] [blame]
[email protected]41619a9a2011-04-13 20:07:321// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]1c3cf8b2010-05-11 23:18:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1870d5cf2011-05-12 01:55:405#include "base/stringprintf.h"
[email protected]3ab9cb82011-06-03 18:02:076#include "base/utf_string_conversions.h"
[email protected]41619a9a2011-04-13 20:07:327#include "chrome/browser/background_contents_service.h"
[email protected]d2fad142011-04-15 10:18:448#include "chrome/browser/background_contents_service_factory.h"
[email protected]1c3cf8b2010-05-11 23:18:279#include "chrome/browser/extensions/extension_apitest.h"
[email protected]41619a9a2011-04-13 20:07:3210#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
[email protected]1c3cf8b2010-05-11 23:18:2713#include "chrome/common/chrome_switches.h"
[email protected]41619a9a2011-04-13 20:07:3214#include "chrome/common/extensions/extension.h"
[email protected]1c3cf8b2010-05-11 23:18:2715#include "chrome/test/ui_test_utils.h"
16#include "net/base/mock_host_resolver.h"
17
18class AppBackgroundPageApiTest : public ExtensionApiTest {
19 public:
20 void SetUpCommandLine(CommandLine* command_line) {
21 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]1c3cf8b2010-05-11 23:18:2722 command_line->AppendSwitch(switches::kDisablePopupBlocking);
[email protected]41619a9a2011-04-13 20:07:3223 command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage);
[email protected]1c3cf8b2010-05-11 23:18:2724 }
[email protected]f08debf2010-10-23 10:13:0225
26 bool CreateApp(const std::string& app_manifest,
27 FilePath* app_dir) {
28 if (!app_dir_.CreateUniqueTempDir()) {
29 LOG(ERROR) << "Unable to create a temporary directory.";
30 return false;
31 }
32 FilePath manifest_path = app_dir_.path().AppendASCII("manifest.json");
33 int bytes_written = file_util::WriteFile(manifest_path,
34 app_manifest.data(),
35 app_manifest.size());
36 if (bytes_written != static_cast<int>(app_manifest.size())) {
37 LOG(ERROR) << "Unable to write complete manifest to file. Return code="
38 << bytes_written;
39 return false;
40 }
41 *app_dir = app_dir_.path();
42 return true;
43 }
44
45 private:
46 ScopedTempDir app_dir_;
[email protected]1c3cf8b2010-05-11 23:18:2747};
48
[email protected]4dca3172010-09-19 20:40:1549IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, Basic) {
[email protected]1c3cf8b2010-05-11 23:18:2750 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:3051 ASSERT_TRUE(StartTestServer());
[email protected]1c3cf8b2010-05-11 23:18:2752
[email protected]1870d5cf2011-05-12 01:55:4053 std::string app_manifest = base::StringPrintf(
[email protected]f08debf2010-10-23 10:13:0254 "{"
55 " \"name\": \"App\","
56 " \"version\": \"0.1\","
57 " \"app\": {"
58 " \"urls\": ["
59 " \"http://a.com/\""
60 " ],"
61 " \"launch\": {"
62 " \"web_url\": \"http://a.com:%d/\""
63 " }"
64 " },"
65 " \"permissions\": [\"background\"]"
66 "}",
67 test_server()->host_port_pair().port());
68
69 FilePath app_dir;
70 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
71 ASSERT_TRUE(LoadExtension(app_dir));
[email protected]1c3cf8b2010-05-11 23:18:2772 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
73}
74
[email protected]4b5e29f2011-01-11 07:44:0875// Crashy, http://crbug.com/49215.
76IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
[email protected]1c3cf8b2010-05-11 23:18:2777 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:3078 ASSERT_TRUE(StartTestServer());
[email protected]1c3cf8b2010-05-11 23:18:2779
[email protected]1870d5cf2011-05-12 01:55:4080 std::string app_manifest = base::StringPrintf(
[email protected]f08debf2010-10-23 10:13:0281 "{"
82 " \"name\": \"App\","
83 " \"version\": \"0.1\","
84 " \"app\": {"
85 " \"urls\": ["
86 " \"http://a.com/\""
87 " ],"
88 " \"launch\": {"
89 " \"web_url\": \"http://a.com:%d/\""
90 " }"
91 " }"
92 "}",
93 test_server()->host_port_pair().port());
94
95 FilePath app_dir;
96 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
97 ASSERT_TRUE(LoadExtension(app_dir));
[email protected]1c3cf8b2010-05-11 23:18:2798 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
99 << message_;
100}
[email protected]41619a9a2011-04-13 20:07:32101
102IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
103 host_resolver()->AddRule("a.com", "127.0.0.1");
104 ASSERT_TRUE(StartTestServer());
105
[email protected]1870d5cf2011-05-12 01:55:40106 std::string app_manifest = base::StringPrintf(
[email protected]41619a9a2011-04-13 20:07:32107 "{"
108 " \"name\": \"App\","
109 " \"version\": \"0.1\","
110 " \"app\": {"
111 " \"urls\": ["
112 " \"http://a.com/\""
113 " ],"
114 " \"launch\": {"
115 " \"web_url\": \"http://a.com:%d/\""
116 " }"
117 " },"
118 " \"permissions\": [\"background\"],"
119 " \"background_page\": \"http://a.com:%d/test.html\""
120 "}",
121 test_server()->host_port_pair().port(),
122 test_server()->host_port_pair().port());
123
124 FilePath app_dir;
125 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
126 ASSERT_TRUE(LoadExtension(app_dir));
127
128 const Extension* extension = GetSingleLoadedExtension();
[email protected]d2fad142011-04-15 10:18:44129 ASSERT_TRUE(
130 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
131 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
[email protected]41619a9a2011-04-13 20:07:32132}