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.
5 #include "base/path_service.h"
6 #include "base/run_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
11 #include "chrome/browser/ui/app_list/app_list_service.h"
12 #include "chrome/browser/ui/app_list/test/chrome_app_list_test_support.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/host_desktop.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "ui/app_list/app_list_model.h"
19 #include "ui/app_list/search_box_model.h"
20 #include "ui/app_list/search_result.h"
21 #include "ui/app_list/search_result_observer.h"
22 #include "ui/base/models/list_model_observer.h"
24 // Browser Test for AppListController that runs on all platforms supporting
26 typedef InProcessBrowserTest AppListControllerBrowserTest
;
28 // Test the CreateNewWindow function of the controller delegate.
29 IN_PROC_BROWSER_TEST_F(AppListControllerBrowserTest
, CreateNewWindow
) {
30 const chrome::HostDesktopType desktop
= chrome::GetActiveDesktop();
31 AppListService
* service
= test::GetAppListService();
32 AppListControllerDelegate
* controller(service
->GetControllerDelegate());
33 ASSERT_TRUE(controller
);
35 EXPECT_EQ(1U, chrome::GetBrowserCount(browser()->profile(), desktop
));
36 EXPECT_EQ(0U, chrome::GetBrowserCount(
37 browser()->profile()->GetOffTheRecordProfile(), desktop
));
39 controller
->CreateNewWindow(browser()->profile(), false);
40 EXPECT_EQ(2U, chrome::GetBrowserCount(browser()->profile(), desktop
));
42 controller
->CreateNewWindow(browser()->profile(), true);
43 EXPECT_EQ(1U, chrome::GetBrowserCount(
44 browser()->profile()->GetOffTheRecordProfile(), desktop
));
47 // Browser Test for AppListController that observes search result changes.
48 class AppListControllerSearchResultsBrowserTest
49 : public ExtensionBrowserTest
,
50 public app_list::SearchResultObserver
,
51 public ui::ListModelObserver
{
53 AppListControllerSearchResultsBrowserTest()
54 : observed_result_(NULL
),
55 item_uninstall_count_(0),
56 observed_results_list_(NULL
) {}
58 void WatchResultsLookingForItem(
59 app_list::AppListModel::SearchResults
* search_results
,
60 const std::string
& extension_name
) {
61 EXPECT_FALSE(observed_results_list_
);
62 observed_results_list_
= search_results
;
63 observed_results_list_
->AddObserver(this);
64 item_to_observe_
= base::ASCIIToUTF16(extension_name
);
67 void StopWatchingResults() {
68 EXPECT_TRUE(observed_results_list_
);
69 observed_results_list_
->RemoveObserver(this);
73 void AttemptToLocateItem() {
74 if (observed_result_
) {
75 observed_result_
->RemoveObserver(this);
76 observed_result_
= NULL
;
79 for (size_t i
= 0; i
< observed_results_list_
->item_count(); ++i
) {
80 if (observed_results_list_
->GetItemAt(i
)->title() != item_to_observe_
)
83 // Ensure there is at most one.
84 EXPECT_FALSE(observed_result_
);
85 observed_result_
= observed_results_list_
->GetItemAt(i
);
89 observed_result_
->AddObserver(this);
92 // Overridden from SearchResultObserver:
93 virtual void OnIconChanged() OVERRIDE
{}
94 virtual void OnActionsChanged() OVERRIDE
{}
95 virtual void OnIsInstallingChanged() OVERRIDE
{}
96 virtual void OnPercentDownloadedChanged() OVERRIDE
{}
97 virtual void OnItemInstalled() OVERRIDE
{}
98 virtual void OnItemUninstalled() OVERRIDE
{
99 ++item_uninstall_count_
;
100 EXPECT_TRUE(observed_result_
);
103 // Overridden from ui::ListModelObserver:
104 virtual void ListItemsAdded(size_t start
, size_t count
) OVERRIDE
{
105 AttemptToLocateItem();
107 virtual void ListItemsRemoved(size_t start
, size_t count
) OVERRIDE
{
108 AttemptToLocateItem();
110 virtual void ListItemMoved(size_t index
, size_t target_index
) OVERRIDE
{}
111 virtual void ListItemsChanged(size_t start
, size_t count
) OVERRIDE
{}
113 app_list::SearchResult
* observed_result_
;
114 int item_uninstall_count_
;
117 base::string16 item_to_observe_
;
118 app_list::AppListModel::SearchResults
* observed_results_list_
;
120 DISALLOW_COPY_AND_ASSIGN(AppListControllerSearchResultsBrowserTest
);
123 // Test showing search results, and uninstalling one of them while displayed.
124 IN_PROC_BROWSER_TEST_F(AppListControllerSearchResultsBrowserTest
,
125 UninstallSearchResult
) {
126 test::SigninProfile(browser()->profile());
127 base::FilePath test_extension_path
;
128 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_extension_path
));
129 test_extension_path
= test_extension_path
.AppendASCII("extensions")
130 .AppendASCII("platform_apps")
131 .AppendASCII("minimal");
132 const extensions::Extension
* extension
=
133 InstallExtension(test_extension_path
,
134 1 /* expected_change: new install */);
135 ASSERT_TRUE(extension
);
137 AppListService
* service
= test::GetAppListService();
138 ASSERT_TRUE(service
);
139 service
->ShowForProfile(browser()->profile());
141 app_list::AppListModel
* model
= test::GetAppListModel(service
);
143 WatchResultsLookingForItem(model
->results(), extension
->name());
145 // Ensure a search finds the extension.
146 EXPECT_FALSE(observed_result_
);
147 model
->search_box()->SetText(base::ASCIIToUTF16("minimal"));
148 EXPECT_TRUE(observed_result_
);
150 // Ensure the UI is updated. This is via PostTask in views.
151 base::RunLoop().RunUntilIdle();
153 // Now uninstall and ensure this browser test observes it.
154 EXPECT_EQ(0, item_uninstall_count_
);
155 UninstallExtension(extension
->id());
156 EXPECT_EQ(1, item_uninstall_count_
);
158 // Results should not be immediately refreshed. When they are, the item should
159 // be removed from the model.
160 EXPECT_TRUE(observed_result_
);
161 base::RunLoop().RunUntilIdle();
162 EXPECT_FALSE(observed_result_
);
163 StopWatchingResults();
164 service
->DismissAppList();