Home | History | Annotate | Download | only in manifest_handlers
      1 // Copyright 2014 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/strings/utf_string_conversions.h"
      6 #include "chrome/common/extensions/manifest_handlers/automation.h"
      7 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
      8 #include "extensions/common/error_utils.h"
      9 #include "extensions/common/manifest_constants.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace extensions {
     13 
     14 class AutomationManifestTest : public ExtensionManifestTest {
     15  public:
     16   AutomationManifestTest() : channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
     17 
     18  protected:
     19   AutomationInfo* GetAutomationInfo(scoped_refptr<Extension> extension) {
     20     return static_cast<AutomationInfo*>(
     21         extension->GetManifestData(manifest_keys::kAutomation));
     22   }
     23 
     24  private:
     25   ScopedCurrentChannel channel_;
     26 };
     27 
     28 TEST_F(AutomationManifestTest, AsBooleanFalse) {
     29   scoped_refptr<Extension> extension =
     30       LoadAndExpectSuccess("automation_boolean_false.json");
     31   ASSERT_TRUE(extension.get());
     32 
     33   const AutomationInfo* info = AutomationInfo::Get(extension.get());
     34   ASSERT_FALSE(info);
     35 }
     36 
     37 TEST_F(AutomationManifestTest, AsBooleanTrue) {
     38   scoped_refptr<Extension> extension =
     39       LoadAndExpectSuccess("automation_boolean_true.json");
     40   ASSERT_TRUE(extension.get());
     41 
     42   const AutomationInfo* info = AutomationInfo::Get(extension.get());
     43   ASSERT_TRUE(info);
     44 
     45   EXPECT_FALSE(info->desktop);
     46   EXPECT_FALSE(info->interact);
     47   EXPECT_FALSE(info->specified_matches);
     48   EXPECT_TRUE(info->matches.is_empty());
     49 }
     50 
     51 TEST_F(AutomationManifestTest, InteractTrue) {
     52   scoped_refptr<Extension> extension =
     53       LoadAndExpectSuccess("automation_interact_true.json");
     54   ASSERT_TRUE(extension.get());
     55 
     56   const AutomationInfo* info = AutomationInfo::Get(extension.get());
     57   ASSERT_TRUE(info);
     58 
     59   EXPECT_FALSE(info->desktop);
     60   EXPECT_TRUE(info->interact);
     61   EXPECT_FALSE(info->specified_matches);
     62   EXPECT_TRUE(info->matches.is_empty());
     63 }
     64 
     65 TEST_F(AutomationManifestTest, Matches) {
     66   scoped_refptr<Extension> extension = LoadAndExpectWarning(
     67       "automation_matches.json",
     68       ErrorUtils::FormatErrorMessage(
     69           automation_errors::kErrorInvalidMatch,
     70           "www.badpattern.com",
     71           URLPattern::GetParseResultString(
     72               URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR)));
     73   ASSERT_TRUE(extension.get());
     74 
     75   const AutomationInfo* info = AutomationInfo::Get(extension.get());
     76   ASSERT_TRUE(info);
     77 
     78   EXPECT_FALSE(info->desktop);
     79   EXPECT_FALSE(info->interact);
     80   EXPECT_TRUE(info->specified_matches);
     81   EXPECT_FALSE(info->matches.is_empty());
     82 
     83   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.google.com/")));
     84   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.google.com")));
     85   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com/")));
     86   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com")));
     87 
     88   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com/")));
     89   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com")));
     90 }
     91 
     92 TEST_F(AutomationManifestTest, EmptyMatches) {
     93   scoped_refptr<Extension> extension =
     94       LoadAndExpectWarning("automation_empty_matches.json",
     95                            automation_errors::kErrorNoMatchesProvided);
     96   ASSERT_TRUE(extension.get());
     97 
     98   const AutomationInfo* info = AutomationInfo::Get(extension.get());
     99   ASSERT_TRUE(info);
    100 
    101   EXPECT_FALSE(info->desktop);
    102   EXPECT_FALSE(info->interact);
    103   EXPECT_TRUE(info->specified_matches);
    104   EXPECT_TRUE(info->matches.is_empty());
    105 }
    106 
    107 TEST_F(AutomationManifestTest, NoValidMatches) {
    108   std::string error;
    109   scoped_refptr<Extension> extension =
    110       LoadExtension(Manifest("automation_no_valid_matches.json"), &error);
    111   ASSERT_TRUE(extension.get());
    112   EXPECT_EQ("", error);
    113   EXPECT_EQ(2u, extension->install_warnings().size());
    114   EXPECT_EQ(ErrorUtils::FormatErrorMessage(
    115                 automation_errors::kErrorInvalidMatch,
    116                 "www.badpattern.com",
    117                 URLPattern::GetParseResultString(
    118                     URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR)),
    119             extension->install_warnings()[0].message);
    120   EXPECT_EQ(automation_errors::kErrorNoMatchesProvided,
    121             extension->install_warnings()[1].message);
    122 
    123   const AutomationInfo* info = AutomationInfo::Get(extension.get());
    124   ASSERT_TRUE(info);
    125 
    126   EXPECT_FALSE(info->desktop);
    127   EXPECT_FALSE(info->interact);
    128   EXPECT_TRUE(info->specified_matches);
    129   EXPECT_TRUE(info->matches.is_empty());
    130 }
    131 
    132 TEST_F(AutomationManifestTest, DesktopFalse) {
    133   scoped_refptr<Extension> extension =
    134       LoadAndExpectSuccess("automation_desktop_false.json");
    135   ASSERT_TRUE(extension.get());
    136 
    137   const AutomationInfo* info = AutomationInfo::Get(extension.get());
    138   ASSERT_TRUE(info);
    139 
    140   EXPECT_FALSE(info->desktop);
    141   EXPECT_FALSE(info->interact);
    142   EXPECT_FALSE(info->specified_matches);
    143   EXPECT_TRUE(info->matches.is_empty());
    144 }
    145 
    146 TEST_F(AutomationManifestTest, DesktopTrue) {
    147   scoped_refptr<Extension> extension =
    148       LoadAndExpectSuccess("automation_desktop_true.json");
    149   ASSERT_TRUE(extension.get());
    150 
    151   const AutomationInfo* info = AutomationInfo::Get(extension.get());
    152   ASSERT_TRUE(info);
    153 
    154   EXPECT_TRUE(info->desktop);
    155   EXPECT_TRUE(info->interact);
    156   EXPECT_FALSE(info->specified_matches);
    157   EXPECT_TRUE(info->matches.is_empty());
    158 }
    159 
    160 TEST_F(AutomationManifestTest, Desktop_InteractTrue) {
    161   scoped_refptr<Extension> extension =
    162       LoadAndExpectSuccess("automation_desktop_interact_true.json");
    163   ASSERT_TRUE(extension.get());
    164 
    165   const AutomationInfo* info = AutomationInfo::Get(extension.get());
    166   ASSERT_TRUE(info);
    167 
    168   EXPECT_TRUE(info->desktop);
    169   EXPECT_TRUE(info->interact);
    170   EXPECT_FALSE(info->specified_matches);
    171   EXPECT_TRUE(info->matches.is_empty());
    172 }
    173 
    174 TEST_F(AutomationManifestTest, Desktop_InteractFalse) {
    175   scoped_refptr<Extension> extension =
    176       LoadAndExpectWarning("automation_desktop_interact_false.json",
    177                            automation_errors::kErrorDesktopTrueInteractFalse);
    178   ASSERT_TRUE(extension.get());
    179 
    180   const AutomationInfo* info = AutomationInfo::Get(extension.get());
    181   ASSERT_TRUE(info);
    182 
    183   EXPECT_TRUE(info->desktop);
    184   EXPECT_TRUE(info->interact);
    185   EXPECT_FALSE(info->specified_matches);
    186   EXPECT_TRUE(info->matches.is_empty());
    187 }
    188 
    189 TEST_F(AutomationManifestTest, Desktop_MatchesSpecified) {
    190   scoped_refptr<Extension> extension = LoadAndExpectWarning(
    191       "automation_desktop_matches_specified.json",
    192       automation_errors::kErrorDesktopTrueMatchesSpecified);
    193   ASSERT_TRUE(extension.get());
    194 
    195   const AutomationInfo* info = AutomationInfo::Get(extension.get());
    196   ASSERT_TRUE(info);
    197 
    198   EXPECT_TRUE(info->desktop);
    199   EXPECT_TRUE(info->interact);
    200   EXPECT_FALSE(info->specified_matches);
    201   EXPECT_TRUE(info->matches.is_empty());
    202 }
    203 
    204 }  // namespace extensions
    205