Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cronet / android / test / network_change_notifier_util.cc
blobb6444aa11967b808b92c74d7ee5b8bfd670872ce
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"
14 namespace cronet {
16 namespace {
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) {
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_;
37 private:
38 bool ip_address_changed_;
40 DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver);
43 } // namespace
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(
49 JNIEnv* jenv,
50 const JavaParamRef<jclass>& jcaller) {
51 // This method is called on a Java thread with no MessageLoop, but we need
52 // one for the NetworkChangeNotifier to call the observer on.
53 base::MessageLoop loop;
54 TestIPAddressObserver test_observer;
55 net::NetworkChangeNotifier::AddIPAddressObserver(&test_observer);
56 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
58 base::RunLoop().RunUntilIdle();
59 net::NetworkChangeNotifier::RemoveIPAddressObserver(&test_observer);
61 return test_observer.ip_address_changed();
64 bool RegisterNetworkChangeNotifierUtil(JNIEnv* jenv) {
65 return RegisterNativesImpl(jenv);
68 } // namespace cronet