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
24// Browser Test for AppListService on Views platforms.
25typedef InProcessBrowserTest AppListServiceViewsBrowserTest;
26
27// Test closing the native app list window as if via a request from the OS.
28IN_PROC_BROWSER_TEST_F(AppListServiceViewsBrowserTest, NativeClose) {
29  AppListService* service = test::GetAppListService();
30  EXPECT_FALSE(service->GetAppListWindow());
31
32  // Since the profile is loaded, this will create a view immediately. This is
33  // important, because anything asynchronous would need an interactive_uitest
34  // due to the possibility of the app list being dismissed, and
35  // AppListService::GetAppListWindow returning NULL.
36  service->ShowForProfile(browser()->profile());
37  gfx::NativeWindow window = service->GetAppListWindow();
38  EXPECT_TRUE(window);
39
40  views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
41  EXPECT_TRUE(widget);
42  widget->Close();
43
44  // Close is asynchronous (dismiss is not) so sink the message queue.
45  base::RunLoop().RunUntilIdle();
46  EXPECT_FALSE(service->GetAppListWindow());
47
48  // Show again to get some code coverage for possibly stale pointers.
49  service->ShowForProfile(browser()->profile());
50  EXPECT_TRUE(service->GetAppListWindow());
51  service->DismissAppList();  // Note: in Ash, this will invalidate the window.
52
53  // Note: no need to sink message queue.
54  EXPECT_FALSE(service->GetAppListWindow());
55}
56
57// Browser Test for AppListController that ensures the App Info dialog opens
58// correctly.
59class AppListControllerAppInfoDialogBrowserTest : public ExtensionBrowserTest {
60 public:
61  AppListControllerAppInfoDialogBrowserTest() {}
62  virtual ~AppListControllerAppInfoDialogBrowserTest() {}
63
64  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
65    ExtensionBrowserTest::SetUpCommandLine(command_line);
66  }
67
68 private:
69  DISALLOW_COPY_AND_ASSIGN(AppListControllerAppInfoDialogBrowserTest);
70};
71
72// Test the DoShowAppInfoFlow function of the controller delegate.
73// Crashes on all platforms some of the time (flaky). http://crbug.com/378251
74IN_PROC_BROWSER_TEST_F(AppListControllerAppInfoDialogBrowserTest,
75                       DISABLED_DoShowAppInfoFlow) {
76  // Install an extension to open the dialog for.
77  base::FilePath test_extension_path;
78  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_extension_path));
79  test_extension_path = test_extension_path.AppendASCII("extensions")
80                            .AppendASCII("platform_apps")
81                            .AppendASCII("minimal");
82  const extensions::Extension* extension = InstallExtension(
83      test_extension_path, 1 /* expected_change: new install */);
84  ASSERT_TRUE(extension);
85
86  // Open the app list window.
87  AppListService* service = test::GetAppListService();
88  EXPECT_FALSE(service->GetAppListWindow());
89
90  service->ShowForProfile(browser()->profile());
91  gfx::NativeWindow window = service->GetAppListWindow();
92  EXPECT_TRUE(window);
93
94  views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
95  ASSERT_TRUE(widget);
96
97  // Open the app info dialog.
98  views::Widget::Widgets owned_widgets;
99  widget->GetAllOwnedWidgets(window, &owned_widgets);
100  EXPECT_EQ(0U, owned_widgets.size());
101
102  AppListControllerDelegate* controller = service->GetControllerDelegate();
103  ASSERT_TRUE(controller);
104  controller->DoShowAppInfoFlow(browser()->profile(), extension->id());
105  base::RunLoop().RunUntilIdle();
106
107  owned_widgets.clear();
108  widget->GetAllOwnedWidgets(window, &owned_widgets);
109  EXPECT_EQ(1U, owned_widgets.size());
110
111  // Close the app info dialog.
112  views::Widget* app_info_dialog = *owned_widgets.begin();
113  app_info_dialog->Close();
114  base::RunLoop().RunUntilIdle();
115
116  owned_widgets.clear();
117  widget->GetAllOwnedWidgets(window, &owned_widgets);
118  EXPECT_EQ(0U, owned_widgets.size());
119}
120