1// Copyright (c) 2011 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/stringprintf.h"
6#if defined (OS_WIN)
7#include "base/win/windows_version.h"
8#endif  // defined (OS_WIN)
9
10#include "chrome/browser/extensions/extension_apitest.h"
11#include "chrome/browser/extensions/extension_webstore_private_api.h"
12#include "chrome/common/chrome_switches.h"
13#include "chrome/test/ui_test_utils.h"
14#include "net/base/mock_host_resolver.h"
15
16class ExtensionGalleryInstallApiTest : public ExtensionApiTest {
17 public:
18  void SetUpCommandLine(CommandLine* command_line) {
19    ExtensionApiTest::SetUpCommandLine(command_line);
20    command_line->AppendSwitchASCII(switches::kAppsGalleryURL,
21        "http://www.example.com");
22  }
23
24  bool RunInstallTest(const std::string& page) {
25    std::string base_url = base::StringPrintf(
26        "http://www.example.com:%u/files/extensions/",
27        test_server()->host_port_pair().port());
28
29    std::string testing_install_base_url = base_url;
30    testing_install_base_url += "good.crx";
31
32    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
33      switches::kAppsGalleryUpdateURL, testing_install_base_url);
34
35    std::string page_url = base_url;
36    page_url += "api_test/extension_gallery_install/" + page;
37
38    return RunPageTest(page_url.c_str());
39  }
40};
41
42namespace {
43
44bool RunningOnXP() {
45#if defined (OS_WIN)
46  return GetVersion() < base::win::VERSION_VISTA;
47#else
48  return false;
49#endif  // defined (OS_WIN)
50}
51
52}  // namespace
53
54// TODO(asargent) - for some reason this test occasionally fails on XP,
55// but not other versions of windows. http://crbug.com/55642
56#if defined (OS_WIN)
57#define MAYBE_InstallAndUninstall FLAKY_InstallAndUninstall
58#else
59#define MAYBE_InstallAndUninstall InstallAndUninstall
60#endif
61IN_PROC_BROWSER_TEST_F(ExtensionGalleryInstallApiTest,
62                       MAYBE_InstallAndUninstall) {
63  if (RunningOnXP()) {
64    LOG(INFO) << "Adding host resolver rule";
65  }
66  host_resolver()->AddRule("www.example.com", "127.0.0.1");
67  if (RunningOnXP()) {
68    LOG(INFO) << "Starting test server";
69  }
70  ASSERT_TRUE(test_server()->Start());
71
72  if (RunningOnXP()) {
73    LOG(INFO) << "Starting tests without user gesture checking";
74  }
75  BeginInstallFunction::SetIgnoreUserGestureForTests(true);
76  ASSERT_TRUE(RunInstallTest("test.html"));
77  ASSERT_TRUE(RunInstallTest("complete_without_begin.html"));
78  ASSERT_TRUE(RunInstallTest("invalid_begin.html"));
79
80  if (RunningOnXP()) {
81    LOG(INFO) << "Starting tests with user gesture checking";
82  }
83  BeginInstallFunction::SetIgnoreUserGestureForTests(false);
84  ASSERT_TRUE(RunInstallTest("no_user_gesture.html"));
85}
86