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.
5 #include "network_change_notifier_util.h"
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"
18 // An IPAddressObserver to test whether Cronet can receive notification
19 // when network changes.
20 class TestIPAddressObserver
21 : public net::NetworkChangeNotifier::IPAddressObserver
{
23 TestIPAddressObserver() : ip_address_changed_(false) {
26 ~TestIPAddressObserver() override
{}
28 // net::NetworkChangeNotifier::IPAddressObserver implementation.
29 void OnIPAddressChanged() override
{
30 ip_address_changed_
= true;
33 bool ip_address_changed() const {
34 return ip_address_changed_
;
38 bool ip_address_changed_
;
40 DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver
);
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();
56 base::RunLoop().RunUntilIdle();
57 net::NetworkChangeNotifier::RemoveIPAddressObserver(&test_observer
);
59 return test_observer
.ip_address_changed();
62 bool RegisterNetworkChangeNotifierUtil(JNIEnv
* jenv
) {
63 return RegisterNativesImpl(jenv
);