extension_app_model_builder_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 2012 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/extension_app_model_builder.h" 6 7#include <string> 8 9#include "base/files/file_path.h" 10#include "base/memory/scoped_ptr.h" 11#include "base/prefs/pref_service.h" 12#include "base/run_loop.h" 13#include "base/values.h" 14#include "chrome/browser/extensions/extension_function_test_utils.h" 15#include "chrome/browser/extensions/extension_service_unittest.h" 16#include "chrome/browser/extensions/install_tracker.h" 17#include "chrome/browser/extensions/install_tracker_factory.h" 18#include "chrome/browser/ui/app_list/app_list_controller_delegate_impl.h" 19#include "chrome/common/extensions/extension_constants.h" 20#include "chrome/common/pref_names.h" 21#include "chrome/test/base/testing_profile.h" 22#include "extensions/browser/app_sorting.h" 23#include "extensions/common/manifest.h" 24#include "testing/gtest/include/gtest/gtest.h" 25#include "ui/app_list/app_list_item_model.h" 26 27namespace { 28 29const char kHostedAppId[] = "dceacbkfkmllgmjmbhgkpjegnodmildf"; 30const char kPackagedApp1Id[] = "emfkafnhnpcmabnnkckkchdilgeoekbo"; 31const char kPackagedApp2Id[] = "jlklkagmeajbjiobondfhiekepofmljl"; 32 33// Get a string of all apps in |model| joined with ','. 34std::string GetModelContent(app_list::AppListModel* model) { 35 std::string content; 36 for (size_t i = 0; i < model->item_list()->item_count(); ++i) { 37 if (i > 0) 38 content += ','; 39 content += model->item_list()->item_at(i)->title(); 40 } 41 return content; 42} 43 44scoped_refptr<extensions::Extension> MakeApp(const std::string& name, 45 const std::string& version, 46 const std::string& url, 47 const std::string& id) { 48 std::string err; 49 DictionaryValue value; 50 value.SetString("name", name); 51 value.SetString("version", version); 52 value.SetString("app.launch.web_url", url); 53 scoped_refptr<extensions::Extension> app = 54 extensions::Extension::Create( 55 base::FilePath(), 56 extensions::Manifest::INTERNAL, 57 value, 58 extensions::Extension::WAS_INSTALLED_BY_DEFAULT, 59 id, 60 &err); 61 EXPECT_EQ(err, ""); 62 return app; 63} 64 65class TestAppListControllerDelegate : public AppListControllerDelegate { 66 public: 67 virtual ~TestAppListControllerDelegate() {} 68 virtual void DismissView() OVERRIDE {} 69 virtual gfx::NativeWindow GetAppListWindow() OVERRIDE { return NULL; } 70 virtual gfx::ImageSkia GetWindowIcon() OVERRIDE { return gfx::ImageSkia(); } 71 virtual bool IsAppPinned(const std::string& extension_id) OVERRIDE { 72 return false; 73 } 74 virtual void PinApp(const std::string& extension_id) OVERRIDE {} 75 virtual void UnpinApp(const std::string& extension_id) OVERRIDE {} 76 virtual Pinnable GetPinnable() OVERRIDE { return NO_PIN; } 77 virtual bool CanDoCreateShortcutsFlow() OVERRIDE { return false; } 78 virtual void DoCreateShortcutsFlow(Profile* profile, 79 const std::string& extension_id) OVERRIDE { 80 } 81 virtual void CreateNewWindow(Profile* profile, bool incognito) OVERRIDE {} 82 virtual void ActivateApp(Profile* profile, 83 const extensions::Extension* extension, 84 AppListSource source, 85 int event_flags) OVERRIDE {} 86 virtual void LaunchApp(Profile* profile, 87 const extensions::Extension* extension, 88 AppListSource source, 89 int event_flags) OVERRIDE {} 90 virtual void ShowForProfileByPath( 91 const base::FilePath& profile_path) OVERRIDE {} 92 virtual bool ShouldShowUserIcon() OVERRIDE { return false; } 93}; 94 95const char kDefaultApps[] = "Packaged App 1,Packaged App 2,Hosted App"; 96const size_t kDefaultAppCount = 3u; 97 98} // namespace 99 100class ExtensionAppModelBuilderTest : public ExtensionServiceTestBase { 101 public: 102 ExtensionAppModelBuilderTest() {} 103 virtual ~ExtensionAppModelBuilderTest() {} 104 105 virtual void SetUp() OVERRIDE { 106 ExtensionServiceTestBase::SetUp(); 107 108 // Load "app_list" extensions test profile. 109 // The test profile has 4 extensions: 110 // 1 dummy extension, 2 packaged extension apps and 1 hosted extension app. 111 base::FilePath source_install_dir = data_dir_ 112 .AppendASCII("app_list") 113 .AppendASCII("Extensions"); 114 base::FilePath pref_path = source_install_dir 115 .DirName() 116 .AppendASCII("Preferences"); 117 InitializeInstalledExtensionService(pref_path, source_install_dir); 118 service_->Init(); 119 120 // There should be 4 extensions in the test profile. 121 const ExtensionSet* extensions = service_->extensions(); 122 ASSERT_EQ(static_cast<size_t>(4), extensions->size()); 123 124 model_.reset(new app_list::AppListModel); 125 controller_.reset(new TestAppListControllerDelegate); 126 builder_.reset(new ExtensionAppModelBuilder( 127 profile_.get(), model_.get(), controller_.get())); 128 } 129 130 virtual void TearDown() OVERRIDE { 131 builder_.reset(); 132 controller_.reset(); 133 model_.reset(); 134 } 135 136 protected: 137 scoped_ptr<app_list::AppListModel> model_; 138 scoped_ptr<TestAppListControllerDelegate> controller_; 139 scoped_ptr<ExtensionAppModelBuilder> builder_; 140 141 base::ScopedTempDir second_profile_temp_dir_; 142 143 private: 144 DISALLOW_COPY_AND_ASSIGN(ExtensionAppModelBuilderTest); 145}; 146 147TEST_F(ExtensionAppModelBuilderTest, Build) { 148 // The apps list would have 3 extension apps in the profile. 149 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 150} 151 152TEST_F(ExtensionAppModelBuilderTest, HideWebStore) { 153 // Install a "web store" app. 154 scoped_refptr<extensions::Extension> store = 155 MakeApp("webstore", 156 "0.0", 157 "http://google.com", 158 std::string(extension_misc::kWebStoreAppId)); 159 service_->AddExtension(store.get()); 160 161 // Install an "enterprise web store" app. 162 scoped_refptr<extensions::Extension> enterprise_store = 163 MakeApp("enterprise_webstore", 164 "0.0", 165 "http://google.com", 166 std::string(extension_misc::kEnterpriseWebStoreAppId)); 167 service_->AddExtension(enterprise_store.get()); 168 169 // Web stores should be present in the AppListModel. 170 app_list::AppListModel model1; 171 ExtensionAppModelBuilder builder1( 172 profile_.get(), &model1, controller_.get()); 173 std::string content = GetModelContent(&model1); 174 EXPECT_NE(std::string::npos, content.find("webstore")); 175 EXPECT_NE(std::string::npos, content.find("enterprise_webstore")); 176 177 // Activate the HideWebStoreIcon policy. 178 profile_->GetPrefs()->SetBoolean(prefs::kHideWebStoreIcon, true); 179 180 // Web stores should NOT be in the AppListModel. 181 app_list::AppListModel model2; 182 ExtensionAppModelBuilder builder2( 183 profile_.get(), &model2, controller_.get()); 184 content = GetModelContent(&model2); 185 EXPECT_EQ(std::string::npos, content.find("webstore")); 186 EXPECT_EQ(std::string::npos, content.find("enterprise_webstore")); 187} 188 189TEST_F(ExtensionAppModelBuilderTest, DisableAndEnable) { 190 service_->DisableExtension(kHostedAppId, 191 extensions::Extension::DISABLE_NONE); 192 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 193 194 service_->EnableExtension(kHostedAppId); 195 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 196} 197 198TEST_F(ExtensionAppModelBuilderTest, Uninstall) { 199 service_->UninstallExtension(kPackagedApp2Id, false, NULL); 200 EXPECT_EQ(std::string("Packaged App 1,Hosted App"), 201 GetModelContent(model_.get())); 202 203 base::RunLoop().RunUntilIdle(); 204} 205 206TEST_F(ExtensionAppModelBuilderTest, UninstallTerminatedApp) { 207 const extensions::Extension* app = 208 service_->GetInstalledExtension(kPackagedApp2Id); 209 ASSERT_TRUE(app != NULL); 210 211 // Simulate an app termination. 212 service_->TrackTerminatedExtensionForTest(app); 213 214 service_->UninstallExtension(kPackagedApp2Id, false, NULL); 215 EXPECT_EQ(std::string("Packaged App 1,Hosted App"), 216 GetModelContent(model_.get())); 217 218 base::RunLoop().RunUntilIdle(); 219} 220 221TEST_F(ExtensionAppModelBuilderTest, Reinstall) { 222 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 223 224 // Install kPackagedApp1Id again should not create a new entry. 225 extensions::InstallTracker* tracker = 226 extensions::InstallTrackerFactory::GetForProfile(profile_.get()); 227 extensions::InstallObserver::ExtensionInstallParams params( 228 kPackagedApp1Id, "", gfx::ImageSkia(), true, true); 229 tracker->OnBeginExtensionInstall(params); 230 231 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 232} 233 234TEST_F(ExtensionAppModelBuilderTest, OrdinalPrefsChange) { 235 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 236 237 syncer::StringOrdinal package_app_page = 238 sorting->GetPageOrdinal(kPackagedApp1Id); 239 sorting->SetPageOrdinal(kHostedAppId, package_app_page.CreateBefore()); 240 // Old behavior: This would be "Hosted App,Packaged App 1,Packaged App 2" 241 // New behavior: Sorting order doesn't change. 242 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 243 244 syncer::StringOrdinal app1_ordinal = 245 sorting->GetAppLaunchOrdinal(kPackagedApp1Id); 246 syncer::StringOrdinal app2_ordinal = 247 sorting->GetAppLaunchOrdinal(kPackagedApp2Id); 248 sorting->SetPageOrdinal(kHostedAppId, package_app_page); 249 sorting->SetAppLaunchOrdinal(kHostedAppId, 250 app1_ordinal.CreateBetween(app2_ordinal)); 251 // Old behavior: This would be "Packaged App 1,Hosted App,Packaged App 2" 252 // New behavior: Sorting order doesn't change. 253 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 254} 255 256TEST_F(ExtensionAppModelBuilderTest, OnExtensionMoved) { 257 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 258 sorting->SetPageOrdinal(kHostedAppId, 259 sorting->GetPageOrdinal(kPackagedApp1Id)); 260 261 service_->OnExtensionMoved(kHostedAppId, kPackagedApp1Id, kPackagedApp2Id); 262 // Old behavior: This would be "Packaged App 1,Hosted App,Packaged App 2" 263 // New behavior: Sorting order doesn't change. 264 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 265 266 service_->OnExtensionMoved(kHostedAppId, kPackagedApp2Id, std::string()); 267 // Old behavior: This would be restored to the default order. 268 // New behavior: Sorting order still doesn't change. 269 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 270 271 service_->OnExtensionMoved(kHostedAppId, std::string(), kPackagedApp1Id); 272 // Old behavior: This would be "Hosted App,Packaged App 1,Packaged App 2" 273 // New behavior: Sorting order doesn't change. 274 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 275} 276 277TEST_F(ExtensionAppModelBuilderTest, InvalidOrdinal) { 278 // Creates a no-ordinal case. 279 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 280 sorting->ClearOrdinals(kPackagedApp1Id); 281 282 // Creates an corrupted ordinal case. 283 extensions::ExtensionScopedPrefs* scoped_prefs = service_->extension_prefs(); 284 scoped_prefs->UpdateExtensionPref( 285 kHostedAppId, 286 "page_ordinal", 287 base::Value::CreateStringValue("a corrupted ordinal")); 288 289 // This should not assert or crash. 290 ExtensionAppModelBuilder builder1( 291 profile_.get(), model_.get(), controller_.get()); 292} 293 294TEST_F(ExtensionAppModelBuilderTest, OrdinalConfilicts) { 295 // Creates conflict ordinals for app1 and app2. 296 syncer::StringOrdinal conflict_ordinal = 297 syncer::StringOrdinal::CreateInitialOrdinal(); 298 299 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 300 sorting->SetPageOrdinal(kHostedAppId, conflict_ordinal); 301 sorting->SetAppLaunchOrdinal(kHostedAppId, conflict_ordinal); 302 303 sorting->SetPageOrdinal(kPackagedApp1Id, conflict_ordinal); 304 sorting->SetAppLaunchOrdinal(kPackagedApp1Id, conflict_ordinal); 305 306 sorting->SetPageOrdinal(kPackagedApp2Id, conflict_ordinal); 307 sorting->SetAppLaunchOrdinal(kPackagedApp2Id, conflict_ordinal); 308 309 // This should not assert or crash. 310 ExtensionAppModelBuilder builder1( 311 profile_.get(), model_.get(), controller_.get()); 312 313 // By default, conflicted items are sorted by their app ids (= order added). 314 EXPECT_EQ(std::string("Hosted App,Packaged App 1,Packaged App 2"), 315 GetModelContent(model_.get())); 316} 317 318TEST_F(ExtensionAppModelBuilderTest, SwitchProfile) { 319 EXPECT_EQ(kDefaultAppCount, model_->item_list()->item_count()); 320 321 // Switch to a profile with no apps, ensure all apps are removed. 322 ExtensionServiceInitParams params = 323 CreateDefaultInitParamsInTempDir(&second_profile_temp_dir_); 324 scoped_ptr<TestingProfile> profile2 = CreateTestingProfile(params); 325 InitializeExtensionServiceForProfile(params, profile2.get()); 326 327 builder_->SwitchProfile(profile2.get()); 328 EXPECT_EQ(0u, model_->item_list()->item_count()); 329 330 // Switch back to the main profile, ensure apps are restored. 331 builder_->SwitchProfile(profile_.get()); 332 EXPECT_EQ(kDefaultAppCount, model_->item_list()->item_count()); 333} 334