summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/webstore_installer_browsertest.cc
blob: 92a080f85ef93f903837df867761c4f4ff2178d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/webstore_installer.h"

#include <utility>

#include "base/bind.h"
#include "base/run_loop.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/webstore_installer_test.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/webstore_install_result.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/value_builder.h"

namespace extensions {

namespace {

const char kExtensionName[] = "InstallerExtension";
const char kWebstoreDomain[] = "cws.com";
const char kAppDomain[] = "app.com";
const char kNonAppDomain[] = "nonapp.com";
const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje";
const char kTestDataPath[] = "extensions/api_test/webstore_inline_install";
const char kCrxFilename[] = "extension.crx";

}  // namespace

// Test version of WebstoreInstaller that intercepts the destructor.
class TestWebstoreInstaller : public WebstoreInstaller {
 public:
  TestWebstoreInstaller(Profile* profile,
                        Delegate* delegate,
                        content::WebContents* web_contents,
                        const std::string& id,
                        scoped_ptr<Approval> approval,
                        InstallSource source)
      : WebstoreInstaller(profile,
                          delegate,
                          web_contents,
                          id,
                          std::move(approval),
                          source) {}

  void SetDeletedClosure(const base::Closure& cb) { deleted_closure_ = cb; }

 private:
  ~TestWebstoreInstaller() override {
    if (!deleted_closure_.is_null())
      deleted_closure_.Run();
  }

  base::Closure deleted_closure_;
};

class WebstoreInstallerBrowserTest
    : public WebstoreInstallerTest,
      public WebstoreInstaller::Delegate {
 public:
  WebstoreInstallerBrowserTest()
      : WebstoreInstallerTest(
            kWebstoreDomain,
            kTestDataPath,
            kCrxFilename,
            kAppDomain,
            kNonAppDomain) {}
  ~WebstoreInstallerBrowserTest() override {}

  void SetDoneClosure(const base::Closure& done_closure) {
    done_closure_ = done_closure;
  }

  bool success() const { return success_; }

  // Overridden from WebstoreInstaller::Delegate:
  void OnExtensionDownloadStarted(const std::string& id,
                                  content::DownloadItem* item) override;
  void OnExtensionDownloadProgress(const std::string& id,
                                   content::DownloadItem* item) override;
  void OnExtensionInstallSuccess(const std::string& id) override;
  void OnExtensionInstallFailure(
      const std::string& id,
      const std::string& error,
      WebstoreInstaller::FailureReason reason) override;

 private:
  base::Closure done_closure_;
  bool success_;
};

void WebstoreInstallerBrowserTest::OnExtensionDownloadStarted(
    const std::string& id, content::DownloadItem* item) {
}

void WebstoreInstallerBrowserTest::OnExtensionDownloadProgress(
    const std::string& id, content::DownloadItem* item) {
}

void WebstoreInstallerBrowserTest::OnExtensionInstallSuccess(
    const std::string& id) {
  success_ = true;
  done_closure_.Run();
}

void WebstoreInstallerBrowserTest::OnExtensionInstallFailure(
    const std::string& id,
    const std::string& error,
    WebstoreInstaller::FailureReason reason) {
  success_ = false;
  done_closure_.Run();
}

IN_PROC_BROWSER_TEST_F(WebstoreInstallerBrowserTest, WebstoreInstall) {
  scoped_ptr<base::DictionaryValue> manifest(
      DictionaryBuilder()
          .Set("name", kExtensionName)
          .Set("description", "Foo")
          .Set("manifest_version", 2)
          .Set("version", "1.0")
          .Set("permissions", ListBuilder().Append("tabs").Build())
          .Build());

  content::WebContents* active_web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(active_web_contents);

  // Create an approval.
  scoped_ptr<WebstoreInstaller::Approval> approval =
      WebstoreInstaller::Approval::CreateWithNoInstallPrompt(
          browser()->profile(), kTestExtensionId, std::move(manifest), false);

  // Create and run a WebstoreInstaller.
  base::RunLoop run_loop;
  SetDoneClosure(run_loop.QuitClosure());
  TestWebstoreInstaller* installer = new TestWebstoreInstaller(
      browser()->profile(), this, active_web_contents, kTestExtensionId,
      std::move(approval), WebstoreInstaller::INSTALL_SOURCE_OTHER);
  installer->Start();
  run_loop.Run();

  EXPECT_TRUE(success());
  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
  ASSERT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId));
}

IN_PROC_BROWSER_TEST_F(WebstoreInstallerBrowserTest, SimultaneousInstall) {
  scoped_ptr<base::DictionaryValue> manifest(
      DictionaryBuilder()
          .Set("name", kExtensionName)
          .Set("description", "Foo")
          .Set("manifest_version", 2)
          .Set("version", "1.0")
          .Set("permissions", ListBuilder().Append("tabs").Build())
          .Build());

  content::WebContents* active_web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(active_web_contents);

  // Create an approval.
  scoped_ptr<WebstoreInstaller::Approval> approval =
      WebstoreInstaller::Approval::CreateWithNoInstallPrompt(
          browser()->profile(),
          kTestExtensionId,
          scoped_ptr<base::DictionaryValue>(manifest->DeepCopy()),
          false);

  // Create and run a WebstoreInstaller.
  base::RunLoop run_loop;
  SetDoneClosure(run_loop.QuitClosure());
  scoped_refptr<TestWebstoreInstaller> installer = new TestWebstoreInstaller(
      browser()->profile(), this, active_web_contents, kTestExtensionId,
      std::move(approval), WebstoreInstaller::INSTALL_SOURCE_OTHER);
  installer->Start();

  // Simulate another mechanism installing the same extension.
  scoped_refptr<const Extension> extension =
      ExtensionBuilder()
          .SetLocation(Manifest::INTERNAL)
          .SetID(kTestExtensionId)
          .SetManifest(std::move(manifest))
          .Build();
  extension_service()->OnExtensionInstalled(extension.get(),
                                            syncer::StringOrdinal(),
                                            0);

  run_loop.Run();

  // Wait for the WebstoreInstaller to be destroyed. Bad things happen if we
  // don't wait for this.
  base::RunLoop run_loop2;
  installer->SetDeletedClosure(run_loop2.QuitClosure());
  installer = nullptr;
  run_loop2.Run();

  EXPECT_TRUE(success());
  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
  // Extension ends up as disabled because of permissions.
  ASSERT_TRUE(registry->disabled_extensions().GetByID(kTestExtensionId));
}

}  // namespace extensions