Home | History | Annotate | Download | only in extensions
      1 // Copyright 2013 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 "chrome/browser/extensions/browsertest_util.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "chrome/browser/extensions/extension_browsertest.h"
      9 #include "chrome/browser/extensions/test_extension_dir.h"
     10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     11 #include "chrome/test/base/in_process_browser_test.h"
     12 #include "testing/gtest/include/gtest/gtest-spi.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace extensions {
     16 namespace {
     17 
     18 // TODO(jyasskin): It should be possible to test extensions without
     19 // inheriting from the monolithic ExtensionBrowserTest.
     20 class ExtensionBrowsertestUtilTest : public ExtensionBrowserTest {
     21 };
     22 
     23 IN_PROC_BROWSER_TEST_F(ExtensionBrowsertestUtilTest,
     24                        ExecuteScriptInBackground) {
     25   TestExtensionDir ext_dir;
     26   ext_dir.WriteManifest(
     27       "{\n"
     28       "  \"name\": \"ExecuteScript extension\",\n"
     29       "  \"version\": \"0.1\",\n"
     30       "  \"manifest_version\": 2,\n"
     31       "  \"background\": {\"scripts\": [\"background.js\"]}\n"
     32       "}\n");
     33   ext_dir.WriteFile(FILE_PATH_LITERAL("background.js"), "");
     34   const Extension* extension = LoadExtension(ext_dir.unpacked_path());
     35   ASSERT_TRUE(extension);
     36   EXPECT_EQ(extension->id(),
     37             browsertest_util::ExecuteScriptInBackgroundPage(
     38                 profile(),
     39                 extension->id(),
     40                 "window.domAutomationController.send(chrome.runtime.id);"));
     41 
     42   // This test checks that executing a script doesn't block the browser process.
     43   EXPECT_EQ(base::IntToString(browser()->tab_strip_model()->count()),
     44             browsertest_util::ExecuteScriptInBackgroundPage(
     45                 profile(),
     46                 extension->id(),
     47                 "chrome.tabs.query({}, function(result) {\n"
     48                 "  window.domAutomationController.send('' + result.length);\n"
     49                 "});"));
     50 
     51   // An argument that isn't a string should cause a failure, not a hang.
     52   EXPECT_NONFATAL_FAILURE(browsertest_util::ExecuteScriptInBackgroundPage(
     53                               profile(),
     54                               extension->id(),
     55                               "window.domAutomationController.send(3);"),
     56                           "send(3)");
     57 }
     58 
     59 }  // namespace
     60 }  // namespace extensions
     61