Index: chrome/browser/printing/cloud_print/cloud_print_proxy_service_unittest.cc |
diff --git a/chrome/browser/printing/cloud_print/cloud_print_proxy_service_unittest.cc b/chrome/browser/printing/cloud_print/cloud_print_proxy_service_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a176262389686865c8822287ae706f287deffb20 |
--- /dev/null |
+++ b/chrome/browser/printing/cloud_print/cloud_print_proxy_service_unittest.cc |
@@ -0,0 +1,341 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
+#include "chrome/browser/service/service_process_control.h" |
+#include "chrome/common/cloud_print/cloud_print_proxy_info.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/common/service_messages.h" |
+#include "chrome/test/base/testing_pref_service.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/test/test_browser_thread.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using content::BrowserThread; |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
you could kill this declaration and just spell out
Scott Byer
2011/11/15 01:42:23
Done.
|
+ |
+using ::testing::Assign; |
+using ::testing::AtMost; |
+using ::testing::Invoke; |
+using ::testing::Mock; |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
needed?
Scott Byer
2011/11/15 01:42:23
Nope. Done.
|
+using ::testing::Property; |
+using ::testing::ReturnPointee; |
+using ::testing::StrictMock; |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
not used.
Scott Byer
2011/11/15 01:42:23
Done.
|
+using ::testing::WithArgs; |
+using ::testing::_; |
+ |
+class MockServiceProcessControl : public ServiceProcessControl { |
+ public: |
+ static std::string EnabledUserId(); |
+ |
+ MOCK_CONST_METHOD0(is_connected, bool()); |
+ |
+ MOCK_METHOD2(Launch, void(const base::Closure&, const base::Closure&)); |
+ MOCK_METHOD0(Disconnect, void()); |
+ |
+ MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message&)); |
+ MOCK_METHOD1(OnChannelConnected, void(int32)); |
+ MOCK_METHOD0(OnChannelError, void()); |
+ |
+ MOCK_METHOD1(Send, bool(IPC::Message*)); |
+ |
+ typedef enum { |
+ kServiceStateDisabled, |
+ kServiceStateEnabled, |
+ kServiceStateNone |
+ } ServiceState; |
+ |
+ void SetConnectSuccessMockExpectations(ServiceState state); |
+ |
+ void SetServiceEnabledExpectations(); |
+ void SetServiceDisabledExpectations(); |
+ void SetWillBeDisabledExpectations(); |
+ |
+ bool SendEnabledInfo(); |
+ bool SendDisabledInfo(); |
+ |
+ private: |
+ bool connected_; |
+ cloud_print::CloudPrintProxyInfo info_; |
+}; |
+ |
+// static |
+std::string MockServiceProcessControl::EnabledUserId() { |
+ return std::string("dorothy@somewhere.otr"); |
+} |
+ |
+void CallTask(const base::Closure& task) { |
+ if (!task.is_null()) |
+ task.Run(); |
+} |
+ |
+void MockServiceProcessControl::SetConnectSuccessMockExpectations( |
+ ServiceState service_state) { |
+ EXPECT_CALL(*this, is_connected()).WillRepeatedly(ReturnPointee(&connected_)); |
+ |
+ EXPECT_CALL(*this, Launch(_, _)) |
+ .WillRepeatedly( |
+ DoAll(Assign(&connected_, true), WithArgs<0>(Invoke(CallTask)))); |
+ EXPECT_CALL(*this, Disconnect()).Times(AtMost(1)) |
+ .WillRepeatedly(Assign(&connected_, false)); |
+ |
+ EXPECT_CALL(*this, Send(_)).Times(0); |
+ |
+ if (service_state == kServiceStateEnabled) |
+ SetServiceEnabledExpectations(); |
+ else if (service_state == kServiceStateDisabled) |
+ SetServiceDisabledExpectations(); |
+} |
+ |
+void MockServiceProcessControl::SetServiceEnabledExpectations() { |
+ EXPECT_CALL( |
+ *this, |
+ Send(Property(&IPC::Message::type, |
+ static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID)))) |
+ .Times(1).WillOnce( |
+ WithoutArgs( |
+ Invoke(this, &MockServiceProcessControl::SendEnabledInfo))); |
+} |
+ |
+void MockServiceProcessControl::SetServiceDisabledExpectations() { |
+ EXPECT_CALL( |
+ *this, |
+ Send(Property(&IPC::Message::type, |
+ static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID)))) |
+ .Times(1).WillOnce( |
+ WithoutArgs( |
+ Invoke(this, &MockServiceProcessControl::SendDisabledInfo))); |
+} |
+ |
+void MockServiceProcessControl::SetWillBeDisabledExpectations() { |
+ EXPECT_CALL( |
+ *this, |
+ Send(Property(&IPC::Message::type, |
+ static_cast<int32>(ServiceMsg_DisableCloudPrintProxy::ID)))) |
+ .Times(1); |
+} |
+ |
+bool MockServiceProcessControl::SendEnabledInfo() { |
+ info_.enabled = true; |
+ info_.email = EnabledUserId(); |
+ OnCloudPrintProxyInfo(info_); |
+ return true; |
+} |
+ |
+bool MockServiceProcessControl::SendDisabledInfo() { |
+ info_.enabled = false; |
+ info_.email = std::string(); |
+ OnCloudPrintProxyInfo(info_); |
+ return true; |
+} |
+ |
+class TestCloudPrintProxyService : public CloudPrintProxyService { |
+ public: |
+ explicit TestCloudPrintProxyService(Profile* profile) |
+ : CloudPrintProxyService(profile) { |
+ process_control_ = new MockServiceProcessControl(); |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
how about just making this a regular class member
Scott Byer
2011/11/15 01:42:23
Done.
|
+ } |
+ virtual ~TestCloudPrintProxyService() { |
+ delete process_control_; |
+ } |
+ |
+ virtual ServiceProcessControl* GetServiceProcessControl() { |
+ return process_control_; |
+ } |
+ MockServiceProcessControl* GetMockServiceProcessControl() { |
+ return process_control_; |
+ } |
+ |
+ private: |
+ MockServiceProcessControl* process_control_; |
+}; |
+ |
+class CloudPrintProxyPolicyTest : public ::testing::Test { |
+ public: |
+ CloudPrintProxyPolicyTest() |
+ : ui_thread_(BrowserThread::UI, &message_loop_) { |
+ } |
+ |
+ protected: |
+ MessageLoopForUI message_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ TestingProfile profile_; |
+}; |
+ |
+TEST_F(CloudPrintProxyPolicyTest, VerifyExpectations) { |
+ MockServiceProcessControl mock_control; |
+ mock_control.SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateNone); |
+ |
+ EXPECT_FALSE(mock_control.is_connected()); |
+ mock_control.Launch(base::Closure(), base::Closure()); |
+ EXPECT_TRUE(mock_control.is_connected()); |
+ mock_control.Launch(base::Closure(), base::Closure()); |
+ EXPECT_TRUE(mock_control.is_connected()); |
+ mock_control.Disconnect(); |
+ EXPECT_FALSE(mock_control.is_connected()); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabled) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
Any reason to wrap this in a scoped_ptr? Regular n
Scott Byer
2011/11/15 01:42:23
Done.
|
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateDisabled); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue( |
+ MockServiceProcessControl::EnabledUserId())); |
+ |
+ service->Initialize(); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabled) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
ditto (and also below).
Scott Byer
2011/11/15 01:42:23
Done.
|
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateEnabled); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue(std::string())); |
+ |
+ service->Initialize(); |
+ service->RefreshStatusFromService(); |
+ |
+ EXPECT_EQ(MockServiceProcessControl::EnabledUserId(), |
+ prefs->GetString(prefs::kCloudPrintEmail)); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyDisabled) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateDisabled); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue(std::string())); |
+ prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
+ Value::CreateBooleanValue(false)); |
+ |
+ service->Initialize(); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyEnabled) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateEnabled); |
+ service->GetMockServiceProcessControl()->SetWillBeDisabledExpectations(); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue(std::string())); |
+ prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
+ Value::CreateBooleanValue(false)); |
+ |
+ service->Initialize(); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabledThenSetPolicy) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateDisabled); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue( |
+ MockServiceProcessControl::EnabledUserId())); |
+ |
+ service->Initialize(); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+ |
+ prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
+ Value::CreateBooleanValue(false)); |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
validate here that it's still disabled?
Scott Byer
2011/11/15 01:42:23
Done.
|
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabledThenSetPolicy) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateEnabled); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue(std::string())); |
+ |
+ service->Initialize(); |
+ service->RefreshStatusFromService(); |
+ |
+ EXPECT_EQ(MockServiceProcessControl::EnabledUserId(), |
+ prefs->GetString(prefs::kCloudPrintEmail)); |
+ |
+ service->GetMockServiceProcessControl()->SetWillBeDisabledExpectations(); |
+ prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
+ Value::CreateBooleanValue(false)); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, |
+ StartWithPolicySetProxyDisabledThenClearPolicy) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateDisabled); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue(std::string())); |
+ prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
+ Value::CreateBooleanValue(false)); |
+ |
+ service->Initialize(); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+ prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled); |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+} |
+ |
+TEST_F(CloudPrintProxyPolicyTest, |
+ StartWithPolicySetProxyEnabledThenClearPolicy) { |
+ scoped_ptr<TestCloudPrintProxyService> service( |
+ new TestCloudPrintProxyService(&profile_)); |
+ |
+ service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( |
+ MockServiceProcessControl::kServiceStateEnabled); |
+ service->GetMockServiceProcessControl()->SetWillBeDisabledExpectations(); |
+ |
+ TestingPrefService* prefs = profile_.GetTestingPrefService(); |
+ prefs->SetUserPref(prefs::kCloudPrintEmail, |
+ Value::CreateStringValue(std::string())); |
+ prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
+ Value::CreateBooleanValue(false)); |
+ |
+ service->Initialize(); |
+ |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
+ prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled); |
+ EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
I guess an additional test that first sets the pol
Scott Byer
2011/11/15 01:42:23
Thank you! At some point in the middle of the day
|
+} |