1// Copyright 2013 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#import "chrome/browser/ui/app_list/app_list_service_mac.h"
6
7#include <vector>
8
9#include "base/command_line.h"
10#include "chrome/browser/apps/app_shim/app_shim_handler_mac.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/common/chrome_switches.h"
13#include "chrome/common/mac/app_mode_common.h"
14#include "chrome/test/base/in_process_browser_test.h"
15#import "ui/app_list/cocoa/app_list_window_controller.h"
16
17using apps::AppShimHandler;
18
19namespace test {
20
21class AppListServiceMacTestApi {
22 public:
23  static AppListWindowController* window_controller() {
24    return AppListServiceMac::GetInstance()->window_controller_;
25  }
26};
27
28}  // namespace test
29
30namespace {
31
32// Browser test for mac-specific AppListService functionality.
33class AppListServiceMacInteractiveTest : public InProcessBrowserTest,
34                                         public AppShimHandler::Host {
35 public:
36  AppListServiceMacInteractiveTest() : launch_count_(0) {}
37
38 protected:
39  void LaunchShim() {
40    // AppList shims always launch normally (never relaunched via dock icon).
41    AppShimHandler::GetForAppMode(app_mode::kAppListModeId)->
42        OnShimLaunch(this,
43                     apps::APP_SHIM_LAUNCH_NORMAL,
44                     std::vector<base::FilePath>());
45  }
46
47  AppListViewController* GetViewController() {
48    return [test::AppListServiceMacTestApi::window_controller()
49        appListViewController];
50  }
51
52  // testing::Test overrides:
53  virtual void TearDown() OVERRIDE {
54    // At tear-down, NOTIFICATION_APP_TERMINATING should have been sent for the
55    // browser shutdown. References to browser-owned objects must be removed
56    // from the app list UI.
57    AppListViewController* view_controller = GetViewController();
58    // Note this first check will fail if the test doesn't ever show the
59    // app list, but currently all tests in this file do.
60    EXPECT_TRUE(view_controller);
61    EXPECT_FALSE([view_controller delegate]);
62  }
63
64  // AppShimHandler::Host overrides:
65  virtual void OnAppLaunchComplete(apps::AppShimLaunchResult result) OVERRIDE {
66    // AppList shims are always given APP_SHIM_LAUNCH_DUPLICATE_HOST, indicating
67    // that the shim process should immediately close.
68    EXPECT_EQ(apps::APP_SHIM_LAUNCH_DUPLICATE_HOST, result);
69    ++launch_count_;
70  }
71  virtual void OnAppClosed() OVERRIDE {
72    NOTREACHED();
73  }
74  virtual void OnAppHide() OVERRIDE {}
75  virtual void OnAppRequestUserAttention(
76      apps::AppShimAttentionType type) OVERRIDE {}
77  virtual base::FilePath GetProfilePath() const OVERRIDE {
78    NOTREACHED();  // Currently unused in this test.
79    return base::FilePath();
80  }
81  virtual std::string GetAppId() const OVERRIDE {
82    return app_mode::kAppListModeId;
83  }
84
85  int launch_count_;
86
87 private:
88  DISALLOW_COPY_AND_ASSIGN(AppListServiceMacInteractiveTest);
89};
90
91}  // namespace
92
93IN_PROC_BROWSER_TEST_F(AppListServiceMacInteractiveTest, ShowAppListUsingShim) {
94  // Check that AppListService has registered as a shim handler for "app_list".
95  EXPECT_TRUE(AppShimHandler::GetForAppMode(app_mode::kAppListModeId));
96
97  AppListService* service =
98      AppListService::Get(chrome::HOST_DESKTOP_TYPE_NATIVE);
99  EXPECT_FALSE(service->IsAppListVisible());
100
101  // Creation should be lazy.
102  EXPECT_FALSE(GetViewController());
103
104  // With no saved profile, the default profile should be chosen and saved.
105  service->Show();
106  EXPECT_EQ(browser()->profile(), service->GetCurrentAppListProfile());
107  EXPECT_TRUE(service->IsAppListVisible());
108  EXPECT_EQ(0, launch_count_);
109  service->DismissAppList();
110  EXPECT_FALSE(service->IsAppListVisible());
111
112  // Test showing the app list via the shim. The shim should immediately close
113  // leaving the app list visible.
114  LaunchShim();
115  EXPECT_TRUE(service->IsAppListVisible());
116  EXPECT_EQ(1, launch_count_);
117
118  // Launching again should toggle the app list.
119  LaunchShim();
120  EXPECT_FALSE(service->IsAppListVisible());
121  EXPECT_EQ(2, launch_count_);
122
123  // Test showing the app list via the shim, then dismissing the app list.
124  LaunchShim();
125  EXPECT_TRUE(service->IsAppListVisible());
126  EXPECT_EQ(3, launch_count_);
127  service->DismissAppList();
128  EXPECT_FALSE(service->IsAppListVisible());
129
130  // View sticks around until shutdown.
131  AppListViewController* view_controller = GetViewController();
132  EXPECT_TRUE(view_controller);
133  EXPECT_TRUE([view_controller delegate]);
134}
135