"Fossies" - the Fresh Open Source Software Archive

Member "google-gadgets-for-linux-0.11.2/extensions/qt_script_runtime/tests/cross_context_test.cc" (15 Mar 2009, 3980 Bytes) of package /linux/misc/old/google-gadgets-for-linux-0.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   Copyright 2008 Google Inc.
    3 
    4   Licensed under the Apache License, Version 2.0 (the "License");
    5   you may not use this file except in compliance with the License.
    6   You may obtain a copy of the License at
    7 
    8        http://www.apache.org/licenses/LICENSE-2.0
    9 
   10   Unless required by applicable law or agreed to in writing, software
   11   distributed under the License is distributed on an "AS IS" BASIS,
   12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13   See the License for the specific language governing permissions and
   14   limitations under the License.
   15 */
   16 
   17 #include <iostream>
   18 #include <ggadget/scriptable_helper.h>
   19 #include <ggadget/memory_options.h>
   20 #include <ggadget/scriptable_options.h>
   21 #include <unittest/gtest.h>
   22 #include "../js_script_context.h"
   23 #include "../js_script_runtime.h"
   24 
   25 using namespace ggadget;
   26 using namespace ggadget::qt;
   27 
   28 class Scriptable1 : public ScriptableHelperNativeOwnedDefault {
   29  public:
   30   DEFINE_CLASS_ID(0x1111111111111111, ScriptableInterface);
   31   virtual bool IsStrict() const { return false; }
   32 };
   33 
   34 class Scriptable2 : public ScriptableHelperDefault {
   35  public:
   36   DEFINE_CLASS_ID(0x2222222222222222, ScriptableInterface);
   37   virtual bool IsStrict() const { return false; }
   38 };
   39 
   40 class Scriptable3 : public ScriptableHelperDefault {
   41  public:
   42   DEFINE_CLASS_ID(0x3333333333333333, ScriptableInterface);
   43   virtual bool IsStrict() const { return false; }
   44 };
   45 
   46 ScriptableInterface *Constructor() { return new Scriptable3(); }
   47 MemoryOptions data;
   48 ScriptableOptions options(&data, true);
   49 ScriptableOptions *GetData() {  return &options; }
   50 void Print(const char *str) {
   51   std::cout << str << "\n";
   52 }
   53 void Assert(bool value) {
   54   ASSERT_TRUE(value);
   55 }
   56 TEST(ShareNativeObject, Test) {
   57   JSScriptRuntime *runtime = new JSScriptRuntime();
   58   ScriptContextInterface *ctx1 = runtime->CreateContext();
   59   ScriptContextInterface *ctx2 = runtime->CreateContext();
   60   ASSERT_TRUE(ctx1 && ctx2);
   61 
   62   Scriptable1 *global1 = new Scriptable1();
   63   Scriptable2 *global2 = new Scriptable2();
   64   ctx1->SetGlobalObject(global1);
   65   ctx2->SetGlobalObject(global2);
   66 
   67   global1->RegisterProperty("data", NewSlot(GetData), NULL);
   68 //  global2->RegisterProperty("data", NewSlot(GetData), NULL);
   69   ctx2->AssignFromNative(NULL, "", "data", Variant(&options));
   70   global1->RegisterMethod("print", NewSlot(Print));
   71   global2->RegisterMethod("print", NewSlot(Print));
   72   global1->RegisterMethod("assert", NewSlot(Assert));
   73   global2->RegisterMethod("assert", NewSlot(Assert));
   74 
   75   ctx1->Execute("data.putValue('name', 'tiger');", NULL, 0);
   76   ctx1->Execute("assert(data.getValue('name') == 'tiger');", NULL, 0);
   77   ctx2->Execute("assert(data.getValue('name') == 'tiger');", NULL, 0);
   78 
   79   // build an object in ctx1
   80   ctx1->Execute("function MyObj() { this.value = 'google'; }", NULL, 0);
   81   ctx1->Execute("var obj = new MyObj()", NULL, 0);
   82   ctx1->Execute("data.putValue('obj', obj);", NULL, 0);
   83 
   84   ctx1->Execute("var r = data.getValue('obj');",  NULL, 0);
   85   ctx1->Execute("assert(r.value == 'google');", NULL, 0);
   86 
   87   ctx2->Execute("var r = data.getValue('obj');",  NULL, 0);
   88   ctx2->Execute("assert(r.value == 'google');", NULL, 0);
   89 
   90   // change object property from native context
   91   ctx1->Execute("r.value = 'Beijing';",  NULL, 0);
   92 
   93   ctx1->Execute("assert(r.value == 'Beijing');", NULL, 0);
   94   ctx2->Execute("assert(r.value == 'Beijing');", NULL, 0);
   95 
   96   // change object property from foreign context
   97   ctx2->Execute("r.value = 'linux';",  NULL, 0);
   98 
   99   ctx1->Execute("assert(r.value == 'linux');", NULL, 0);
  100   ctx2->Execute("assert(r.value == 'linux');", NULL, 0);
  101 
  102   // change object property from native context, again
  103   ctx1->Execute("r.value = 'Beijing';",  NULL, 0);
  104 
  105   ctx1->Execute("assert(r.value == 'Beijing');", NULL, 0);
  106   ctx2->Execute("assert(r.value == 'Beijing');", NULL, 0);
  107 
  108   ctx1->Destroy();
  109   ctx2->Destroy();
  110   delete runtime;
  111 }
  112 
  113 int main(int argc, char *argv[]) {
  114   setlocale(LC_ALL, "");
  115   testing::ParseGTestFlags(&argc, argv);
  116   return RUN_ALL_TESTS();
  117 }