Home | History | Annotate | Download | only in extensions
      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 "base/memory/scoped_ptr.h"
      6 #include "base/message_loop/message_loop.h"
      7 #include "base/prefs/pref_service.h"
      8 #include "chrome/browser/extensions/default_apps.h"
      9 #include "chrome/browser/extensions/external_pref_loader.h"
     10 #include "chrome/common/chrome_paths.h"
     11 #include "chrome/common/pref_names.h"
     12 #include "chrome/test/base/testing_profile.h"
     13 #include "content/public/test/test_browser_thread.h"
     14 #include "extensions/common/extension.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 using default_apps::Provider;
     18 
     19 namespace extensions {
     20 
     21 class MockExternalLoader : public ExternalLoader {
     22  public:
     23   MockExternalLoader() {}
     24 
     25   virtual void StartLoading() OVERRIDE {}
     26  private:
     27   virtual ~MockExternalLoader() {}
     28 };
     29 
     30 class DefaultAppsTest : public testing::Test {
     31  public:
     32   DefaultAppsTest() : ui_thread_(content::BrowserThread::UI, &loop_) {}
     33   virtual ~DefaultAppsTest() {}
     34  private:
     35   base::MessageLoopForIO loop_;
     36   content::TestBrowserThread ui_thread_;
     37 };
     38 
     39 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
     40 // Chrome OS has different way of installing default apps.
     41 // Android does not currently support installing apps via Chrome.
     42 TEST_F(DefaultAppsTest, Install) {
     43   scoped_ptr<TestingProfile> profile(new TestingProfile());
     44   ExternalLoader* loader = new MockExternalLoader();
     45 
     46   Provider provider(profile.get(), NULL, loader, Manifest::INTERNAL,
     47                     Manifest::INTERNAL, Extension::NO_FLAGS);
     48 
     49   // The default apps should be installed if kDefaultAppsInstallState
     50   // is unknown.
     51   EXPECT_TRUE(provider.ShouldInstallInProfile());
     52   int state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState);
     53   EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps);
     54 
     55   // The default apps should only be installed once.
     56   EXPECT_FALSE(provider.ShouldInstallInProfile());
     57   state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState);
     58   EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps);
     59 
     60   // The default apps should not be installed if the state is
     61   // kNeverProvideDefaultApps
     62   profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
     63       default_apps::kNeverInstallDefaultApps);
     64   EXPECT_FALSE(provider.ShouldInstallInProfile());
     65   state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState);
     66   EXPECT_TRUE(state == default_apps::kNeverInstallDefaultApps);
     67 
     68   // The old default apps with kAlwaysInstallDefaultAppss should be migrated.
     69   profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
     70       default_apps::kProvideLegacyDefaultApps);
     71   EXPECT_TRUE(provider.ShouldInstallInProfile());
     72   state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState);
     73   EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps);
     74 
     75   class DefaultTestingProfile : public TestingProfile {
     76     virtual  bool WasCreatedByVersionOrLater(
     77         const std::string& version) OVERRIDE {
     78       return false;
     79     }
     80   };
     81   profile.reset(new DefaultTestingProfile);
     82   Provider provider2(profile.get(), NULL, loader, Manifest::INTERNAL,
     83                      Manifest::INTERNAL, Extension::NO_FLAGS);
     84   // The old default apps with kProvideLegacyDefaultApps should be migrated
     85   // even if the profile version is older than Chrome version.
     86   profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
     87       default_apps::kProvideLegacyDefaultApps);
     88   EXPECT_TRUE(provider2.ShouldInstallInProfile());
     89   state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState);
     90   EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps);
     91 }
     92 #endif
     93 
     94 }  // namespace extensions
     95