This source file includes following definitions.
- Wait
- Observe
- SetUpCommandLine
- LoadExtensionAndWait
- IsBackgroundPageAlive
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/bookmark_test_helpers.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/browser_action_test_util.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/extensions/lazy_background_page_test_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/omnibox/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/switches.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"
using extensions::Extension;
namespace {
class LoadedIncognitoObserver : public content::NotificationObserver {
public:
explicit LoadedIncognitoObserver(Profile* profile) : profile_(profile) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<Profile>(profile));
}
void Wait() {
ASSERT_TRUE(original_complete_.get());
original_complete_->Wait();
incognito_complete_->Wait();
}
private:
virtual void Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE {
original_complete_.reset(new LazyBackgroundObserver(profile_));
incognito_complete_.reset(
new LazyBackgroundObserver(profile_->GetOffTheRecordProfile()));
}
Profile* profile_;
content::NotificationRegistrar registrar_;
scoped_ptr<LazyBackgroundObserver> original_complete_;
scoped_ptr<LazyBackgroundObserver> incognito_complete_;
};
}
class LazyBackgroundPageApiTest : public ExtensionApiTest {
public:
LazyBackgroundPageApiTest() {}
virtual ~LazyBackgroundPageApiTest() {}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(
extensions::switches::kEventPageIdleTime, "1000");
command_line->AppendSwitchASCII(
extensions::switches::kEventPageSuspendingTime, "1000");
}
const Extension* LoadExtensionAndWait(const std::string& test_name) {
LazyBackgroundObserver page_complete;
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII(test_name);
const Extension* extension = LoadExtension(extdir);
if (extension)
page_complete.Wait();
return extension;
}
bool IsBackgroundPageAlive(const std::string& extension_id) {
extensions::ProcessManager* pm = extensions::ExtensionSystem::Get(
browser()->profile())->process_manager();
return pm->GetBackgroundHostForExtension(extension_id);
}
private:
DISALLOW_COPY_AND_ASSIGN(LazyBackgroundPageApiTest);
};
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) {
ASSERT_TRUE(LoadExtensionAndWait("browser_action_create_tab"));
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
int num_tabs_before = browser()->tab_strip_model()->count();
LazyBackgroundObserver page_complete;
BrowserActionTestUtil(browser()).Press(0);
page_complete.Wait();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
EXPECT_EQ(num_tabs_before + 1, browser()->tab_strip_model()->count());
EXPECT_EQ(std::string(chrome::kChromeUIExtensionsURL),
browser()->tab_strip_model()->GetActiveWebContents()->
GetURL().spec());
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest,
BrowserActionCreateTabAfterCallback) {
ASSERT_TRUE(LoadExtensionAndWait("browser_action_with_callback"));
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
int num_tabs_before = browser()->tab_strip_model()->count();
LazyBackgroundObserver page_complete;
BrowserActionTestUtil(browser()).Press(0);
page_complete.Wait();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
EXPECT_EQ(num_tabs_before + 1, browser()->tab_strip_model()->count());
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BroadcastEvent) {
ASSERT_TRUE(StartEmbeddedTestServer());
const Extension* extension = LoadExtensionAndWait("broadcast_event");
ASSERT_TRUE(extension);
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
int num_page_actions = browser()->window()->GetLocationBar()->
GetLocationBarForTesting()->PageActionVisibleCount();
LazyBackgroundObserver page_complete;
content::WindowedNotificationObserver page_action_changed(
chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
content::NotificationService::AllSources());
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
page_complete.Wait();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
page_action_changed.Wait();
EXPECT_EQ(num_page_actions + 1,
browser()->window()->GetLocationBar()->
GetLocationBarForTesting()->PageActionVisibleCount());
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, Filters) {
ASSERT_TRUE(StartEmbeddedTestServer());
const Extension* extension = LoadExtensionAndWait("filters");
ASSERT_TRUE(extension);
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
LazyBackgroundObserver page_complete;
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
page_complete.Wait();
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnInstalled) {
ResultCatcher catcher;
ASSERT_TRUE(LoadExtensionAndWait("on_installed"));
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForDialog) {
LazyBackgroundObserver background_observer;
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII("wait_for_dialog");
const Extension* extension = LoadExtension(extdir);
ASSERT_TRUE(extension);
AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
ASSERT_TRUE(dialog);
EXPECT_TRUE(IsBackgroundPageAlive(extension->id()));
extensions::ProcessManager* pm =
extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
int previous_keep_alive_count = pm->GetLazyKeepaliveCount(extension);
dialog->CloseModalDialog();
EXPECT_EQ(previous_keep_alive_count - 1,
pm->GetLazyKeepaliveCount(extension));
background_observer.WaitUntilClosed();
EXPECT_FALSE(IsBackgroundPageAlive(extension->id()));
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForView) {
LazyBackgroundObserver page_complete;
ResultCatcher catcher;
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII("wait_for_view");
const Extension* extension = LoadExtension(extdir);
ASSERT_TRUE(extension);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_EQ(extension->GetResourceURL("extension_page.html").spec(),
browser()->tab_strip_model()->GetActiveWebContents()->
GetURL().spec());
EXPECT_TRUE(IsBackgroundPageAlive(last_loaded_extension_id()));
browser()->tab_strip_model()->CloseWebContentsAt(
browser()->tab_strip_model()->active_index(), TabStripModel::CLOSE_NONE);
page_complete.Wait();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForRequest) {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(StartEmbeddedTestServer());
LazyBackgroundObserver page_complete;
ResultCatcher catcher;
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII("wait_for_request");
const Extension* extension = LoadExtension(extdir);
ASSERT_TRUE(extension);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
extensions::ProcessManager* pm =
extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
extensions::ExtensionHost* host =
pm->GetBackgroundHostForExtension(last_loaded_extension_id());
ASSERT_TRUE(host);
bool result = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
host->render_view_host(), "abortRequest()", &result));
EXPECT_TRUE(result);
page_complete.Wait();
EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
}
#if defined(OS_MACOSX)
#define MAYBE_WaitForNTP DISABLED_WaitForNTP
#else
#define MAYBE_WaitForNTP WaitForNTP
#endif
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, MAYBE_WaitForNTP) {
LazyBackgroundObserver lazybg;
ResultCatcher catcher;
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII("wait_for_ntp");
const Extension* extension = LoadExtension(extdir);
ASSERT_TRUE(extension);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
EXPECT_TRUE(IsBackgroundPageAlive(last_loaded_extension_id()));
ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
lazybg.Wait();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
}
#if defined(OS_WIN)
#define MAYBE_IncognitoSplitMode DISABLED_IncognitoSplitMode
#else
#define MAYBE_IncognitoSplitMode IncognitoSplitMode
#endif
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, MAYBE_IncognitoSplitMode) {
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
browser()->profile(), GURL("about:blank"));
{
LoadedIncognitoObserver loaded(browser()->profile());
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII("incognito_split");
ASSERT_TRUE(LoadExtensionIncognito(extdir));
loaded.Wait();
}
extensions::ProcessManager* pm =
extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
extensions::ProcessManager* pmi =
extensions::ExtensionSystem::Get(incognito_browser->profile())->
process_manager();
EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
EXPECT_FALSE(pmi->GetBackgroundHostForExtension(last_loaded_extension_id()));
{
ExtensionTestMessageListener listener("waiting", false);
ExtensionTestMessageListener listener_incognito("waiting_incognito", false);
LazyBackgroundObserver page_complete(browser()->profile());
BrowserActionTestUtil(browser()).Press(0);
page_complete.Wait();
EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
EXPECT_FALSE(
pmi->GetBackgroundHostForExtension(last_loaded_extension_id()));
EXPECT_TRUE(listener.was_satisfied());
EXPECT_FALSE(listener_incognito.was_satisfied());
}
{
ExtensionTestMessageListener listener("waiting", false);
ExtensionTestMessageListener listener_incognito("waiting_incognito", false);
LazyBackgroundObserver page_complete(browser()->profile()),
page2_complete(incognito_browser->profile());
BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForProfile(browser()->profile());
test::WaitForBookmarkModelToLoad(bookmark_model);
const BookmarkNode* parent = bookmark_model->bookmark_bar_node();
bookmark_model->AddURL(
parent, 0, base::ASCIIToUTF16("Title"), GURL("about:blank"));
page_complete.Wait();
page2_complete.Wait();
EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
EXPECT_FALSE(
pmi->GetBackgroundHostForExtension(last_loaded_extension_id()));
EXPECT_TRUE(listener.was_satisfied());
EXPECT_TRUE(listener_incognito.was_satisfied());
}
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, Messaging) {
ASSERT_TRUE(StartEmbeddedTestServer());
ASSERT_TRUE(LoadExtensionAndWait("messaging"));
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
EXPECT_EQ(1, browser()->tab_strip_model()->count());
ResultCatcher catcher;
LazyBackgroundObserver lazybg;
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
lazybg.WaitUntilLoaded();
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_TRUE(IsBackgroundPageAlive(last_loaded_extension_id()));
ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
lazybg.WaitUntilClosed();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, ImpulseAddsCount) {
ASSERT_TRUE(StartEmbeddedTestServer());
const Extension* extension = LoadExtensionAndWait("messaging");
ASSERT_TRUE(extension);
extensions::ProcessManager* pm =
extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
EXPECT_EQ(1, browser()->tab_strip_model()->count());
ResultCatcher catcher;
LazyBackgroundObserver lazybg;
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
lazybg.WaitUntilLoaded();
int previous_keep_alive_count = pm->GetLazyKeepaliveCount(extension);
pm->KeepaliveImpulse(extension);
EXPECT_EQ(previous_keep_alive_count + 1,
pm->GetLazyKeepaliveCount(extension));
ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
lazybg.WaitUntilClosed();
EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnUnload) {
ASSERT_TRUE(LoadExtensionAndWait("on_unload"));
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
BrowserActionTestUtil browser_action(browser());
ASSERT_EQ(1, browser_action.NumberOfBrowserActions());
EXPECT_EQ("Success", browser_action.GetTooltip(0));
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, EventDispatchToTab) {
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
const extensions::Extension* extension =
LoadExtensionAndWait("event_dispatch_to_tab");
ExtensionTestMessageListener page_ready("ready", true);
GURL page_url = extension->GetResourceURL("page.html");
ui_test_utils::NavigateToURL(browser(), page_url);
EXPECT_TRUE(page_ready.WaitUntilSatisfied());
ExtensionTestMessageListener event_page_ready("ready", true);
BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForProfile(browser()->profile());
test::WaitForBookmarkModelToLoad(bookmark_model);
bookmark_utils::AddIfNotBookmarked(bookmark_model,
GURL("http://www.google.com"),
base::UTF8ToUTF16("Google"));
EXPECT_TRUE(event_page_ready.WaitUntilSatisfied());
page_ready.Reply("go");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, UpdateExtensionsPage) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIExtensionsURL));
ResultCatcher catcher;
base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
AppendASCII("wait_for_view");
const Extension* extension = LoadExtension(extdir);
ASSERT_TRUE(extension);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_EQ(extension->GetResourceURL("extension_page.html").spec(),
browser()->tab_strip_model()->GetActiveWebContents()->
GetURL().spec());
EXPECT_TRUE(IsBackgroundPageAlive(last_loaded_extension_id()));
LazyBackgroundObserver page_complete;
browser()->tab_strip_model()->CloseWebContentsAt(
browser()->tab_strip_model()->active_index(), TabStripModel::CLOSE_NONE);
page_complete.WaitUntilClosed();
EXPECT_FALSE(IsBackgroundPageAlive(last_loaded_extension_id()));
content::RenderFrameHost* frame = content::FrameMatchingPredicate(
browser()->tab_strip_model()->GetActiveWebContents(),
base::Bind(&content::FrameHasSourceUrl,
GURL(chrome::kChromeUIExtensionsFrameURL)));
bool is_inactive;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
frame,
"var ele = document.querySelectorAll('div.active-views');"
"window.domAutomationController.send("
" ele[0].innerHTML.search('(Inactive)') > 0);",
&is_inactive));
EXPECT_TRUE(is_inactive);
}
IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnSuspendUseStorageApi) {
EXPECT_TRUE(LoadExtensionAndWait("on_suspend"));
}