Package com.opera.core.systems

Source Code of com.opera.core.systems.OperaProfileTest

/*
Copyright 2011-2012 Opera Software ASA

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.opera.core.systems;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;

import com.opera.core.systems.testing.NoDriver;
import com.opera.core.systems.testing.OperaDriverTestCase;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.openqa.selenium.io.Zip;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

@NoDriver
public class OperaProfileTest extends OperaDriverTestCase {

  private static final String VALID_BASE64_ENCODED_PROFILE =
      "UEsDBBQACAAIAGWKr0AAAAAAAAAAAAAAAAAOAAAAb3BlcmFwcmVmcy5pbmmLDi1OLVIIKEpNK47l\n8kzPyy9KVQjNK0otLE0tLklNUQjILygtKFawVTDg4gIAUEsHCExnhFouAAAALAAAAFBLAQIUABQA\nCAAIAGWKr0BMZ4RaLgAAACwAAAAOAAAAAAAAAAAAAAAAAAAAAABvcGVyYXByZWZzLmluaVBLBQYA\nAAAAAQABADwAAABqAAAAAAA=";
  public OperaProfile profile;
  public File temporaryProfile;
  public File existingProfile;

  @Rule
  public TemporaryFolder existingProfileDirectory;

  @Rule
  public TemporaryFolder temporaryProfileDirectory;

  @Before
  public void beforeEach() throws IOException {
    existingProfileDirectory = new TemporaryFolder();
    temporaryProfileDirectory = new TemporaryFolder();
    existingProfileDirectory.create();
    temporaryProfileDirectory.create();
    existingProfile = existingProfileDirectory.getRoot();
    temporaryProfile = temporaryProfileDirectory.getRoot();

    // Prepare an existing profile.  Because we are afraid to overwrite any test fixtures, we'll
    // copy the existing profile from the fixtures directory into a new temporary file system.
    //
    // TODO(andreastt): Abstract this out into OperaDriverTestCase
    try {
      Files.copy(resources.locate("profile/opera.ini"),
                 new File(existingProfile.getPath() + File.separator + "operaprefs.ini"));
    } catch (IOException e) {
      fail("Unable to prepare new profile");
    }
  }

  @Test
  public void testConstructionWithNoArguments() {  // new temporary profile
    profile = new OperaProfile();
    assertNotNull(profile);
    assertEquals(0, profile.preferences().size());
  }

  @Test
  public void testConstructionWithNewProfileString() {  // use specified (empty) profile
    profile = new OperaProfile(temporaryProfile.getPath());
    assertNotNull(profile);
  }

  @Test
  public void testConstructionWithExistingProfileString() {  // use specified profile
    profile = new OperaProfile(existingProfile.getPath());
    assertNotNull(profile);
    assertEquals(46, profile.preferences().size());
  }

  @Test(expected = IllegalArgumentException.class)
  public void testConstructionWithEmptyString() {
    profile = new OperaProfile("");
  }

  @Test
  public void testConstructionWithNewProfileDirectory() {  // use specified (empty) profile
    profile = new OperaProfile(temporaryProfile);
    assertNotNull(profile);
  }

  @Test
  public void testConstructionWithExistingProfileDirectory() {  // use specified profile
    profile = new OperaProfile(existingProfile);
    assertNotNull(profile);
    assertEquals(46, profile.preferences().size());
  }

  /*
  @Test
  @Ignore // TODO(andreastt): No good reason why we're ignoring this, investigate
  public void testProfileDeleted() throws Exception {
    DesiredCapabilities c = new DesiredCapabilities();
    c.setCapability(OperaDriver.PROFILE, (String) null);

    TestOperaDriver a;
    try {
      a = new TestOperaDriver(c);
    } catch (Exception e) {
      // If immediately exited, then it doesn't support the flags
      if (e.getMessage().contains("Opera exited immediately")) {
        return;
      } else {
        throw e;
      }
    }
    String profile = a.preferences().get("User ScopePrefs", "Opera Directory").toString();
    assertTrue("Temporary directory exists", (new File(profile)).exists());
    a.quit();
    assertFalse("Temporary directory does not exist after quit", (new File(profile)).exists());
  }
  */

  @Test
  public void testPreferences() {
    OperaProfile profile = new OperaProfile();
    assertNotNull(profile.preferences());
  }

  @Test
  public void testToJson() throws IOException, JSONException {
    profile = new OperaProfile(existingProfile);

    String json = profile.toJson().getString(OperaProfile.BASE64_JSON_KEY);
    new Zip().unzip(json, temporaryProfile);
    OperaProfile extractedProfile = new OperaProfile(temporaryProfile.getPath());

    assertThat(profile, matchesProfile(extractedProfile));
  }

  @Test
  public void testFromJson() throws IOException, JSONException {
    OperaProfile data = new OperaProfile(existingProfile);
    profile = TestOperaProfile.fromJson(temporaryProfile, data.toJson().getString(OperaProfile.BASE64_JSON_KEY));

    assertThat(profile, matchesProfile(data));
  }

  @Test
  public void fromCapabilitiesWithProfile() throws Throwable {
    DesiredCapabilities caps = DesiredCapabilities.opera();
    Map<String, String> profileMap = ImmutableMap.of(OperaProfile.BASE64_JSON_KEY, VALID_BASE64_ENCODED_PROFILE);
    caps.setCapability("opera.profile", profileMap);

    // Shouldn't throw
    new OperaSettings().merge(caps);
  }

  private Matcher matchesProfile(final OperaProfile expected) {
    return new BaseMatcher() {
      public boolean matches(Object o) {
        OperaProfile actual = (OperaProfile) o;

        try {
          if ((expected.toJson().toString().equals(actual.toJson().toString())) &&
              (expected.preferences().size() == actual.preferences().size())) {
            return true;
          }
        } catch (IOException e) {
          // fall through, caught by JUnit
        }

        return false;
      }

      public void describeTo(Description description) {
        description.appendText(expected.toString());
      }
    };
  }

  private static class TestOperaProfile extends OperaProfile {

    public static OperaProfile fromJson(File directory, String json) throws IOException {
      new Zip().unzip(json, directory);
      return new OperaProfile(directory);
    }

  }

}
TOP

Related Classes of com.opera.core.systems.OperaProfileTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.