mockuseragent_test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.testing.MockUserAgentTest');
  15. goog.require('goog.dispose');
  16. goog.require('goog.testing.MockUserAgent');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.userAgent');
  19. goog.setTestOnly('goog.testing.MockUserAgentTest');
  20. var mockUserAgent;
  21. function setUp() {
  22. mockUserAgent = new goog.testing.MockUserAgent();
  23. }
  24. function tearDown() {
  25. goog.dispose(mockUserAgent);
  26. assertFalse(mockUserAgent.installed_);
  27. }
  28. function testMockUserAgentInstall() {
  29. var originalUserAgentFunction = goog.userAgent.getUserAgentString;
  30. assertFalse(!!mockUserAgent.installed_);
  31. mockUserAgent.install();
  32. assertTrue(mockUserAgent.installed_);
  33. assertNotEquals(goog.userAgent.getUserAgentString, originalUserAgentFunction);
  34. mockUserAgent.uninstall();
  35. assertFalse(mockUserAgent.installed_);
  36. assertEquals(originalUserAgentFunction, goog.userAgent.getUserAgentString);
  37. }
  38. function testMockUserAgentGetAgent() {
  39. var uaString = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) ' +
  40. 'AppleWebKit/525.13 (KHTML, like Gecko) ' +
  41. 'Chrome/0.2.149.27 Safari/525.13';
  42. mockUserAgent = new goog.testing.MockUserAgent();
  43. mockUserAgent.setUserAgentString(uaString);
  44. mockUserAgent.install();
  45. assertTrue(mockUserAgent.installed_);
  46. assertEquals(uaString, goog.userAgent.getUserAgentString());
  47. }
  48. function testMockUserAgentNavigator() {
  49. var fakeNavigator = {};
  50. mockUserAgent = new goog.testing.MockUserAgent();
  51. mockUserAgent.setNavigator(fakeNavigator);
  52. mockUserAgent.install();
  53. assertTrue(mockUserAgent.installed_);
  54. assertEquals(fakeNavigator, goog.userAgent.getNavigator());
  55. }
  56. function testMockUserAgentDocumentMode() {
  57. var fakeDocumentMode = -1;
  58. mockUserAgent = new goog.testing.MockUserAgent();
  59. mockUserAgent.setDocumentMode(fakeDocumentMode);
  60. mockUserAgent.install();
  61. assertTrue(mockUserAgent.installed_);
  62. assertEquals(fakeDocumentMode, goog.userAgent.getDocumentMode_());
  63. assertEquals(fakeDocumentMode, goog.userAgent.DOCUMENT_MODE);
  64. }