extension_app_model_builder_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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(kDefaultAppCount, model_->item_list()->item_count()); 150 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 151} 152 153TEST_F(ExtensionAppModelBuilderTest, HideWebStore) { 154 // Install a "web store" app. 155 scoped_refptr<extensions::Extension> store = 156 MakeApp("webstore", 157 "0.0", 158 "http://google.com", 159 std::string(extension_misc::kWebStoreAppId)); 160 service_->AddExtension(store.get()); 161 162 // Install an "enterprise web store" app. 163 scoped_refptr<extensions::Extension> enterprise_store = 164 MakeApp("enterprise_webstore", 165 "0.0", 166 "http://google.com", 167 std::string(extension_misc::kEnterpriseWebStoreAppId)); 168 service_->AddExtension(enterprise_store.get()); 169 170 // Web stores should be present in the AppListModel. 171 app_list::AppListModel model1; 172 ExtensionAppModelBuilder builder1( 173 profile_.get(), &model1, controller_.get()); 174 std::string content = GetModelContent(&model1); 175 EXPECT_NE(std::string::npos, content.find("webstore")); 176 EXPECT_NE(std::string::npos, content.find("enterprise_webstore")); 177 178 // Activate the HideWebStoreIcon policy. 179 profile_->GetPrefs()->SetBoolean(prefs::kHideWebStoreIcon, true); 180 181 // Web stores should NOT be in the AppListModel. 182 app_list::AppListModel model2; 183 ExtensionAppModelBuilder builder2( 184 profile_.get(), &model2, controller_.get()); 185 content = GetModelContent(&model2); 186 EXPECT_EQ(std::string::npos, content.find("webstore")); 187 EXPECT_EQ(std::string::npos, content.find("enterprise_webstore")); 188} 189 190TEST_F(ExtensionAppModelBuilderTest, DisableAndEnable) { 191 service_->DisableExtension(kHostedAppId, 192 extensions::Extension::DISABLE_NONE); 193 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 194 195 service_->EnableExtension(kHostedAppId); 196 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 197} 198 199TEST_F(ExtensionAppModelBuilderTest, Uninstall) { 200 service_->UninstallExtension(kPackagedApp2Id, false, NULL); 201 EXPECT_EQ(std::string("Packaged App 1,Hosted App"), 202 GetModelContent(model_.get())); 203 204 base::RunLoop().RunUntilIdle(); 205} 206 207TEST_F(ExtensionAppModelBuilderTest, UninstallTerminatedApp) { 208 const extensions::Extension* app = 209 service_->GetInstalledExtension(kPackagedApp2Id); 210 ASSERT_TRUE(app != NULL); 211 212 // Simulate an app termination. 213 service_->TrackTerminatedExtensionForTest(app); 214 215 service_->UninstallExtension(kPackagedApp2Id, false, NULL); 216 EXPECT_EQ(std::string("Packaged App 1,Hosted App"), 217 GetModelContent(model_.get())); 218 219 base::RunLoop().RunUntilIdle(); 220} 221 222TEST_F(ExtensionAppModelBuilderTest, Reinstall) { 223 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 224 225 // Install kPackagedApp1Id again should not create a new entry. 226 extensions::InstallTracker* tracker = 227 extensions::InstallTrackerFactory::GetForProfile(profile_.get()); 228 extensions::InstallObserver::ExtensionInstallParams params( 229 kPackagedApp1Id, "", gfx::ImageSkia(), true, true); 230 tracker->OnBeginExtensionInstall(params); 231 232 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 233} 234 235TEST_F(ExtensionAppModelBuilderTest, OrdinalPrefsChange) { 236 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 237 238 syncer::StringOrdinal package_app_page = 239 sorting->GetPageOrdinal(kPackagedApp1Id); 240 sorting->SetPageOrdinal(kHostedAppId, package_app_page.CreateBefore()); 241 // Old behavior: This would be "Hosted App,Packaged App 1,Packaged App 2" 242 // New behavior: Sorting order doesn't change. 243 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 244 245 syncer::StringOrdinal app1_ordinal = 246 sorting->GetAppLaunchOrdinal(kPackagedApp1Id); 247 syncer::StringOrdinal app2_ordinal = 248 sorting->GetAppLaunchOrdinal(kPackagedApp2Id); 249 sorting->SetPageOrdinal(kHostedAppId, package_app_page); 250 sorting->SetAppLaunchOrdinal(kHostedAppId, 251 app1_ordinal.CreateBetween(app2_ordinal)); 252 // Old behavior: This would be "Packaged App 1,Hosted App,Packaged App 2" 253 // New behavior: Sorting order doesn't change. 254 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 255} 256 257TEST_F(ExtensionAppModelBuilderTest, OnExtensionMoved) { 258 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 259 sorting->SetPageOrdinal(kHostedAppId, 260 sorting->GetPageOrdinal(kPackagedApp1Id)); 261 262 service_->OnExtensionMoved(kHostedAppId, kPackagedApp1Id, kPackagedApp2Id); 263 // Old behavior: This would be "Packaged App 1,Hosted App,Packaged App 2" 264 // New behavior: Sorting order doesn't change. 265 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 266 267 service_->OnExtensionMoved(kHostedAppId, kPackagedApp2Id, std::string()); 268 // Old behavior: This would be restored to the default order. 269 // New behavior: Sorting order still doesn't change. 270 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 271 272 service_->OnExtensionMoved(kHostedAppId, std::string(), kPackagedApp1Id); 273 // Old behavior: This would be "Hosted App,Packaged App 1,Packaged App 2" 274 // New behavior: Sorting order doesn't change. 275 EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get())); 276} 277 278TEST_F(ExtensionAppModelBuilderTest, InvalidOrdinal) { 279 // Creates a no-ordinal case. 280 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 281 sorting->ClearOrdinals(kPackagedApp1Id); 282 283 // Creates an corrupted ordinal case. 284 extensions::ExtensionScopedPrefs* scoped_prefs = service_->extension_prefs(); 285 scoped_prefs->UpdateExtensionPref( 286 kHostedAppId, 287 "page_ordinal", 288 base::Value::CreateStringValue("a corrupted ordinal")); 289 290 // This should not assert or crash. 291 ExtensionAppModelBuilder builder1( 292 profile_.get(), model_.get(), controller_.get()); 293} 294 295TEST_F(ExtensionAppModelBuilderTest, OrdinalConfilicts) { 296 // Creates conflict ordinals for app1 and app2. 297 syncer::StringOrdinal conflict_ordinal = 298 syncer::StringOrdinal::CreateInitialOrdinal(); 299 300 extensions::AppSorting* sorting = service_->extension_prefs()->app_sorting(); 301 sorting->SetPageOrdinal(kHostedAppId, conflict_ordinal); 302 sorting->SetAppLaunchOrdinal(kHostedAppId, conflict_ordinal); 303 304 sorting->SetPageOrdinal(kPackagedApp1Id, conflict_ordinal); 305 sorting->SetAppLaunchOrdinal(kPackagedApp1Id, conflict_ordinal); 306 307 sorting->SetPageOrdinal(kPackagedApp2Id, conflict_ordinal); 308 sorting->SetAppLaunchOrdinal(kPackagedApp2Id, conflict_ordinal); 309 310 // This should not assert or crash. 311 ExtensionAppModelBuilder builder1( 312 profile_.get(), model_.get(), controller_.get()); 313 314 // By default, conflicted items are sorted by their app ids (= order added). 315 EXPECT_EQ(std::string("Hosted App,Packaged App 1,Packaged App 2"), 316 GetModelContent(model_.get())); 317} 318