summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_gallery_install_apitest.cc
blob: bf3a5808ed913602d4ff67e0668c14aa6d380d5c (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
// Copyright (c) 2011 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 "base/stringprintf.h"
#if defined (OS_WIN)
#include "base/win/windows_version.h"
#endif  // defined (OS_WIN)

#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_webstore_private_api.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
#include "content/common/notification_service.h"
#include "net/base/mock_host_resolver.h"

// This is a helper class to let us automatically accept extension install
// dialogs.
class GalleryInstallApiTestObserver :
    public base::RefCounted<GalleryInstallApiTestObserver>,
    public NotificationObserver {
 public:
  GalleryInstallApiTestObserver() {
    registrar_.Add(this,
                  chrome::NOTIFICATION_EXTENSION_WILL_SHOW_CONFIRM_DIALOG,
                  NotificationService::AllSources());
  }

  void InstallUIProceed(ExtensionInstallUI::Delegate* delegate) {
    delegate->InstallUIProceed();
  }

  virtual void Observe(int type,
                       const NotificationSource& source,
                       const NotificationDetails& details) OVERRIDE {
    ExtensionInstallUI* prompt = Source<ExtensionInstallUI>(source).ptr();
    CHECK(prompt->delegate_);
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(
            this,
            &GalleryInstallApiTestObserver::InstallUIProceed,
            prompt->delegate_));
  }

 private:
  NotificationRegistrar registrar_;
};

class ExtensionGalleryInstallApiTest : public ExtensionApiTest {
 public:
  void SetUpCommandLine(CommandLine* command_line) {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitchASCII(switches::kAppsGalleryURL,
        "http://www.example.com");
  }

  bool RunInstallTest(const std::string& page) {
    scoped_refptr<GalleryInstallApiTestObserver> observer =
        new GalleryInstallApiTestObserver();  // Responds to install dialog.
    std::string base_url = base::StringPrintf(
        "http://www.example.com:%u/files/extensions/",
        test_server()->host_port_pair().port());

    std::string testing_install_base_url = base_url;
    testing_install_base_url += "good.crx";

    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      switches::kAppsGalleryUpdateURL, testing_install_base_url);

    std::string page_url = base_url;
    page_url += "api_test/extension_gallery_install/" + page;

    return RunPageTest(page_url.c_str());
  }
};

namespace {

bool RunningOnXP() {
#if defined (OS_WIN)
  return GetVersion() < base::win::VERSION_VISTA;
#else
  return false;
#endif  // defined (OS_WIN)
}

}  // namespace

// TODO(asargent) - for some reason this test occasionally fails on XP,
// but not other versions of windows. http://crbug.com/55642
#if defined (OS_WIN)
#define MAYBE_InstallAndUninstall DISABLED_InstallAndUninstall
#else
#define MAYBE_InstallAndUninstall InstallAndUninstall
#endif
IN_PROC_BROWSER_TEST_F(ExtensionGalleryInstallApiTest,
                       MAYBE_InstallAndUninstall) {
  if (RunningOnXP()) {
    LOG(INFO) << "Adding host resolver rule";
  }
  host_resolver()->AddRule("www.example.com", "127.0.0.1");
  if (RunningOnXP()) {
    LOG(INFO) << "Starting test server";
  }
  ASSERT_TRUE(test_server()->Start());

  if (RunningOnXP()) {
    LOG(INFO) << "Starting tests without user gesture checking";
  }
  BeginInstallFunction::SetIgnoreUserGestureForTests(true);
  ASSERT_TRUE(RunInstallTest("test.html"));
  ASSERT_TRUE(RunInstallTest("complete_without_begin.html"));
  ASSERT_TRUE(RunInstallTest("invalid_begin.html"));

  if (RunningOnXP()) {
    LOG(INFO) << "Starting tests with user gesture checking";
  }
  BeginInstallFunction::SetIgnoreUserGestureForTests(false);
  ASSERT_TRUE(RunInstallTest("no_user_gesture.html"));
}