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