Upstream version 8.37.186.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core_internal / javatests / src / org / xwalk / core / internal / xwview / test / SetNetworkAvailableTest.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013-2014 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.internal.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.internal.XWalkClient;
15 import org.xwalk.core.internal.XWalkViewInternal;
16
17 /**
18  * Test case for XWalkViewInternal.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 XWalkViewInternalTestBase {
25     @Override
26     public void setUp() throws Exception {
27         super.setUp();
28     }
29
30     @Feature({"SetNetworkAvailableTest"})
31     @SmallTest
32     public void testSetNetworkAvailableTest() throws Throwable {
33         final String code = "navigator.onLine";
34         loadAssetFile("navigator.online.html");
35         String title = getTitleOnUiThread();
36
37         final XWalkViewInternal xwView = getXWalkView();
38
39         if ("true".equals(title)) {
40             getInstrumentation().runOnMainSync(new Runnable() {
41                 @Override
42                 public void run() {
43                     // Forcing to trigger 'offline' event.
44                     xwView.setNetworkAvailable(false);
45                 }
46             });
47
48             /**
49              * Expectations:
50              * 1. navigator.onLine is false;
51              * 2. window.onoffline event is fired.
52              */
53             assertEquals("false", executeJavaScriptAndWaitForResult(code));
54             assertEquals("offline:false", getTitleOnUiThread());
55
56             getInstrumentation().runOnMainSync(new Runnable() {
57                 @Override
58                 public void run() {
59                     // Forcing to trigger 'online' event.
60                     xwView.setNetworkAvailable(true);
61                 }
62             });
63
64             /**
65              * Expectations:
66              * 1. navigator.onLine is true;
67              * 2. window.ononline event is fired.
68              */
69             assertEquals("true", executeJavaScriptAndWaitForResult(code));
70             assertEquals("online:true", getTitleOnUiThread());
71         }
72
73         if ("false".equals(title)) {
74              getInstrumentation().runOnMainSync(new Runnable() {
75                  @Override
76                  public void run() {
77                      // Forcing to trigger 'online' event.
78                      xwView.setNetworkAvailable(true);
79                  }
80              });
81
82             /**
83              * Expectations:
84              * 1. navigator.onLine is true;
85              * 2. window.ononline event is fired.
86              */
87             assertEquals("true", executeJavaScriptAndWaitForResult(code));
88             assertEquals("online:true", getTitleOnUiThread());
89
90             getInstrumentation().runOnMainSync(new Runnable() {
91                 @Override
92                 public void run() {
93                     // Forcing to trigger 'offline' event.
94                     xwView.setNetworkAvailable(false);
95                 }
96             });
97
98             /**
99              * Expectations:
100              * 1. navigator.onLine is false;
101              * 2. window.onoffline event is fired.
102              */
103             assertEquals("false", executeJavaScriptAndWaitForResult(code));
104             assertEquals("offline:false", getTitleOnUiThread());
105         }
106     }
107 }