| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/chromeos/login/ui/webui_login_display.h" |
| 11 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" | 14 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" |
| 15 #include "chrome/browser/chromeos/policy/user_policy_test_helper.h" |
| 12 #include "chrome/browser/prefs/session_startup_pref.h" | 16 #include "chrome/browser/prefs/session_startup_pref.h" |
| 13 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/host_desktop.h" | 19 #include "chrome/browser/ui/host_desktop.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "components/user_manager/user.h" |
| 22 #include "components/user_manager/user_manager.h" |
| 23 #include "content/public/browser/notification_observer.h" |
| 24 #include "content/public/browser/notification_registrar.h" |
| 25 #include "content/public/browser/notification_service.h" |
| 17 #include "policy/policy_constants.h" | 26 #include "policy/policy_constants.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 |
| 29 namespace { |
| 30 // Helper class that counts the number of notifications of the specified |
| 31 // type that have been received. |
| 32 class CountNotificationObserver : public content::NotificationObserver { |
| 33 public: |
| 34 CountNotificationObserver(int notification_type_to_count, |
| 35 const content::NotificationSource& source) |
| 36 : notification_count_(0) { |
| 37 registrar_.Add(this, notification_type_to_count, source); |
| 38 } |
| 39 |
| 40 // NotificationObserver: |
| 41 void Observe(int type, |
| 42 const content::NotificationSource& source, |
| 43 const content::NotificationDetails& details) override { |
| 44 // Count the number of notifications seen. |
| 45 notification_count_++; |
| 46 } |
| 47 |
| 48 int notification_count() const { return notification_count_; } |
| 49 |
| 50 private: |
| 51 int notification_count_; |
| 52 content::NotificationRegistrar registrar_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(CountNotificationObserver); |
| 55 }; |
| 56 |
| 57 } // namespace |
| 18 | 58 |
| 19 namespace policy { | 59 namespace policy { |
| 20 | 60 |
| 21 class UserCloudPolicyManagerTest : public LoginPolicyTestBase { | 61 class UserCloudPolicyManagerTest : public LoginPolicyTestBase { |
| 22 protected: | 62 protected: |
| 23 UserCloudPolicyManagerTest() {} | 63 UserCloudPolicyManagerTest() {} |
| 24 | 64 |
| 25 void GetMandatoryPoliciesValue(base::DictionaryValue* policy) const override { | 65 void GetMandatoryPoliciesValue(base::DictionaryValue* policy) const override { |
| 26 scoped_ptr<base::ListValue> list(new base::ListValue); | 66 scoped_ptr<base::ListValue> list(new base::ListValue); |
| 27 list->AppendString("chrome://policy"); | 67 list->AppendString("chrome://policy"); |
| 28 list->AppendString("chrome://about"); | 68 list->AppendString("chrome://about"); |
| 29 | 69 |
| 30 policy->Set(key::kRestoreOnStartupURLs, std::move(list)); | 70 policy->Set(key::kRestoreOnStartupURLs, std::move(list)); |
| 31 policy->SetInteger(key::kRestoreOnStartup, | 71 policy->SetInteger(key::kRestoreOnStartup, |
| 32 SessionStartupPref::kPrefValueURLs); | 72 SessionStartupPref::kPrefValueURLs); |
| 33 } | 73 } |
| 34 | 74 |
| 35 private: | 75 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); | 76 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); |
| 37 }; | 77 }; |
| 38 | 78 |
| 39 IN_PROC_BROWSER_TEST_F(UserCloudPolicyManagerTest, StartSession) { | 79 IN_PROC_BROWSER_TEST_F(UserCloudPolicyManagerTest, StartSession) { |
| 40 const char* const kStartupURLs[] = {"chrome://policy", "chrome://about"}; | 80 const char* const kStartupURLs[] = {"chrome://policy", "chrome://about"}; |
| 41 | 81 |
| 42 SkipToLoginScreen(); | 82 SkipToLoginScreen(); |
| 43 LogIn(kAccountId, kAccountPassword); | 83 LogIn(kAccountId, kAccountPassword); |
| 44 | 84 |
| 85 // User should be marked as having a valid OAuth token. |
| 86 const user_manager::UserManager* const user_manager = |
| 87 user_manager::UserManager::Get(); |
| 88 EXPECT_EQ(user_manager::User::OAUTH2_TOKEN_STATUS_VALID, |
| 89 user_manager->GetActiveUser()->oauth_token_status()); |
| 90 |
| 45 // Check that the startup pages specified in policy were opened. | 91 // Check that the startup pages specified in policy were opened. |
| 46 BrowserList* browser_list = BrowserList::GetInstance(); | 92 BrowserList* browser_list = BrowserList::GetInstance(); |
| 47 EXPECT_EQ(1U, browser_list->size()); | 93 EXPECT_EQ(1U, browser_list->size()); |
| 48 Browser* browser = browser_list->get(0); | 94 Browser* browser = browser_list->get(0); |
| 49 ASSERT_TRUE(browser); | 95 ASSERT_TRUE(browser); |
| 50 | 96 |
| 51 TabStripModel* tabs = browser->tab_strip_model(); | 97 TabStripModel* tabs = browser->tab_strip_model(); |
| 52 ASSERT_TRUE(tabs); | 98 ASSERT_TRUE(tabs); |
| 53 const int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); | 99 const int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); |
| 54 EXPECT_EQ(expected_tab_count, tabs->count()); | 100 EXPECT_EQ(expected_tab_count, tabs->count()); |
| 55 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) { | 101 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) { |
| 56 EXPECT_EQ(GURL(kStartupURLs[i]), | 102 EXPECT_EQ(GURL(kStartupURLs[i]), |
| 57 tabs->GetWebContentsAt(i)->GetVisibleURL()); | 103 tabs->GetWebContentsAt(i)->GetVisibleURL()); |
| 58 } | 104 } |
| 59 } | 105 } |
| 60 | 106 |
| 107 IN_PROC_BROWSER_TEST_F(UserCloudPolicyManagerTest, ErrorLoadingPolicy) { |
| 108 // Delete the policy file - this will cause a 500 error on policy requests. |
| 109 user_policy_helper()->DeletePolicyFile(); |
| 110 SkipToLoginScreen(); |
| 111 CountNotificationObserver observer( |
| 112 chrome::NOTIFICATION_SESSION_STARTED, |
| 113 content::NotificationService::AllSources()); |
| 114 GetLoginDisplay()->ShowSigninScreenForCreds(kAccountId, kAccountPassword); |
| 115 base::RunLoop().Run(); |
| 116 // Should not receive a SESSION_STARTED notification. |
| 117 ASSERT_EQ(0, observer.notification_count()); |
| 118 |
| 119 // User should not be marked as having a valid OAuth token. That way, if we |
| 120 // try to load the user in the future, we will attempt to load policy again. |
| 121 const user_manager::UserManager* user_manager = |
| 122 user_manager::UserManager::Get(); |
| 123 EXPECT_NE(user_manager::User::OAUTH2_TOKEN_STATUS_VALID, |
| 124 user_manager->GetActiveUser()->oauth_token_status()); |
| 125 } |
| 126 |
| 61 } // namespace policy | 127 } // namespace policy |
| OLD | NEW |