Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: chrome/browser/extensions/app_background_page_apitest.cc

Issue 10298002: No longer start BG mode until a BackgroundContents is loaded (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed tests. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/stringprintf.h" 5 #include "base/stringprintf.h"
6 #include "base/utf_string_conversions.h" 6 #include "base/utf_string_conversions.h"
7 #include "chrome/browser/background/background_contents_service.h" 7 #include "chrome/browser/background/background_contents_service.h"
8 #include "chrome/browser/background/background_contents_service_factory.h" 8 #include "chrome/browser/background/background_contents_service_factory.h"
9 #include "chrome/browser/background/background_mode_manager.h"
10 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/extension_apitest.h" 11 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
14 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/extensions/extension.h" 17 #include "chrome/common/extensions/extension.h"
16 #include "chrome/test/base/ui_test_utils.h" 18 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/test/test_notification_tracker.h" 19 #include "content/test/test_notification_tracker.h"
18 #include "net/base/mock_host_resolver.h" 20 #include "net/base/mock_host_resolver.h"
(...skipping 18 matching lines...) Expand all
37 app_manifest.size()); 39 app_manifest.size());
38 if (bytes_written != static_cast<int>(app_manifest.size())) { 40 if (bytes_written != static_cast<int>(app_manifest.size())) {
39 LOG(ERROR) << "Unable to write complete manifest to file. Return code=" 41 LOG(ERROR) << "Unable to write complete manifest to file. Return code="
40 << bytes_written; 42 << bytes_written;
41 return false; 43 return false;
42 } 44 }
43 *app_dir = app_dir_.path(); 45 *app_dir = app_dir_.path();
44 return true; 46 return true;
45 } 47 }
46 48
49 bool CheckBackgroundMode(bool expected_background_mode) {
50 BackgroundModeManager* manager =
51 g_browser_process->background_mode_manager();
52 // If background mode is disabled on this platform (e.g. cros), then skip
53 // this check.
54 if (!manager || !manager->IsBackgroundModePrefEnabled()) {
55 DLOG(WARNING) << "Skipping check - background mode disabled";
56 return true;
57 }
58 return manager->IsBackgroundModeActiveForTest() == expected_background_mode;
59 }
60
47 private: 61 private:
48 ScopedTempDir app_dir_; 62 ScopedTempDir app_dir_;
49 }; 63 };
50 64
51 // Disable on Mac only. http://crbug.com/95139 65 // Disable on Mac only. http://crbug.com/95139
52 #if defined(OS_MACOSX) 66 #if defined(OS_MACOSX)
53 #define MAYBE_Basic DISABLED_Basic 67 #define MAYBE_Basic DISABLED_Basic
54 #else 68 #else
55 #define MAYBE_Basic Basic 69 #define MAYBE_Basic Basic
56 #endif 70 #endif
(...skipping 15 matching lines...) Expand all
72 " \"web_url\": \"http://a.com:%d/\"" 86 " \"web_url\": \"http://a.com:%d/\""
73 " }" 87 " }"
74 " }," 88 " },"
75 " \"permissions\": [\"background\"]" 89 " \"permissions\": [\"background\"]"
76 "}", 90 "}",
77 test_server()->host_port_pair().port()); 91 test_server()->host_port_pair().port());
78 92
79 FilePath app_dir; 93 FilePath app_dir;
80 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); 94 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
81 ASSERT_TRUE(LoadExtension(app_dir)); 95 ASSERT_TRUE(LoadExtension(app_dir));
96 // Background mode should not be active until a background page is created.
97 ASSERT_TRUE(CheckBackgroundMode(false));
82 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; 98 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
99 // Background mode should be active now because a background page was created.
100 ASSERT_TRUE(CheckBackgroundMode(true));
83 } 101 }
84 102
85 // Crashy, http://crbug.com/69215. 103 // Crashy, http://crbug.com/69215.
86 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { 104 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
87 host_resolver()->AddRule("a.com", "127.0.0.1"); 105 host_resolver()->AddRule("a.com", "127.0.0.1");
88 ASSERT_TRUE(StartTestServer()); 106 ASSERT_TRUE(StartTestServer());
89 107
90 std::string app_manifest = base::StringPrintf( 108 std::string app_manifest = base::StringPrintf(
91 "{" 109 "{"
92 " \"name\": \"App\"," 110 " \"name\": \"App\","
93 " \"version\": \"0.1\"," 111 " \"version\": \"0.1\","
94 " \"manifest_version\": 2," 112 " \"manifest_version\": 2,"
95 " \"app\": {" 113 " \"app\": {"
96 " \"urls\": [" 114 " \"urls\": ["
97 " \"http://a.com/\"" 115 " \"http://a.com/\""
98 " ]," 116 " ],"
99 " \"launch\": {" 117 " \"launch\": {"
100 " \"web_url\": \"http://a.com:%d/\"" 118 " \"web_url\": \"http://a.com:%d/\""
101 " }" 119 " }"
102 " }" 120 " }"
103 "}", 121 "}",
104 test_server()->host_port_pair().port()); 122 test_server()->host_port_pair().port());
105 123
106 FilePath app_dir; 124 FilePath app_dir;
107 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); 125 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
108 ASSERT_TRUE(LoadExtension(app_dir)); 126 ASSERT_TRUE(LoadExtension(app_dir));
109 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) 127 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
110 << message_; 128 << message_;
129 ASSERT_TRUE(CheckBackgroundMode(false));
111 } 130 }
112 131
113 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { 132 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
114 host_resolver()->AddRule("a.com", "127.0.0.1"); 133 host_resolver()->AddRule("a.com", "127.0.0.1");
115 ASSERT_TRUE(StartTestServer()); 134 ASSERT_TRUE(StartTestServer());
116 135
117 std::string app_manifest = base::StringPrintf( 136 std::string app_manifest = base::StringPrintf(
118 "{" 137 "{"
119 " \"name\": \"App\"," 138 " \"name\": \"App\","
120 " \"version\": \"0.1\"," 139 " \"version\": \"0.1\","
121 " \"manifest_version\": 2," 140 " \"manifest_version\": 2,"
122 " \"app\": {" 141 " \"app\": {"
123 " \"urls\": [" 142 " \"urls\": ["
124 " \"http://a.com/\"" 143 " \"http://a.com/\""
125 " ]," 144 " ],"
126 " \"launch\": {" 145 " \"launch\": {"
127 " \"web_url\": \"http://a.com:%d/\"" 146 " \"web_url\": \"http://a.com:%d/\""
128 " }" 147 " }"
129 " }," 148 " },"
130 " \"permissions\": [\"background\"]," 149 " \"permissions\": [\"background\"],"
131 " \"background\": {" 150 " \"background\": {"
132 " \"page\": \"http://a.com:%d/test.html\"" 151 " \"page\": \"http://a.com:%d/test.html\""
133 " }" 152 " }"
134 "}", 153 "}",
135 test_server()->host_port_pair().port(), 154 test_server()->host_port_pair().port(),
136 test_server()->host_port_pair().port()); 155 test_server()->host_port_pair().port());
137 156
138 FilePath app_dir; 157 FilePath app_dir;
139 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); 158 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
159 // Background mode should not be active now because no background app was
160 // loaded.
140 ASSERT_TRUE(LoadExtension(app_dir)); 161 ASSERT_TRUE(LoadExtension(app_dir));
162 // Background mode be active now because a background page was created when
163 // the app was loaded.
164 ASSERT_TRUE(CheckBackgroundMode(true));
141 165
142 const Extension* extension = GetSingleLoadedExtension(); 166 const Extension* extension = GetSingleLoadedExtension();
143 ASSERT_TRUE( 167 ASSERT_TRUE(
144 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> 168 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
145 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); 169 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
146 } 170 }
147 171
148 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { 172 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) {
149 // Make sure that no BackgroundContentses get deleted (a signal that repeated 173 // Make sure that no BackgroundContentses get deleted (a signal that repeated
150 // window.open calls recreate instances, instead of being no-ops). 174 // window.open calls recreate instances, instead of being no-ops).
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 " \"permissions\": [\"background\"]" 340 " \"permissions\": [\"background\"]"
317 "}", 341 "}",
318 test_server()->host_port_pair().port(), 342 test_server()->host_port_pair().port(),
319 test_server()->host_port_pair().port()); 343 test_server()->host_port_pair().port());
320 344
321 FilePath app_dir; 345 FilePath app_dir;
322 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); 346 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
323 ASSERT_TRUE(LoadExtension(app_dir)); 347 ASSERT_TRUE(LoadExtension(app_dir));
324 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; 348 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_;
325 } 349 }
350
351 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenThenClose) {
352 host_resolver()->AddRule("a.com", "127.0.0.1");
353 ASSERT_TRUE(StartTestServer());
354
355 std::string app_manifest = base::StringPrintf(
356 "{"
357 " \"name\": \"App\","
358 " \"version\": \"0.1\","
359 " \"manifest_version\": 2,"
360 " \"app\": {"
361 " \"urls\": ["
362 " \"http://a.com/\""
363 " ],"
364 " \"launch\": {"
365 " \"web_url\": \"http://a.com:%d/\""
366 " }"
367 " },"
368 " \"permissions\": [\"background\"]"
369 "}",
370 test_server()->host_port_pair().port());
371
372 FilePath app_dir;
373 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
374 ASSERT_TRUE(LoadExtension(app_dir));
375 // There isn't a background page loaded initially.
376 const Extension* extension = GetSingleLoadedExtension();
377 ASSERT_FALSE(
378 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
379 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
380 // Background mode should not be active until a background page is created.
381 ASSERT_TRUE(CheckBackgroundMode(false));
382 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
383 // Background mode should be active now because a background page was created.
384 ASSERT_TRUE(CheckBackgroundMode(true));
385 ASSERT_TRUE(
386 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
387 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
388 // Now close the BackgroundContents.
389 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_;
390 // Background mode should no longer be active.
391 ASSERT_TRUE(CheckBackgroundMode(false));
392 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698