• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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"
23 
24 // Browser Test for AppListController that runs on all platforms supporting
25 // app_list.
26 typedef InProcessBrowserTest AppListControllerBrowserTest;
27 
28 // Test the CreateNewWindow function of the controller delegate.
IN_PROC_BROWSER_TEST_F(AppListControllerBrowserTest,CreateNewWindow)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);
34 
35   EXPECT_EQ(1U, chrome::GetBrowserCount(browser()->profile(), desktop));
36   EXPECT_EQ(0U, chrome::GetBrowserCount(
37       browser()->profile()->GetOffTheRecordProfile(), desktop));
38 
39   controller->CreateNewWindow(browser()->profile(), false);
40   EXPECT_EQ(2U, chrome::GetBrowserCount(browser()->profile(), desktop));
41 
42   controller->CreateNewWindow(browser()->profile(), true);
43   EXPECT_EQ(1U, chrome::GetBrowserCount(
44       browser()->profile()->GetOffTheRecordProfile(), desktop));
45 }
46 
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 {
52  public:
AppListControllerSearchResultsBrowserTest()53   AppListControllerSearchResultsBrowserTest()
54       : observed_result_(NULL),
55         item_uninstall_count_(0),
56         observed_results_list_(NULL) {}
57 
WatchResultsLookingForItem(app_list::AppListModel::SearchResults * search_results,const std::string & extension_name)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);
65   }
66 
StopWatchingResults()67   void StopWatchingResults() {
68     EXPECT_TRUE(observed_results_list_);
69     observed_results_list_->RemoveObserver(this);
70   }
71 
72  protected:
AttemptToLocateItem()73   void AttemptToLocateItem() {
74     if (observed_result_) {
75       observed_result_->RemoveObserver(this);
76       observed_result_ = NULL;
77     }
78 
79     for (size_t i = 0; i < observed_results_list_->item_count(); ++i) {
80       if (observed_results_list_->GetItemAt(i)->title() != item_to_observe_)
81         continue;
82 
83       // Ensure there is at most one.
84       EXPECT_FALSE(observed_result_);
85       observed_result_ = observed_results_list_->GetItemAt(i);
86     }
87 
88     if (observed_result_)
89       observed_result_->AddObserver(this);
90   }
91 
92   // Overridden from SearchResultObserver:
OnIconChanged()93   virtual void OnIconChanged() OVERRIDE {}
OnActionsChanged()94   virtual void OnActionsChanged() OVERRIDE {}
OnIsInstallingChanged()95   virtual void OnIsInstallingChanged() OVERRIDE {}
OnPercentDownloadedChanged()96   virtual void OnPercentDownloadedChanged() OVERRIDE {}
OnItemInstalled()97   virtual void OnItemInstalled() OVERRIDE {}
OnItemUninstalled()98   virtual void OnItemUninstalled() OVERRIDE {
99     ++item_uninstall_count_;
100     EXPECT_TRUE(observed_result_);
101   }
102 
103   // Overridden from ui::ListModelObserver:
ListItemsAdded(size_t start,size_t count)104   virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE {
105     AttemptToLocateItem();
106   }
ListItemsRemoved(size_t start,size_t count)107   virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE {
108     AttemptToLocateItem();
109   }
ListItemMoved(size_t index,size_t target_index)110   virtual void ListItemMoved(size_t index, size_t target_index) OVERRIDE {}
ListItemsChanged(size_t start,size_t count)111   virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE {}
112 
113   app_list::SearchResult* observed_result_;
114   int item_uninstall_count_;
115 
116  private:
117   base::string16 item_to_observe_;
118   app_list::AppListModel::SearchResults* observed_results_list_;
119 
120   DISALLOW_COPY_AND_ASSIGN(AppListControllerSearchResultsBrowserTest);
121 };
122 
123 // Test showing search results, and uninstalling one of them while displayed.
IN_PROC_BROWSER_TEST_F(AppListControllerSearchResultsBrowserTest,UninstallSearchResult)124 IN_PROC_BROWSER_TEST_F(AppListControllerSearchResultsBrowserTest,
125                        UninstallSearchResult) {
126   base::FilePath test_extension_path;
127   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_extension_path));
128   test_extension_path = test_extension_path.AppendASCII("extensions")
129       .AppendASCII("platform_apps")
130       .AppendASCII("minimal");
131   const extensions::Extension* extension =
132       InstallExtension(test_extension_path,
133                        1 /* expected_change: new install */);
134   ASSERT_TRUE(extension);
135 
136   AppListService* service = test::GetAppListService();
137   ASSERT_TRUE(service);
138   service->ShowForProfile(browser()->profile());
139 
140   app_list::AppListModel* model = test::GetAppListModel(service);
141   ASSERT_TRUE(model);
142   WatchResultsLookingForItem(model->results(), extension->name());
143 
144   // Ensure a search finds the extension.
145   EXPECT_FALSE(observed_result_);
146   model->search_box()->SetText(base::ASCIIToUTF16("minimal"));
147   EXPECT_TRUE(observed_result_);
148 
149   // Ensure the UI is updated. This is via PostTask in views.
150   base::RunLoop().RunUntilIdle();
151 
152   // Now uninstall and ensure this browser test observes it.
153   EXPECT_EQ(0, item_uninstall_count_);
154   UninstallExtension(extension->id());
155   EXPECT_EQ(1, item_uninstall_count_);
156 
157   // Results should not be immediately refreshed. When they are, the item should
158   // be removed from the model.
159   EXPECT_TRUE(observed_result_);
160   base::RunLoop().RunUntilIdle();
161   EXPECT_FALSE(observed_result_);
162   StopWatchingResults();
163   service->DismissAppList();
164 }
165