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/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;
18
19namespace extensions {
20
21class MockExternalLoader : public ExternalLoader {
22 public:
23  MockExternalLoader() {}
24
25  virtual void StartLoading() OVERRIDE {}
26 private:
27  virtual ~MockExternalLoader() {}
28};
29
30class DefaultAppsTest : public testing::Test {
31 public:
32  DefaultAppsTest() : loop_(base::MessageLoop::TYPE_IO),
33      ui_thread_(content::BrowserThread::UI, &loop_) {}
34  virtual ~DefaultAppsTest() {}
35 private:
36  base::MessageLoop loop_;
37  content::TestBrowserThread ui_thread_;
38};
39
40#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
41// Chrome OS has different way of installing default apps.
42// Android does not currently support installing apps via Chrome.
43TEST_F(DefaultAppsTest, Install) {
44  scoped_ptr<TestingProfile> profile(new TestingProfile());
45  ExternalLoader* loader = new MockExternalLoader();
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
95}  // namespace extensions
96