Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "core/experiments/Experiments.h" | 5 #include "core/experiments/Experiments.h" |
| 6 | 6 |
| 7 #include "core/HTMLNames.h" | 7 #include "core/HTMLNames.h" |
| 8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
| 9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
| 11 #include "core/html/HTMLDocument.h" | 11 #include "core/html/HTMLDocument.h" |
| 12 #include "core/html/HTMLHeadElement.h" | 12 #include "core/html/HTMLHeadElement.h" |
| 13 #include "core/html/HTMLMetaElement.h" | 13 #include "core/html/HTMLMetaElement.h" |
| 14 #include "core/testing/DummyPageHolder.h" | 14 #include "core/testing/DummyPageHolder.h" |
| 15 #include "platform/weborigin/KURL.h" | 15 #include "platform/weborigin/KURL.h" |
| 16 #include "platform/weborigin/SecurityOrigin.h" | 16 #include "platform/weborigin/SecurityOrigin.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char* kNonExistingAPIName = "This API does not exist"; | 22 const char* kNonExistingAPIName = "This API does not exist"; |
| 23 const char* kFrobulateAPIName = "Frobulate"; | 23 const char* kFrobulateAPIName = "Frobulate"; |
| 24 const char* kFrobulateEnabledOrigin = "https://www.example.com"; | 24 const char* kFrobulateEnabledOrigin = "https://www.example.com"; |
| 25 const char* kFrobulateEnabledOriginUnsecure = "http://www.example.com"; | 25 const char* kFrobulateEnabledOriginUnsecure = "http://www.example.com"; |
| 26 | 26 |
| 27 } // namespace | |
| 28 | |
| 27 class ExperimentsTest : public ::testing::Test { | 29 class ExperimentsTest : public ::testing::Test { |
| 28 protected: | 30 protected: |
| 29 ExperimentsTest() | 31 ExperimentsTest() |
| 30 : m_page(DummyPageHolder::create()) | 32 : m_page(DummyPageHolder::create()) |
| 31 , m_frameworkWasEnabled(RuntimeEnabledFeatures::experimentalFrameworkEna bled()) | 33 , m_frameworkWasEnabled(RuntimeEnabledFeatures::experimentalFrameworkEna bled()) |
| 32 { | 34 { |
| 33 if (!RuntimeEnabledFeatures::experimentalFrameworkEnabled()) { | 35 if (!RuntimeEnabledFeatures::experimentalFrameworkEnabled()) { |
| 34 RuntimeEnabledFeatures::setExperimentalFrameworkEnabled(true); | 36 RuntimeEnabledFeatures::setExperimentalFrameworkEnabled(true); |
| 35 } | 37 } |
| 36 } | 38 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 { | 78 { |
| 77 HTMLElement* head = document().head(); | 79 HTMLElement* head = document().head(); |
| 78 ASSERT_TRUE(head); | 80 ASSERT_TRUE(head); |
| 79 | 81 |
| 80 RefPtrWillBeRawPtr<HTMLMetaElement> meta = HTMLMetaElement::create(docum ent()); | 82 RefPtrWillBeRawPtr<HTMLMetaElement> meta = HTMLMetaElement::create(docum ent()); |
| 81 meta->setAttribute(HTMLNames::nameAttr, "api-experiments"); | 83 meta->setAttribute(HTMLNames::nameAttr, "api-experiments"); |
| 82 meta->setAttribute(HTMLNames::contentAttr, keyValue); | 84 meta->setAttribute(HTMLNames::contentAttr, keyValue); |
| 83 head->appendChild(meta.release()); | 85 head->appendChild(meta.release()); |
| 84 } | 86 } |
| 85 | 87 |
| 86 bool isApiEnabled(const String& origin, const String& apiName, const char* a piKeyValue, String& errorMessage) | 88 bool isApiEnabled(const String& origin, const String& apiName, const char* a piKeyValue, String* errorMessage) |
| 87 { | 89 { |
| 88 setPageOrigin(origin); | 90 setPageOrigin(origin); |
| 89 addApiKey(apiKeyValue); | 91 addApiKey(apiKeyValue); |
| 90 return Experiments::isApiEnabled(executionContext(), apiName, errorMessa ge); | 92 return Experiments::isApiEnabled(executionContext(), apiName, errorMessa ge); |
| 91 } | 93 } |
| 92 | 94 |
| 95 bool isApiEnabledWithoutErrorMessage(const String& origin, const String& api Name, const char* apiKeyValue) | |
| 96 { | |
| 97 return isApiEnabled(origin, apiName, apiKeyValue, nullptr); | |
| 98 } | |
| 99 | |
| 93 private: | 100 private: |
| 94 OwnPtr<DummyPageHolder> m_page; | 101 OwnPtr<DummyPageHolder> m_page; |
| 95 RefPtrWillBePersistent<HTMLDocument> m_document; | 102 RefPtrWillBePersistent<HTMLDocument> m_document; |
| 96 const bool m_frameworkWasEnabled; | 103 const bool m_frameworkWasEnabled; |
| 97 }; | 104 }; |
| 98 | 105 |
| 99 TEST_F(ExperimentsTest, EnabledNonExistingAPI) | 106 TEST_F(ExperimentsTest, EnabledNonExistingAPI) |
| 100 { | 107 { |
| 101 String errorMessage; | 108 String errorMessage; |
| 102 bool isNonExistingApiEnabled = isApiEnabled(kFrobulateEnabledOrigin, | 109 bool isNonExistingApiEnabled = isApiEnabled(kFrobulateEnabledOrigin, |
| 103 kNonExistingAPIName, | 110 kNonExistingAPIName, |
| 104 kFrobulateAPIName /* Use existing api name as the key value */, | 111 kFrobulateAPIName /* Use existing api name as the key value */, |
| 105 errorMessage); | 112 &errorMessage); |
| 106 EXPECT_FALSE(isNonExistingApiEnabled); | 113 EXPECT_FALSE(isNonExistingApiEnabled); |
| 107 EXPECT_EQ(("The provided key(s) are not valid for the 'This API does not exi st' API."), errorMessage); | 114 EXPECT_EQ(("The provided key(s) are not valid for the 'This API does not exi st' API."), errorMessage); |
|
iclelland
2016/01/08 15:11:28
Maybe make this a kExpectedErrorMessage constant a
chasej
2016/01/08 17:37:06
True, they are closely related. Given the expecte
iclelland
2016/01/08 17:59:38
I think it makes it a little clearer where the mag
| |
| 108 } | 115 } |
| 109 | 116 |
| 117 TEST_F(ExperimentsTest, EnabledNonExistingAPIWithoutErrorMessage) | |
| 118 { | |
| 119 bool isNonExistingApiEnabled = isApiEnabledWithoutErrorMessage( | |
| 120 kFrobulateEnabledOrigin, | |
| 121 kNonExistingAPIName, | |
| 122 kFrobulateAPIName /* Use existing api name as the key value */); | |
| 123 EXPECT_FALSE(isNonExistingApiEnabled); | |
| 124 } | |
| 125 | |
| 110 // The API should be enabled if a valid key for the origin is provided | 126 // The API should be enabled if a valid key for the origin is provided |
| 111 TEST_F(ExperimentsTest, EnabledSecureRegisteredOrigin) | 127 TEST_F(ExperimentsTest, EnabledSecureRegisteredOrigin) |
| 112 { | 128 { |
| 113 String errorMessage; | 129 String errorMessage; |
| 114 bool isOriginEnabled = isApiEnabled(kFrobulateEnabledOrigin, | 130 bool isOriginEnabled = isApiEnabled(kFrobulateEnabledOrigin, |
| 115 kFrobulateAPIName, | 131 kFrobulateAPIName, |
| 116 kFrobulateAPIName /* Use just the api name as the key value */, | 132 kFrobulateAPIName /* Use just the api name as the key value */, |
| 117 errorMessage); | 133 &errorMessage); |
| 118 EXPECT_TRUE(isOriginEnabled); | 134 EXPECT_TRUE(isOriginEnabled); |
| 119 EXPECT_TRUE(errorMessage.isEmpty()); | 135 EXPECT_TRUE(errorMessage.isEmpty()); |
| 120 } | 136 } |
| 121 | 137 |
| 138 TEST_F(ExperimentsTest, EnabledSecureRegisteredOriginWithoutErrorMessage) | |
| 139 { | |
| 140 bool isOriginEnabled = isApiEnabledWithoutErrorMessage( | |
| 141 kFrobulateEnabledOrigin, | |
| 142 kFrobulateAPIName, | |
| 143 kFrobulateAPIName /* Use just the api name as the key value */); | |
| 144 EXPECT_TRUE(isOriginEnabled); | |
| 145 } | |
| 146 | |
| 122 // The API should not be enabled if the origin is unsecure, even if a valid | 147 // The API should not be enabled if the origin is unsecure, even if a valid |
| 123 // key for the origin is provided | 148 // key for the origin is provided |
| 124 TEST_F(ExperimentsTest, EnabledNonSecureRegisteredOrigin) | 149 TEST_F(ExperimentsTest, EnabledNonSecureRegisteredOrigin) |
| 125 { | 150 { |
| 126 String errorMessage; | 151 String errorMessage; |
| 127 bool isOriginEnabled = isApiEnabled(kFrobulateEnabledOriginUnsecure, | 152 bool isOriginEnabled = isApiEnabled(kFrobulateEnabledOriginUnsecure, |
| 128 kFrobulateAPIName, | 153 kFrobulateAPIName, |
| 129 kFrobulateAPIName /* Use just the api name as the key value */, | 154 kFrobulateAPIName /* Use just the api name as the key value */, |
| 130 errorMessage); | 155 &errorMessage); |
| 131 EXPECT_FALSE(isOriginEnabled); | 156 EXPECT_FALSE(isOriginEnabled); |
| 132 EXPECT_TRUE(errorMessage.contains("secure origin")) << "Message should indic ate only secure origins are allowed, was: " << errorMessage; | 157 EXPECT_TRUE(errorMessage.contains("secure origin")) << "Message should indic ate only secure origins are allowed, was: " << errorMessage; |
| 133 } | 158 } |
| 134 | 159 |
| 135 TEST_F(ExperimentsTest, DisabledException) | 160 TEST_F(ExperimentsTest, DisabledException) |
| 136 { | 161 { |
| 137 DOMException* disabledException = Experiments::createApiDisabledException(kN onExistingAPIName); | 162 DOMException* disabledException = Experiments::createApiDisabledException(kN onExistingAPIName); |
| 138 ASSERT_TRUE(disabledException) << "An exception should have been created"; | 163 ASSERT_TRUE(disabledException) << "An exception should have been created"; |
| 139 EXPECT_EQ(DOMException::getErrorName(NotSupportedError), disabledException-> name()); | 164 EXPECT_EQ(DOMException::getErrorName(NotSupportedError), disabledException-> name()); |
| 140 EXPECT_TRUE(disabledException->message().contains(kNonExistingAPIName)) << " Message should contain the API name, was: " << disabledException->message(); | 165 EXPECT_TRUE(disabledException->message().contains(kNonExistingAPIName)) << " Message should contain the API name, was: " << disabledException->message(); |
| 141 } | 166 } |
| 142 | 167 |
| 143 } // namespace | |
| 144 } // namespace blink | 168 } // namespace blink |
| OLD | NEW |