Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-microtask-delivery.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #include "cctest.h"
31
32 using namespace v8;
33 namespace i = v8::internal;
34
35 namespace {
36 class HarmonyIsolate {
37  public:
38   HarmonyIsolate() {
39     i::FLAG_harmony_promises = true;
40     isolate_ = Isolate::New();
41     isolate_->Enter();
42   }
43
44   ~HarmonyIsolate() {
45     isolate_->Exit();
46     isolate_->Dispose();
47   }
48
49   Isolate* GetIsolate() const { return isolate_; }
50
51  private:
52   Isolate* isolate_;
53 };
54 }
55
56
57 TEST(MicrotaskDeliverySimple) {
58   HarmonyIsolate isolate;
59   HandleScope scope(isolate.GetIsolate());
60   LocalContext context(isolate.GetIsolate());
61   CompileRun(
62       "var ordering = [];"
63       "var resolver = {};"
64       "function handler(resolve) { resolver.resolve = resolve; }"
65       "var obj = {};"
66       "var observeOrders = [1, 4];"
67       "function observer() {"
68         "ordering.push(observeOrders.shift());"
69         "resolver.resolve();"
70       "}"
71       "var p = new Promise(handler);"
72       "p.then(function() {"
73         "ordering.push(2);"
74       "}).then(function() {"
75         "ordering.push(3);"
76         "obj.id++;"
77         "return new Promise(handler);"
78       "}).then(function() {"
79         "ordering.push(5);"
80       "}).then(function() {"
81         "ordering.push(6);"
82       "});"
83       "Object.observe(obj, observer);"
84       "obj.id = 1;");
85   CHECK_EQ(6, CompileRun("ordering.length")->Int32Value());
86   CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value());
87   CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value());
88   CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value());
89   CHECK_EQ(4, CompileRun("ordering[3]")->Int32Value());
90   CHECK_EQ(5, CompileRun("ordering[4]")->Int32Value());
91   CHECK_EQ(6, CompileRun("ordering[5]")->Int32Value());
92 }
93
94
95 TEST(MicrotaskPerIsolateState) {
96   HarmonyIsolate isolate;
97   HandleScope scope(isolate.GetIsolate());
98   LocalContext context1(isolate.GetIsolate());
99   isolate.GetIsolate()->SetAutorunMicrotasks(false);
100   CompileRun(
101       "var obj = { calls: 0 };");
102   Handle<Value> obj = CompileRun("obj");
103   {
104     LocalContext context2(isolate.GetIsolate());
105     context2->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
106                             obj);
107     CompileRun(
108         "var resolver = {};"
109         "new Promise(function(resolve) {"
110           "resolver.resolve = resolve;"
111         "}).then(function() {"
112           "obj.calls++;"
113         "});"
114         "(function() {"
115           "resolver.resolve();"
116         "})();");
117   }
118   {
119     LocalContext context3(isolate.GetIsolate());
120     context3->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
121                             obj);
122     CompileRun(
123         "var foo = { id: 1 };"
124         "Object.observe(foo, function() {"
125           "obj.calls++;"
126         "});"
127         "foo.id++;");
128   }
129   {
130     LocalContext context4(isolate.GetIsolate());
131     context4->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
132                             obj);
133     isolate.GetIsolate()->RunMicrotasks();
134     CHECK_EQ(2, CompileRun("obj.calls")->Int32Value());
135   }
136 }