Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "chrome/common/extensions/manifest.h"
     12 
     13 class AsyncExtensionFunction;
     14 class Browser;
     15 class UIThreadExtensionFunction;
     16 
     17 namespace base {
     18 class Value;
     19 class DictionaryValue;
     20 class ListValue;
     21 }
     22 
     23 namespace extensions {
     24 class Extension;
     25 }
     26 
     27 namespace extension_function_test_utils {
     28 
     29 // Parse JSON and return as the specified type, or NULL if the JSON is invalid
     30 // or not the specified type.
     31 base::Value* ParseJSON(const std::string& data);
     32 base::ListValue* ParseList(const std::string& data);
     33 base::DictionaryValue* ParseDictionary(const std::string& data);
     34 
     35 // Get |key| from |val| as the specified type. If |key| does not exist, or is
     36 // not of the specified type, adds a failure to the current test and returns
     37 // false, 0, empty string, etc.
     38 bool GetBoolean(base::DictionaryValue* val, const std::string& key);
     39 int GetInteger(base::DictionaryValue* val, const std::string& key);
     40 std::string GetString(base::DictionaryValue* val, const std::string& key);
     41 
     42 // If |val| is a dictionary, return it as one, otherwise NULL.
     43 base::DictionaryValue* ToDictionary(base::Value* val);
     44 
     45 // If |val| is a list, return it as one, otherwise NULL.
     46 base::ListValue* ToList(base::Value* val);
     47 
     48 // Creates an extension instance that can be attached to an ExtensionFunction
     49 // before running it.
     50 scoped_refptr<extensions::Extension> CreateEmptyExtension();
     51 
     52 // Creates an extension instance with a specified location that can be attached
     53 // to an ExtensionFunction before running.
     54 scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation(
     55     extensions::Manifest::Location location);
     56 
     57 // Creates an empty extension with a variable ID, for tests that require
     58 // multiple extensions side-by-side having distinct IDs. If not empty, then
     59 // id_input is passed directly to Extension::CreateId() and thus has the same
     60 // behavior as that method. If id_input is empty, then Extension::Create()
     61 // receives an empty explicit ID and generates an appropriate ID for a blank
     62 // extension.
     63 scoped_refptr<extensions::Extension> CreateEmptyExtension(
     64     const std::string& id_input);
     65 
     66 scoped_refptr<extensions::Extension> CreateExtension(
     67     extensions::Manifest::Location location,
     68     base::DictionaryValue* test_extension_value,
     69     const std::string& id_input);
     70 
     71 // Creates an extension instance with a specified extension value that can be
     72 // attached to an ExtensionFunction before running.
     73 scoped_refptr<extensions::Extension> CreateExtension(
     74     base::DictionaryValue* test_extension_value);
     75 
     76 scoped_refptr<extensions::Extension> CreateExtension(
     77     extensions::Manifest::Location location,
     78     base::DictionaryValue* test_extension_value);
     79 
     80 // Returns true if |val| contains privacy information, e.g. url,
     81 // title, and faviconUrl.
     82 bool HasPrivacySensitiveFields(base::DictionaryValue* val);
     83 
     84 enum RunFunctionFlags {
     85   NONE = 0,
     86   INCLUDE_INCOGNITO = 1 << 0
     87 };
     88 
     89 // Run |function| with |args| and return the resulting error. Adds an error to
     90 // the current test if |function| returns a result. Takes ownership of
     91 // |function|.
     92 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
     93                                       const std::string& args,
     94                                       Browser* browser,
     95                                       RunFunctionFlags flags);
     96 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
     97                                       const std::string& args,
     98                                       Browser* browser);
     99 
    100 // Run |function| with |args| and return the result. Adds an error to the
    101 // current test if |function| returns an error. Takes ownership of
    102 // |function|. The caller takes ownership of the result.
    103 base::Value* RunFunctionAndReturnSingleResult(
    104     UIThreadExtensionFunction* function,
    105     const std::string& args,
    106     Browser* browser,
    107     RunFunctionFlags flags);
    108 base::Value* RunFunctionAndReturnSingleResult(
    109     UIThreadExtensionFunction* function,
    110     const std::string& args,
    111     Browser* browser);
    112 
    113 // Create and run |function| with |args|. Works with both synchronous and async
    114 // functions. Ownership of |function| remains with the caller.
    115 //
    116 // TODO(aa): It would be nice if |args| could be validated against the schema
    117 // that |function| expects. That way, we know that we are testing something
    118 // close to what the bindings would actually send.
    119 //
    120 // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
    121 // we're going to need to frob for all the different extension functions. But
    122 // we can refactor when we see what is needed.
    123 bool RunFunction(UIThreadExtensionFunction* function,
    124                  const std::string& args,
    125                  Browser* browser,
    126                  RunFunctionFlags flags);
    127 
    128 } // namespace extension_function_test_utils
    129 
    130 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_
    131