app_list_service_views_browsertest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 } 100 101 private: 102 DISALLOW_COPY_AND_ASSIGN(AppListControllerAppInfoDialogBrowserTest); 103}; 104 105// Test the DoShowAppInfoFlow function of the controller delegate. 106// Crashes on all platforms some of the time (flaky). http://crbug.com/378251 107IN_PROC_BROWSER_TEST_F(AppListControllerAppInfoDialogBrowserTest, 108 DoShowAppInfoFlow) { 109 // Install an extension to open the dialog for. 110 base::FilePath test_extension_path; 111 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_extension_path)); 112 test_extension_path = test_extension_path.AppendASCII("extensions") 113 .AppendASCII("platform_apps") 114 .AppendASCII("minimal"); 115 const extensions::Extension* extension = InstallExtension( 116 test_extension_path, 1 /* expected_change: new install */); 117 ASSERT_TRUE(extension); 118 119 // Open the app list window. 120 AppListService* service = test::GetAppListService(); 121 EXPECT_FALSE(service->GetAppListWindow()); 122 123 service->ShowForProfile(browser()->profile()); 124 gfx::NativeWindow window = service->GetAppListWindow(); 125 EXPECT_TRUE(window); 126 127 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); 128 ASSERT_TRUE(widget); 129 130 test::AppListViewTestApi test_api(GetAppListView(service)); 131 132 // Open the app info dialog. 133 views::Widget::Widgets owned_widgets; 134 widget->GetAllOwnedWidgets(window, &owned_widgets); 135 EXPECT_EQ(0U, owned_widgets.size()); 136 EXPECT_FALSE(test_api.is_overlay_visible()); 137 138 AppListControllerDelegate* controller = service->GetControllerDelegate(); 139 ASSERT_TRUE(controller); 140 controller->DoShowAppInfoFlow(browser()->profile(), extension->id()); 141 142 owned_widgets.clear(); 143 widget->GetAllOwnedWidgets(window, &owned_widgets); 144 EXPECT_EQ(1U, owned_widgets.size()); 145 EXPECT_TRUE(test_api.is_overlay_visible()); 146 147 // Close the app info dialog. 148 views::Widget* app_info_dialog = *owned_widgets.begin(); 149 app_info_dialog->CloseNow(); 150 151 owned_widgets.clear(); 152 widget->GetAllOwnedWidgets(window, &owned_widgets); 153 EXPECT_EQ(0U, owned_widgets.size()); 154 EXPECT_FALSE(test_api.is_overlay_visible()); 155} 156