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
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_apitest.h"
6
7#include "chrome/browser/extensions/lazy_background_page_test_util.h"
8#include "chrome/browser/notifications/desktop_notification_profile_util.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/ui/browser.h"
11#include "extensions/browser/process_manager.h"
12#include "extensions/common/extension.h"
13
14class NotificationIdleTest : public ExtensionApiTest {
15 protected:
16  virtual void SetUpOnMainThread() OVERRIDE {
17    ExtensionApiTest::SetUpOnMainThread();
18
19    extensions::ProcessManager::SetEventPageIdleTimeForTesting(1);
20    extensions::ProcessManager::SetEventPageSuspendingTimeForTesting(1);
21  }
22
23  const extensions::Extension* LoadExtensionAndWait(
24      const std::string& test_name) {
25    LazyBackgroundObserver page_complete;
26    base::FilePath extdir = test_data_dir_.AppendASCII(test_name);
27    const extensions::Extension* extension = LoadExtension(extdir);
28    if (extension)
29      page_complete.Wait();
30    return extension;
31  }
32};
33
34IN_PROC_BROWSER_TEST_F(ExtensionApiTest, NotificationsNoPermission) {
35  ASSERT_TRUE(RunExtensionTest("notifications/has_not_permission")) << message_;
36}
37
38IN_PROC_BROWSER_TEST_F(ExtensionApiTest, NotificationsHasPermission) {
39  DesktopNotificationProfileUtil::GrantPermission(browser()->profile(),
40      GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel"));
41
42  ASSERT_TRUE(RunExtensionTest("notifications/has_permission_prefs"))
43      << message_;
44}
45
46  // MessaceCenter-specific test.
47#if defined(RUN_MESSAGE_CENTER_TESTS)
48#define MAYBE_NotificationsAllowUnload NotificationsAllowUnload
49#else
50#define MAYBE_NotificationsAllowUnload DISABLED_NotificationsAllowUnload
51#endif
52
53IN_PROC_BROWSER_TEST_F(NotificationIdleTest, MAYBE_NotificationsAllowUnload) {
54  const extensions::Extension* extension =
55      LoadExtensionAndWait("notifications/api/unload");
56  ASSERT_TRUE(extension) << message_;
57
58  // Lazy Background Page has been shut down.
59  extensions::ProcessManager* pm =
60      extensions::ExtensionSystem::Get(profile())->process_manager();
61  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
62}
63