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/api/system_indicator/system_indicator_manager.h"
6#include "chrome/browser/extensions/api/system_indicator/system_indicator_manager_factory.h"
7#include "chrome/browser/extensions/extension_action.h"
8#include "chrome/browser/extensions/extension_action_manager.h"
9#include "chrome/browser/extensions/extension_apitest.h"
10#include "chrome/browser/extensions/lazy_background_page_test_util.h"
11#include "chrome/browser/ui/browser.h"
12#include "extensions/browser/extension_system.h"
13#include "extensions/browser/process_manager.h"
14#include "extensions/common/extension.h"
15#include "extensions/test/result_catcher.h"
16
17class SystemIndicatorApiTest : public ExtensionApiTest {
18 public:
19  virtual void SetUpOnMainThread() OVERRIDE {
20    ExtensionApiTest::SetUpOnMainThread();
21    // Set shorter delays to prevent test timeouts in tests that need to wait
22    // for the event page to unload.
23    extensions::ProcessManager::SetEventPageIdleTimeForTesting(1);
24    extensions::ProcessManager::SetEventPageSuspendingTimeForTesting(1);
25  }
26
27  const extensions::Extension* LoadExtensionAndWait(
28      const std::string& test_name) {
29    LazyBackgroundObserver page_complete;
30    base::FilePath extdir = test_data_dir_.AppendASCII(test_name);
31    const extensions::Extension* extension = LoadExtension(extdir);
32    if (extension)
33      page_complete.Wait();
34    return extension;
35  }
36};
37
38IN_PROC_BROWSER_TEST_F(ExtensionApiTest, SystemIndicator) {
39  // Only run this test on supported platforms.  SystemIndicatorManagerFactory
40  // returns NULL on unsupported platforms.
41  extensions::SystemIndicatorManager* manager =
42      extensions::SystemIndicatorManagerFactory::GetForProfile(profile());
43  if (manager) {
44    ASSERT_TRUE(RunExtensionTest("system_indicator/basics")) << message_;
45  }
46}
47
48IN_PROC_BROWSER_TEST_F(SystemIndicatorApiTest, SystemIndicator) {
49  // Only run this test on supported platforms.  SystemIndicatorManagerFactory
50  // returns NULL on unsupported platforms.
51  extensions::SystemIndicatorManager* manager =
52      extensions::SystemIndicatorManagerFactory::GetForProfile(profile());
53  if (manager) {
54    extensions::ResultCatcher catcher;
55
56    const extensions::Extension* extension =
57        LoadExtensionAndWait("system_indicator/unloaded");
58    ASSERT_TRUE(extension) << message_;
59
60    // Lazy Background Page has been shut down.
61    extensions::ProcessManager* pm =
62        extensions::ExtensionSystem::Get(profile())->process_manager();
63    EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id()));
64
65    EXPECT_TRUE(manager->SendClickEventToExtensionForTest(extension->id()));
66    EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
67  }
68}
69