294246dd83e208309b9ab5e9c3bbe6f6b0a68270
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / SetNetworkAvailableTest.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 package org.xwalk.core.xwview.test;
7
8 import android.graphics.Bitmap;
9 import android.test.suitebuilder.annotation.SmallTest;
10 import android.util.Log;
11 import org.chromium.base.test.util.DisabledTest;
12 import org.chromium.base.test.util.Feature;
13
14 import org.xwalk.core.XWalkClient;
15 import org.xwalk.core.XWalkContent;
16 import org.xwalk.core.XWalkView;
17
18 /**
19  * Test case for XWalkView.setNetworkAvailable method
20  *
21  * Once setNetworkAvailable is called, the navigator.onLine property will be
22  * set, and window.ononline/onoffline event will be fired if the property is
23  * changed.
24  */
25 public class SetNetworkAvailableTest extends XWalkViewTestBase {
26     @Override
27     public void setUp() throws Exception {
28         super.setUp();
29
30         setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
31     }
32
33     @Feature({"SetNetworkAvailableTest"})
34     @SmallTest
35     public void testSetNetworkAvailableTest() throws Throwable {
36         final String code = "navigator.onLine";
37         loadAssetFile("navigator.online.html");
38         String title = getTitleOnUiThread();
39
40         final XWalkView xwView = getXWalkView();
41
42         if ("true".equals(title)) {
43             getInstrumentation().runOnMainSync(new Runnable() {
44                 @Override
45                 public void run() {
46                     // Forcing to trigger 'offline' event.
47                     xwView.setNetworkAvailable(false);
48                 }
49             });
50
51             /**
52              * Expectations:
53              * 1. navigator.onLine is false;
54              * 2. window.onoffline event is fired.
55              */
56             assertEquals("false", executeJavaScriptAndWaitForResult(code));
57             assertEquals("offline:false", getTitleOnUiThread());
58
59             getInstrumentation().runOnMainSync(new Runnable() {
60                 @Override
61                 public void run() {
62                     // Forcing to trigger 'online' event.
63                     xwView.setNetworkAvailable(true);
64                 }
65             });
66
67             /**
68              * Expectations:
69              * 1. navigator.onLine is true;
70              * 2. window.ononline event is fired.
71              */
72             assertEquals("true", executeJavaScriptAndWaitForResult(code));
73             assertEquals("online:true", getTitleOnUiThread());
74         }
75
76         if ("false".equals(title)) {
77              getInstrumentation().runOnMainSync(new Runnable() {
78                  @Override
79                  public void run() {
80                      // Forcing to trigger 'online' event.
81                      xwView.setNetworkAvailable(true);
82                  }
83              });
84
85             /**
86              * Expectations:
87              * 1. navigator.onLine is true;
88              * 2. window.ononline event is fired.
89              */
90             assertEquals("true", executeJavaScriptAndWaitForResult(code));
91             assertEquals("online:true", getTitleOnUiThread());
92
93             getInstrumentation().runOnMainSync(new Runnable() {
94                 @Override
95                 public void run() {
96                     // Forcing to trigger 'offline' event.
97                     xwView.setNetworkAvailable(false);
98                 }
99             });
100
101             /**
102              * Expectations:
103              * 1. navigator.onLine is false;
104              * 2. window.onoffline event is fired.
105              */
106             assertEquals("false", executeJavaScriptAndWaitForResult(code));
107             assertEquals("offline:false", getTitleOnUiThread());
108         }
109     }
110 }