Upstream version 8.36.161.0
[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.XWalkView;
15 import org.xwalk.core.internal.XWalkClient;
16
17 /**
18  * Test case for XWalkView.setNetworkAvailable method
19  *
20  * Once setNetworkAvailable is called, the navigator.onLine property will be
21  * set, and window.ononline/onoffline event will be fired if the property is
22  * changed.
23  */
24 public class SetNetworkAvailableTest extends XWalkViewTestBase {
25     @Override
26     public void setUp() throws Exception {
27         super.setUp();
28
29         setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
30     }
31
32     @Feature({"SetNetworkAvailableTest"})
33     @SmallTest
34     public void testSetNetworkAvailableTest() throws Throwable {
35         final String code = "navigator.onLine";
36         loadAssetFile("navigator.online.html");
37         String title = getTitleOnUiThread();
38
39         final XWalkView xwView = getXWalkView();
40
41         if ("true".equals(title)) {
42             getInstrumentation().runOnMainSync(new Runnable() {
43                 @Override
44                 public void run() {
45                     // Forcing to trigger 'offline' event.
46                     xwView.setNetworkAvailable(false);
47                 }
48             });
49
50             /**
51              * Expectations:
52              * 1. navigator.onLine is false;
53              * 2. window.onoffline event is fired.
54              */
55             assertEquals("false", executeJavaScriptAndWaitForResult(code));
56             assertEquals("offline:false", getTitleOnUiThread());
57
58             getInstrumentation().runOnMainSync(new Runnable() {
59                 @Override
60                 public void run() {
61                     // Forcing to trigger 'online' event.
62                     xwView.setNetworkAvailable(true);
63                 }
64             });
65
66             /**
67              * Expectations:
68              * 1. navigator.onLine is true;
69              * 2. window.ononline event is fired.
70              */
71             assertEquals("true", executeJavaScriptAndWaitForResult(code));
72             assertEquals("online:true", getTitleOnUiThread());
73         }
74
75         if ("false".equals(title)) {
76              getInstrumentation().runOnMainSync(new Runnable() {
77                  @Override
78                  public void run() {
79                      // Forcing to trigger 'online' event.
80                      xwView.setNetworkAvailable(true);
81                  }
82              });
83
84             /**
85              * Expectations:
86              * 1. navigator.onLine is true;
87              * 2. window.ononline event is fired.
88              */
89             assertEquals("true", executeJavaScriptAndWaitForResult(code));
90             assertEquals("online:true", getTitleOnUiThread());
91
92             getInstrumentation().runOnMainSync(new Runnable() {
93                 @Override
94                 public void run() {
95                     // Forcing to trigger 'offline' event.
96                     xwView.setNetworkAvailable(false);
97                 }
98             });
99
100             /**
101              * Expectations:
102              * 1. navigator.onLine is false;
103              * 2. window.onoffline event is fired.
104              */
105             assertEquals("false", executeJavaScriptAndWaitForResult(code));
106             assertEquals("offline:false", getTitleOnUiThread());
107         }
108     }
109 }