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