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 "chrome/browser/extensions/external_provider_impl.h" 6 7 #include "base/command_line.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "base/prefs/testing_pref_service.h" 10 #include "base/test/scoped_path_override.h" 11 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chromeos/customization_document.h" 13 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" 14 #include "chrome/browser/extensions/extension_service.h" 15 #include "chrome/browser/extensions/extension_service_test_base.h" 16 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_switches.h" 18 #include "chrome/test/base/testing_browser_process.h" 19 #include "chrome/test/base/testing_profile.h" 20 #include "chromeos/system/mock_statistics_provider.h" 21 #include "chromeos/system/statistics_provider.h" 22 #include "content/public/browser/notification_service.h" 23 #include "content/public/test/test_utils.h" 24 #include "testing/gmock/include/gmock/gmock.h" 25 26 using ::testing::_; 27 using ::testing::NotNull; 28 using ::testing::Return; 29 30 namespace extensions { 31 32 namespace { 33 34 const char kExternalAppId[] = "kekdneafjmhmndejhmbcadfiiofngffo"; 35 36 class ExternalProviderImplChromeOSTest : public ExtensionServiceTestBase { 37 public: 38 ExternalProviderImplChromeOSTest() 39 : fake_user_manager_(new chromeos::FakeUserManager()), 40 scoped_user_manager_(fake_user_manager_) { 41 } 42 43 virtual ~ExternalProviderImplChromeOSTest() {} 44 45 void InitServiceWithExternalProviders() { 46 InitializeEmptyExtensionService(); 47 service_->Init(); 48 49 ProviderCollection providers; 50 extensions::ExternalProviderImpl::CreateExternalProviders( 51 service_, profile_.get(), &providers); 52 53 for (ProviderCollection::iterator i = providers.begin(); 54 i != providers.end(); 55 ++i) { 56 service_->AddProviderForTesting(i->release()); 57 } 58 } 59 60 // ExtensionServiceTestBase overrides: 61 virtual void SetUp() OVERRIDE { 62 ExtensionServiceTestBase::SetUp(); 63 64 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); 65 chromeos::ServicesCustomizationDocument::RegisterPrefs( 66 local_state_.registry()); 67 68 external_externsions_overrides_.reset(new base::ScopedPathOverride( 69 chrome::DIR_EXTERNAL_EXTENSIONS, data_dir().Append("external"))); 70 71 chromeos::system::StatisticsProvider::SetTestProvider( 72 &mock_statistics_provider_); 73 EXPECT_CALL(mock_statistics_provider_, GetMachineStatistic(_, NotNull())) 74 .WillRepeatedly(Return(false)); 75 } 76 77 virtual void TearDown() OVERRIDE { 78 chromeos::system::StatisticsProvider::SetTestProvider(NULL); 79 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); 80 } 81 82 private: 83 TestingPrefServiceSimple local_state_; 84 scoped_ptr<base::ScopedPathOverride> external_externsions_overrides_; 85 chromeos::system::MockStatisticsProvider mock_statistics_provider_; 86 chromeos::FakeUserManager* fake_user_manager_; 87 chromeos::ScopedUserManagerEnabler scoped_user_manager_; 88 89 DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplChromeOSTest); 90 }; 91 92 } // namespace 93 94 // Normal mode, external app should be installed. 95 TEST_F(ExternalProviderImplChromeOSTest, Normal) { 96 InitServiceWithExternalProviders(); 97 98 service_->CheckForExternalUpdates(); 99 content::WindowedNotificationObserver( 100 chrome::NOTIFICATION_CRX_INSTALLER_DONE, 101 content::NotificationService::AllSources()).Wait(); 102 103 EXPECT_TRUE(service_->GetInstalledExtension(kExternalAppId)); 104 } 105 106 // App mode, no external app should be installed. 107 TEST_F(ExternalProviderImplChromeOSTest, AppMode) { 108 CommandLine* command = CommandLine::ForCurrentProcess(); 109 command->AppendSwitchASCII(switches::kForceAppMode, std::string()); 110 command->AppendSwitchASCII(switches::kAppId, std::string("app_id")); 111 112 InitServiceWithExternalProviders(); 113 114 service_->CheckForExternalUpdates(); 115 base::RunLoop().RunUntilIdle(); 116 117 EXPECT_FALSE(service_->GetInstalledExtension(kExternalAppId)); 118 } 119 120 } // namespace extensions 121