Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / test / network_change_notifier_util.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "network_change_notifier_util.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "jni/NetworkChangeNotifierUtil_jni.h"
12 #include "net/base/network_change_notifier.h"
13
14 namespace cronet {
15
16 namespace {
17
18 // An IPAddressObserver to test whether Cronet can receive notification
19 // when network changes.
20 class TestIPAddressObserver
21     : public net::NetworkChangeNotifier::IPAddressObserver {
22  public:
23   TestIPAddressObserver() : ip_address_changed_(false) {
24   }
25
26   ~TestIPAddressObserver() override {}
27
28   // net::NetworkChangeNotifier::IPAddressObserver implementation.
29   void OnIPAddressChanged() override {
30     ip_address_changed_ = true;
31   }
32
33   bool ip_address_changed() const {
34     return ip_address_changed_;
35   }
36
37  private:
38   bool ip_address_changed_;
39
40   DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver);
41 };
42
43 }  // namespace
44
45 // Adds a TestIPAddressObserver to the list of IPAddressObservers, and returns
46 // a boolean indicating whether the TestIPAddressObserver has received
47 // notification when network changes.
48 static jboolean IsTestIPAddressObserverCalled(JNIEnv* jenv, jclass jcaller) {
49   // This method is called on a Java thread with no MessageLoop, but we need
50   // one for the NetworkChangeNotifier to call the observer on.
51   base::MessageLoop loop;
52   TestIPAddressObserver test_observer;
53   net::NetworkChangeNotifier::AddIPAddressObserver(&test_observer);
54   net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
55
56   base::RunLoop().RunUntilIdle();
57   net::NetworkChangeNotifier::RemoveIPAddressObserver(&test_observer);
58
59   return test_observer.ip_address_changed();
60 }
61
62 bool RegisterNetworkChangeNotifierUtil(JNIEnv* jenv) {
63   return RegisterNativesImpl(jenv);
64 }
65
66 }  // namespace cronet