Home | History | Annotate | Download | only in extensions
      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/string_util.h"
      6 #include "chrome/browser/background_contents_service.h"
      7 #include "chrome/browser/background_contents_service_factory.h"
      8 #include "chrome/browser/extensions/extension_apitest.h"
      9 #include "chrome/browser/extensions/extension_service.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/common/chrome_switches.h"
     13 #include "chrome/common/extensions/extension.h"
     14 #include "chrome/test/ui_test_utils.h"
     15 #include "net/base/mock_host_resolver.h"
     16 
     17 class AppBackgroundPageApiTest : public ExtensionApiTest {
     18  public:
     19   void SetUpCommandLine(CommandLine* command_line) {
     20     ExtensionApiTest::SetUpCommandLine(command_line);
     21     command_line->AppendSwitch(switches::kDisablePopupBlocking);
     22     command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage);
     23   }
     24 
     25   bool CreateApp(const std::string& app_manifest,
     26                  FilePath* app_dir) {
     27     if (!app_dir_.CreateUniqueTempDir()) {
     28       LOG(ERROR) << "Unable to create a temporary directory.";
     29       return false;
     30     }
     31     FilePath manifest_path = app_dir_.path().AppendASCII("manifest.json");
     32     int bytes_written = file_util::WriteFile(manifest_path,
     33                                              app_manifest.data(),
     34                                              app_manifest.size());
     35     if (bytes_written != static_cast<int>(app_manifest.size())) {
     36       LOG(ERROR) << "Unable to write complete manifest to file. Return code="
     37                  << bytes_written;
     38       return false;
     39     }
     40     *app_dir = app_dir_.path();
     41     return true;
     42   }
     43 
     44  private:
     45   ScopedTempDir app_dir_;
     46 };
     47 
     48 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, Basic) {
     49   host_resolver()->AddRule("a.com", "127.0.0.1");
     50   ASSERT_TRUE(StartTestServer());
     51 
     52   std::string app_manifest = StringPrintf(
     53       "{"
     54       "  \"name\": \"App\","
     55       "  \"version\": \"0.1\","
     56       "  \"app\": {"
     57       "    \"urls\": ["
     58       "      \"http://a.com/\""
     59       "    ],"
     60       "    \"launch\": {"
     61       "      \"web_url\": \"http://a.com:%d/\""
     62       "    }"
     63       "  },"
     64       "  \"permissions\": [\"background\"]"
     65       "}",
     66       test_server()->host_port_pair().port());
     67 
     68   FilePath app_dir;
     69   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
     70   ASSERT_TRUE(LoadExtension(app_dir));
     71   ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
     72 }
     73 
     74 // Crashy, http://crbug.com/49215.
     75 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
     76   host_resolver()->AddRule("a.com", "127.0.0.1");
     77   ASSERT_TRUE(StartTestServer());
     78 
     79   std::string app_manifest = StringPrintf(
     80       "{"
     81       "  \"name\": \"App\","
     82       "  \"version\": \"0.1\","
     83       "  \"app\": {"
     84       "    \"urls\": ["
     85       "      \"http://a.com/\""
     86       "    ],"
     87       "    \"launch\": {"
     88       "      \"web_url\": \"http://a.com:%d/\""
     89       "    }"
     90       "  }"
     91       "}",
     92       test_server()->host_port_pair().port());
     93 
     94   FilePath app_dir;
     95   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
     96   ASSERT_TRUE(LoadExtension(app_dir));
     97   ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
     98       << message_;
     99 }
    100 
    101 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
    102   host_resolver()->AddRule("a.com", "127.0.0.1");
    103   ASSERT_TRUE(StartTestServer());
    104 
    105   std::string app_manifest = StringPrintf(
    106       "{"
    107       "  \"name\": \"App\","
    108       "  \"version\": \"0.1\","
    109       "  \"app\": {"
    110       "    \"urls\": ["
    111       "      \"http://a.com/\""
    112       "    ],"
    113       "    \"launch\": {"
    114       "      \"web_url\": \"http://a.com:%d/\""
    115       "    }"
    116       "  },"
    117       "  \"permissions\": [\"background\"],"
    118       "  \"background_page\": \"http://a.com:%d/test.html\""
    119       "}",
    120       test_server()->host_port_pair().port(),
    121       test_server()->host_port_pair().port());
    122 
    123   FilePath app_dir;
    124   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
    125   ASSERT_TRUE(LoadExtension(app_dir));
    126 
    127   const Extension* extension = GetSingleLoadedExtension();
    128   ASSERT_TRUE(
    129       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
    130           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
    131 }
    132