OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" | 5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
12 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 12 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
13 #include "chrome/browser/chromeos/contacts/contact_store_observer.h" | 13 #include "chrome/browser/chromeos/contacts/contact_store_observer.h" |
14 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | 14 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
15 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" | 15 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" |
16 #include "chrome/browser/chromeos/gdata/gdata_contacts_service_stub.h" | 16 #include "chrome/browser/chromeos/gdata/gdata_contacts_service_stub.h" |
17 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 17 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 20 #include "net/base/network_change_notifier.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 using content::BrowserThread; | 23 using content::BrowserThread; |
23 | 24 |
24 namespace contacts { | 25 namespace contacts { |
25 namespace test { | 26 namespace test { |
26 | 27 |
27 // ContactStoreObserver implementation that just counts the number of times | 28 // ContactStoreObserver implementation that just counts the number of times |
28 // that it's been told that a store has been updated. | 29 // that it's been told that a store has been updated. |
29 class TestContactStoreObserver : public ContactStoreObserver { | 30 class TestContactStoreObserver : public ContactStoreObserver { |
(...skipping 18 matching lines...) Expand all Loading... |
48 }; | 49 }; |
49 | 50 |
50 class GoogleContactStoreTest : public testing::Test { | 51 class GoogleContactStoreTest : public testing::Test { |
51 public: | 52 public: |
52 GoogleContactStoreTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} | 53 GoogleContactStoreTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
53 virtual ~GoogleContactStoreTest() {} | 54 virtual ~GoogleContactStoreTest() {} |
54 | 55 |
55 protected: | 56 protected: |
56 // testing::Test implementation. | 57 // testing::Test implementation. |
57 virtual void SetUp() OVERRIDE { | 58 virtual void SetUp() OVERRIDE { |
58 testing::Test::SetUp(); | 59 // Create a mock NetworkChangeNotifier so the store won't be notified about |
| 60 // changes to the system's actual network state. |
| 61 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
59 profile_.reset(new TestingProfile); | 62 profile_.reset(new TestingProfile); |
60 | 63 |
61 store_.reset(new GoogleContactStore(profile_.get())); | 64 store_.reset(new GoogleContactStore(profile_.get())); |
62 store_->AddObserver(&observer_); | 65 store_->AddObserver(&observer_); |
63 | 66 |
64 test_api_.reset(new GoogleContactStore::TestAPI(store_.get())); | 67 test_api_.reset(new GoogleContactStore::TestAPI(store_.get())); |
65 | 68 |
66 db_ = new FakeContactDatabase; | 69 db_ = new FakeContactDatabase; |
67 test_api_->SetDatabase(db_); | 70 test_api_->SetDatabase(db_); |
68 | 71 |
69 gdata_service_ = new gdata::GDataContactsServiceStub; | 72 gdata_service_ = new gdata::GDataContactsServiceStub; |
70 test_api_->SetGDataService(gdata_service_); | 73 test_api_->SetGDataService(gdata_service_); |
71 } | 74 } |
72 | 75 |
73 MessageLoopForUI message_loop_; | 76 MessageLoopForUI message_loop_; |
74 content::TestBrowserThread ui_thread_; | 77 content::TestBrowserThread ui_thread_; |
75 | 78 |
76 TestContactStoreObserver observer_; | 79 TestContactStoreObserver observer_; |
| 80 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
77 scoped_ptr<TestingProfile> profile_; | 81 scoped_ptr<TestingProfile> profile_; |
78 scoped_ptr<GoogleContactStore> store_; | 82 scoped_ptr<GoogleContactStore> store_; |
79 scoped_ptr<GoogleContactStore::TestAPI> test_api_; | 83 scoped_ptr<GoogleContactStore::TestAPI> test_api_; |
80 | 84 |
81 FakeContactDatabase* db_; // not owned | 85 FakeContactDatabase* db_; // not owned |
82 gdata::GDataContactsServiceStub* gdata_service_; // not owned | 86 gdata::GDataContactsServiceStub* gdata_service_; // not owned |
83 | 87 |
84 private: | 88 private: |
85 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreTest); | 89 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreTest); |
86 }; | 90 }; |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 // initialization failed) but we should still fetch the second one from GData | 374 // initialization failed) but we should still fetch the second one from GData |
371 // and schedule an update. | 375 // and schedule an update. |
372 store_->Init(); | 376 store_->Init(); |
373 ContactPointers loaded_contacts; | 377 ContactPointers loaded_contacts; |
374 store_->AppendContacts(&loaded_contacts); | 378 store_->AppendContacts(&loaded_contacts); |
375 EXPECT_EQ(ContactsToString(gdata_contacts), | 379 EXPECT_EQ(ContactsToString(gdata_contacts), |
376 ContactsToString(loaded_contacts)); | 380 ContactsToString(loaded_contacts)); |
377 EXPECT_TRUE(test_api_->update_scheduled()); | 381 EXPECT_TRUE(test_api_->update_scheduled()); |
378 } | 382 } |
379 | 383 |
| 384 TEST_F(GoogleContactStoreTest, AvoidUpdatesWhenOffline) { |
| 385 EXPECT_EQ(0, gdata_service_->num_download_requests()); |
| 386 |
| 387 // Notify the store that we're offline. Init() shouldn't attempt an update |
| 388 // and the update timer shouldn't be running. |
| 389 test_api_->NotifyAboutNetworkStateChange(false); |
| 390 store_->Init(); |
| 391 EXPECT_EQ(0, gdata_service_->num_download_requests()); |
| 392 EXPECT_FALSE(test_api_->update_scheduled()); |
| 393 |
| 394 // We should do an update and schedule further updates as soon as we go |
| 395 // online. |
| 396 gdata_service_->reset_stats(); |
| 397 test_api_->NotifyAboutNetworkStateChange(true); |
| 398 EXPECT_EQ(1, gdata_service_->num_download_requests()); |
| 399 EXPECT_TRUE(test_api_->update_scheduled()); |
| 400 |
| 401 // If we call DoUpdate() to mimic the code path that's used for a timer-driven |
| 402 // update while we're offline, we should again defer the update. |
| 403 gdata_service_->reset_stats(); |
| 404 test_api_->NotifyAboutNetworkStateChange(false); |
| 405 test_api_->DoUpdate(); |
| 406 EXPECT_EQ(0, gdata_service_->num_download_requests()); |
| 407 |
| 408 // When we're back online, the update should happen. |
| 409 gdata_service_->reset_stats(); |
| 410 test_api_->NotifyAboutNetworkStateChange(true); |
| 411 EXPECT_EQ(1, gdata_service_->num_download_requests()); |
| 412 } |
| 413 |
380 } // namespace test | 414 } // namespace test |
381 } // namespace contacts | 415 } // namespace contacts |
OLD | NEW |