app_list_service_views_browsertest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2014 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/ui/app_list/app_list_service_views.h"
6
7#include "base/path_service.h"
8#include "base/run_loop.h"
9#include "chrome/browser/extensions/extension_browsertest.h"
10#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
11#include "chrome/browser/ui/app_list/test/chrome_app_list_test_support.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/common/chrome_paths.h"
14#include "chrome/test/base/in_process_browser_test.h"
15#include "ui/app_list/app_list_switches.h"
16#include "ui/app_list/views/app_list_view.h"
17#include "ui/views/widget/widget.h"
18
19#if defined(OS_CHROMEOS)
20#include "ash/shell.h"
21#include "ash/test/app_list_controller_test_api.h"
22#endif
23
24namespace {
25
26app_list::AppListView* GetAppListView(AppListService* service) {
27#if defined(OS_CHROMEOS)
28  return ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).view();
29#else
30  return static_cast<AppListServiceViews*>(service)->shower().app_list();
31#endif
32}
33
34}  // namespace
35
36namespace test {
37
38// Allow access to private variables of the AppListView for testing.
39class AppListViewTestApi {
40 public:
41  explicit AppListViewTestApi(app_list::AppListView* view) : view_(view) {}
42  virtual ~AppListViewTestApi() {}
43
44  bool is_overlay_visible() {
45    DCHECK(view_->overlay_view_);
46    return view_->overlay_view_->visible();
47  }
48
49 private:
50  app_list::AppListView* view_;
51
52  DISALLOW_COPY_AND_ASSIGN(AppListViewTestApi);
53};
54
55}  // namespace test
56
57// Browser Test for AppListService on Views platforms.
58typedef InProcessBrowserTest AppListServiceViewsBrowserTest;
59
60// Test closing the native app list window as if via a request from the OS.
61IN_PROC_BROWSER_TEST_F(AppListServiceViewsBrowserTest, NativeClose) {
62  AppListService* service = test::GetAppListService();
63  EXPECT_FALSE(service->GetAppListWindow());
64
65  // Since the profile is loaded, this will create a view immediately. This is
66  // important, because anything asynchronous would need an interactive_uitest
67  // due to the possibility of the app list being dismissed, and
68  // AppListService::GetAppListWindow returning NULL.
69  service->ShowForProfile(browser()->profile());
70  gfx::NativeWindow window = service->GetAppListWindow();
71  EXPECT_TRUE(window);
72
73  views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
74  EXPECT_TRUE(widget);
75  widget->Close();
76
77  // Close is asynchronous (dismiss is not) so sink the message queue.
78  base::RunLoop().RunUntilIdle();
79  EXPECT_FALSE(service->GetAppListWindow());
80
81  // Show again to get some code coverage for possibly stale pointers.
82  service->ShowForProfile(browser()->profile());
83  EXPECT_TRUE(service->GetAppListWindow());
84  service->DismissAppList();  // Note: in Ash, this will invalidate the window.
85
86  // Note: no need to sink message queue.
87  EXPECT_FALSE(service->GetAppListWindow());
88}
89
90// Browser Test for AppListController that ensures the App Info dialog opens
91// correctly.
92class AppListControllerAppInfoDialogBrowserTest : public ExtensionBrowserTest {
93 public:
94  AppListControllerAppInfoDialogBrowserTest() {}
95  virtual ~AppListControllerAppInfoDialogBrowserTest() {}
96
97  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
98    ExtensionBrowserTest::SetUpCommandLine(command_line);
99    command_line->AppendSwitch(app_list::switches::kEnableAppInfo);
100  }
101
102 private:
103  DISALLOW_COPY_AND_ASSIGN(AppListControllerAppInfoDialogBrowserTest);
104};
105
106// Test the DoShowAppInfoFlow function of the controller delegate.
107IN_PROC_BROWSER_TEST_F(AppListControllerAppInfoDialogBrowserTest,
108                       DoShowAppInfoFlow) {
109  // Install an extension to open the dialog for.
110  test::SigninProfile(browser()->profile());
111  base::FilePath test_extension_path;
112  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_extension_path));
113  test_extension_path = test_extension_path.AppendASCII("extensions")
114                            .AppendASCII("platform_apps")
115                            .AppendASCII("minimal");
116  const extensions::Extension* extension = InstallExtension(
117      test_extension_path, 1 /* expected_change: new install */);
118  ASSERT_TRUE(extension);
119
120  // Open the app list window.
121  AppListService* service = test::GetAppListService();
122  EXPECT_FALSE(service->GetAppListWindow());
123
124  service->ShowForProfile(browser()->profile());
125  gfx::NativeWindow window = service->GetAppListWindow();
126  EXPECT_TRUE(window);
127
128  views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
129  ASSERT_TRUE(widget);
130
131  test::AppListViewTestApi test_api(GetAppListView(service));
132
133  // Open the app info dialog.
134  views::Widget::Widgets owned_widgets;
135  widget->GetAllOwnedWidgets(window, &owned_widgets);
136  EXPECT_EQ(0U, owned_widgets.size());
137  EXPECT_FALSE(test_api.is_overlay_visible());
138
139  AppListControllerDelegate* controller = service->GetControllerDelegate();
140  ASSERT_TRUE(controller);
141  controller->DoShowAppInfoFlow(browser()->profile(), extension->id());
142  base::RunLoop().RunUntilIdle();
143
144  owned_widgets.clear();
145  widget->GetAllOwnedWidgets(window, &owned_widgets);
146  EXPECT_EQ(1U, owned_widgets.size());
147  EXPECT_TRUE(test_api.is_overlay_visible());
148
149  // Close the app info dialog.
150  views::Widget* app_info_dialog = *owned_widgets.begin();
151  app_info_dialog->Close();
152  base::RunLoop().RunUntilIdle();
153
154  owned_widgets.clear();
155  widget->GetAllOwnedWidgets(window, &owned_widgets);
156  EXPECT_EQ(0U, owned_widgets.size());
157  EXPECT_FALSE(test_api.is_overlay_visible());
158}
159